public void paint(Graphics g) {
| setBackground(Color.white); |
| offscreenG.clearRect(0,0,width,height); |
| offscreenG.setColor(Color.black); |
| offscreenG.drawLine(10,height/2,width-20,height/2); |
| offscreenG.drawString("X",width-13, height/2+4); |
| offscreenG.drawLine(width/2,20,width/2,height-10); |
| offscreenG.drawString("Y", width/2-4,13); |
| offscreenG.drawString("[ " + matrix[0].toString() + " " + |
| matrix[1].toString(), 10, 45); |
| offscreenG.drawString(" " + matrix[2].toString() + " " + |
| matrix[3].toString() + " ]", 10, 65); |
| //Label axes with hash marks |
| for(i = 5; i < c.center_x(width-20); i+=5){ |
| offscreenG.drawLine(c.left_x(i),height/2+3,c.left_x(i), |
| offscreenG.drawLine(c.left_x(-i),height/2+3,c.left_x(-i), |
| //label every 10th coordinate |
| Integer inum = new Integer(i); |
| Integer inumneg = new Integer(-i); |
| offscreenG.drawString(inum.toString(),c.left_x(i)-6, |
| offscreenG.drawString(inumneg.toString(),c.left_x(-i)-10, |
| for(i=5;i < (int)(height/2-10)/10; i+=5){ |
| offscreenG.drawLine(width/2-3,c.left_y(i),width/2+3, |
| offscreenG.drawLine(width/2-3,c.left_y(-i),width/2+3, |
| //label every 10th coordinate |
| Integer inum = new Integer(i); |
| Integer inumneg = new Integer(-i); |
| offscreenG.drawString(inum.toString(),width/2-20, |
| offscreenG.drawString(inumneg.toString(),width/2-25, |
| //if mouse is over applet, print coordinates |
| offscreenG.drawString("Mouse: (" + x_coord + "," + y_coord + ")", |
| //if mouse has been pressed, draw vector from origin with label |
| offscreenG.drawLine(c.left_x(0),c.left_y(0),c.left_x( |
| x_vector.floatValue()),c.left_y(y_vector.floatValue())); |
| offscreenG.drawString("Vector: [ " + x_vector + " " + y_vector + |
| " ]",width-150,height-10); |
| //if matrix has been entered, draw vector times matrix |
| xmat = x_vector.floatValue()*matrix[0].floatValue()+ |
| y_vector.floatValue()*matrix[2].floatValue(); |
| ymat = x_vector.floatValue()*matrix[1].floatValue()+ |
| y_vector.floatValue()*matrix[3].floatValue(); |
| offscreenG.drawLine(c.left_x(0),c.left_y(0),c.left_x(xmat), |
| g.drawImage(offscreenImg,0,0,this); //Paint offscreen buffer to screen |
}
//if mouse enters applet, set mouse coordinates
public boolean mouseEnter(Event evt, int x, int y) {
| x_coord = new Float(c.center_x(x)); |
| y_coord = new Float(c.center_y(y)); |
}
//if mouse moves in applet, change mouse coordinates
public boolean mouseMove(Event evt, int x, int y) {
| x_coord = new Float(c.center_x(x)); |
| y_coord = new Float(c.center_y(y)); |
}
//if mouse exits applet, stop evaluating current mouse position
public boolean mouseExit(Event evt, int x, int y) {
}
//if dragging mouse, change vector and mouse coordinates
public boolean mouseDrag(Event evt, int x, int y) {
| x_coord = x_vector = new Float(c.center_x(x)); |
| y_coord = y_vector = new Float(c.center_y(y)); |
}
| //if user clicks, draw vector and current mouse coordinates |
| public boolean mouseDown(Event evt, int x, int y) { |
| x_coord = x_vector = new Float(c.center_x(x)); |
| y_coord = y_vector = new Float(c.center_y(y)); |
| public boolean action(Event event, Object o) { |
| //check matrix has been entered |
| if(event.target instanceof TextField) { |
| if(event.target == text1) { |
| matrix[0] = new Double((String)event.arg); |
| if(event.target == text2) { |
| matrix[1] = new Double((String)event.arg); |
| if(event.target == text3) { |
| matrix[2] = new Double((String)event.arg); |
| if(event.target == text4) { |
| matrix[3] = new Double((String)event.arg); |
}