EigenvectorDemo2.java

//This applet executes a demo to find the eigenvector of a
//2 dimensional matrix graphically
import java.awt.*;
import java.lang.Integer;
import CoordTransform;
public class EigenvectorDemo2 extends java.applet.Applet {
Float x_coord = null;//x position of mouse
Float y_coord = null;//y position of mouse
Float x_vector = null; //x position of vector
Float y_vector = null; //y position of vector
int i = 0; //counter
TextField text1; //location user enters matrix
TextField text2;
TextField text3;
TextField text4;
Image offscreenImg; //used for double buffering
Graphics offscreenG; //used for double buffering
int width; //width of applet
int height; //height of applet
CoordTransform c; //used to transform to user coords
Double matrix[];
int matrixflag = 0;
public void init(){
matrix = new Double[4];
width = this.size().width;
height = this.size().height;
c = new CoordTransform(width,height);
//print text box for entering matrix
setBackground(Color.white);
setLayout(new FlowLayout(FlowLayout.LEFT));
text1 = new TextField(4);
text2 = new TextField(4);
text3 = new TextField(4);
text4 = new TextField(4);
add(new Label("Matrix: "));
add(text1);
add(text2);
add(text3);
add(text4);
//create offscreen buffer
offscreenImg = createImage(width,height);
offscreenG = offscreenImg.getGraphics();
}
public void update(Graphics g){
paint(g);
}
1


EigenvectorDemo2.java

public void paint(Graphics g) {
setBackground(Color.white);
//Clear screen
offscreenG.clearRect(0,0,width,height);
//Draw x and y axes
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);
//Print out Matrix
if(matrixflag > 3) {
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),
height/2-3);
offscreenG.drawLine(c.left_x(-i),height/2+3,c.left_x(-i),
height/2-3);
//label every 10th coordinate
if(i%10 == 0){
Integer inum = new Integer(i);
Integer inumneg = new Integer(-i);
offscreenG.drawString(inum.toString(),c.left_x(i)-6,
height/2+15);
offscreenG.drawString(inumneg.toString(),c.left_x(-i)-10,
height/2+15);
}
}
for(i=5;i < (int)(height/2-10)/10; i+=5){
offscreenG.drawLine(width/2-3,c.left_y(i),width/2+3,
c.left_y(i));
offscreenG.drawLine(width/2-3,c.left_y(-i),width/2+3,
c.left_y(-i));
//label every 10th coordinate
if(i%10 == 0){
Integer inum = new Integer(i);
Integer inumneg = new Integer(-i);
offscreenG.drawString(inum.toString(),width/2-20,
c.left_y(i)+4);
offscreenG.drawString(inumneg.toString(),width/2-25,
c.left_y(-i)+4);
}
}
//if mouse is over applet, print coordinates
if(x_coord != null)
offscreenG.drawString("Mouse: (" + x_coord + "," + y_coord + ")",
2


EigenvectorDemo2.java
10, height-10);
//if mouse has been pressed, draw vector from origin with label
if(x_vector != null){
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
if(matrixflag > 3) {
float xmat,ymat;
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),
c.left_y(ymat));
}
}
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));
repaint();
return true;

}
//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));
repaint();
return true;

}
//if mouse exits applet, stop evaluating current mouse position
public boolean mouseExit(Event evt, int x, int y) {
x_coord = null;
y_coord = null;
repaint();
return true;

}
//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));
repaint();
return true;

}
3


EigenvectorDemo2.java
//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));
repaint();
return true;
}
//get matrix entries
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);
matrixflag++;
}
if(event.target == text2) {
matrix[1] = new Double((String)event.arg);
matrixflag++;
}
if(event.target == text3) {
matrix[2] = new Double((String)event.arg);
matrixflag++;
}
if(event.target == text4) {
matrix[3] = new Double((String)event.arg);
matrixflag++;
}
return true;
}
return false;
}

}
4