/* file la.java let user enter 2x2 matrix A. show vectors for x and Ax when user clicks point x */ import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.event.*; class con extends Panel implements ActionListener, ItemListener { la ap; Button bt; CheckboxGroup cg; Checkbox cbvec,cbcir,cbbox; public con(la ap) { super(); this.ap = ap; setBackground(new Color(192,96,0)); cg = new CheckboxGroup(); cbvec = new Checkbox("Vectors",true,cg); cbvec.addItemListener(this); cbcir = new Checkbox("Circles",false,cg); cbcir.addItemListener(this); cbbox = new Checkbox("Boxes",false,cg); cbbox.addItemListener(this); bt = new Button("Clear"); bt.addActionListener(this); setLayout(new GridLayout(9,1)); add(bt); add(cbvec); add(cbcir); add(cbbox); /*Canvas xicon = new Canvas(); xicon.setBackground(ap.xcolor); Graphics xig = xicon.getGraphics(); xig.drawRect(2,5,3,8); !!!xig.drawString("x",5,5); add(xicon); */ //getGraphics().drawString("x color",10,60); } public void paint(Graphics g) // doesn't show up { g.setColor(ap.xcolor); g.drawString("x",5,80); g.setColor(ap.Axcolor); g.drawString("Ax",5,50); g.setColor(Color.black); } public void actionPerformed(ActionEvent e) { String s = (String)e.getActionCommand(); if(s.equals("Clear")) { ap.graf.repaint(); ap.txt.setText(""); ap.setdefaults(); } } public void itemStateChanged(ItemEvent e) { /* lots of trouble with this. 3 things that did not work: if(e.getSource() == cbvec)... if(e.getItem() == cbvec)... if(cbvec.getState() == true)... */ if(cg.getSelectedCheckbox() == cbvec) ap.cbstate = ap.VEC; else if(cg.getSelectedCheckbox() == cbcir) ap.cbstate = ap.CIR; else ap.cbstate = ap.BOX; } } public class la extends java.applet.Applet { graph graf; double a,b,c,d,x1,x2,Ax1,Ax2; TextArea txt; con ctrls; int cbstate; static final int VEC = 0, CIR = 1, BOX = 2; Color xcolor,Axcolor; public void start(){} public void stop(){} public void init() { graf = new graph(this); txt = new TextArea("Welcome!\n",10,30); txt.setBackground(Color.white); xcolor = Color.orange; Axcolor = Color.blue; ctrls = new con(this); setLayout(new BorderLayout(3,3)); add("West",ctrls); add("Center",graf); add("East",txt); a = 1.2; b = 0; c = 0; d = 0.5; cbstate = VEC; setdefaults(); } public void setdefaults() { txt.append(" Click in the graph area.\n"); txt.append("This defines a vector x.\n"); txt.append(" If the 'Vectors' checkbox\n"); txt.append("is selected,\n"); txt.append("the applet will draw x in\n"); txt.append("orange and Ax in blue,\n"); txt.append("where A = [a b]\n"); txt.append(" [c d]\n"); txt.append(" If the 'Circles' checkbox\n"); txt.append("is selected, we draw a\n"); txt.append("the circle through x and \n"); txt.append("show what A does to it. \n"); txt.append(" If 'Boxes' is selected\n"); txt.append("we draw a box through x and\n"); txt.append("the origin and show how A\n"); txt.append("transforms it.\n"); txt.append(" Change a,b,c,d here too.\n\n"); txt.append("a = "+Double.toString(a)+"\n"); txt.append("b = "+Double.toString(b)+"\n"); txt.append("c = "+Double.toString(c)+"\n"); txt.append("d = "+Double.toString(d)+"\n\n"); } public void parse() { StringTokenizer l = new StringTokenizer(txt.getText(),"\n;"); while(l.hasMoreTokens()) { if(!doline(l.nextToken())) { System.out.println(" Couldn't read elements. "); return; } } } public boolean doline(String line) { // very crude parser reads the matrix entries a,b,c,d and // ignores lines beginning with other characters char c1; int fl = line.length(), j = 0; char form[] = line.toCharArray(); while(( j < fl)&&Character.isSpaceChar(form[j])) j++; if( j == fl) return true; String str; c1 = form[j]; switch(c1) { case 'a': while(((c1 = form[j]) != '=')&&( j < fl)) ++j; str = String.valueOf(form,j+1,fl-j-1); a = Double.valueOf(str).doubleValue(); break; case 'b': while(((c1 = form[j]) != '=')&&( j < fl)) ++j; str = String.valueOf(form,j+1,fl-j-1); b = Double.valueOf(str).doubleValue(); break; case 'c': while(((c1 = form[j]) != '=')&&( j < fl)) ++j; str = String.valueOf(form,j+1,fl-j-1); c = Double.valueOf(str).doubleValue(); break; case 'd': while(((c1 = form[j]) != '=')&&( j < fl)) ++j; str = String.valueOf(form,j+1,fl-j-1); d = Double.valueOf(str).doubleValue(); break; default: // ignore the comments in the txt } return true; } } class graph extends Canvas implements MouseListener { private la ap; int wd,ht,hafwd,hafht; public graph(la ap) { super(); this.ap = ap; setBackground(new Color(128,64,0)); setVisible(true); setSize(300,300); wd = getSize().width; ht = getSize().height; hafwd = (int)(wd/2); hafht = (int)(ht/2); addMouseListener(this); } public void paint(Graphics g) { g.clearRect(0,0,wd,ht); // draw x axis, y axis g.drawLine(0,hafht,wd,hafht); g.drawLine(hafwd,0,hafwd,ht); } public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e) { ap.parse(); if(ap.cbstate == ap.VEC) drawvectors(e); else if(ap.cbstate == ap.CIR) drawcircles(e); else drawboxes(e); } public void drawvectors(MouseEvent e) { Graphics g = getGraphics(); // use pixels as coordinates, and just translate origin // no rescaling is done double x1 = e.getX() - hafwd, x2 = hafht - e.getY(), // double_y = hafht - int_Y Ax1 = ap.a*x1+ap.b*x2, Ax2 = ap.c*x1+ap.d*x2; // the origin is (hafwd,hafht). g.setColor(ap.xcolor); g.drawLine(hafwd,hafht,e.getX(),e.getY()); g.setColor(ap.Axcolor); g.drawLine(hafwd,hafht,hafwd+(int)Ax1,hafht-(int)Ax2); g.setColor(Color.black); // write the coordinates into the text area ap.txt.append("x = ("+Double.toString(x1) +","+Double.toString(x2)+")\n"); ap.txt.append("Ax = ("+Double.toString(Ax1) +","+Double.toString(Ax2)+")\n"); } public void drawcircles(MouseEvent e) { Graphics g = getGraphics(); double x1 = e.getX() - hafwd, x2 = hafht - e.getY(), r = Math.sqrt(x1*x1+x2*x2); g.setColor(ap.xcolor); g.drawOval(hafwd - (int)r,hafht - (int)r,2*(int)r,2*(int)r); g.setColor(ap.Axcolor); double x = r*Math.cos(0), y = r*Math.sin(0), Ax,Ay; int Pts = 25; for( int k = 1; k < 2*Pts+1; ++k) { double xnu = r*Math.cos(k*Math.PI/Pts), ynu = r*Math.sin(k*Math.PI/Pts); g.drawLine(hafwd+(int)(ap.a*x+ap.b*y), hafht-(int)(ap.c*x+ap.d*y), hafwd+(int)(ap.a*xnu+ap.b*ynu), hafht-(int)(ap.c*xnu+ap.d*ynu)); x = xnu; y = ynu; } } public void drawboxes(MouseEvent e) { Graphics g = getGraphics(); // double_y = hafht - int_Y g.setColor(ap.xcolor); int xlo = e.getX()-hafwd, ylo = hafht - e.getY(), xhi = 0, yhi = 0, t; if(xlo > xhi) { t = xhi; xhi = xlo; xlo = t;} if(ylo > yhi) { t = yhi; yhi = ylo; ylo = t;} g.drawRect(hafwd+xlo,hafht-yhi,xhi-xlo,yhi-ylo); g.setColor(ap.Axcolor); // draw image of segment from (xlo,ylo) to (xlo,yhi) double x1 = ap.a*xlo+ap.b*ylo, x2 = ap.a*xlo+ap.b*yhi, y1 = ap.c*xlo+ap.d*ylo, y2 = ap.c*xlo+ap.d*yhi; g.drawLine(hafwd+(int)x1,hafht-(int)y1, hafwd+(int)x2,hafht-(int)y2); // image of segment from (xlo,ylo) to (xhi,ylo) x1 = ap.a*xlo+ap.b*ylo; x2 = ap.a*xhi+ap.b*ylo; y1 = ap.c*xlo+ap.d*ylo; y2 = ap.c*xhi+ap.d*ylo; g.drawLine(hafwd+(int)x1,hafht-(int)y1, hafwd+(int)x2,hafht-(int)y2); // image of segment from (xhi,ylo) to (xhi,yhi) x1 = ap.a*xhi+ap.b*ylo; x2 = ap.a*xhi+ap.b*yhi; y1 = ap.c*xhi+ap.d*ylo; y2 = ap.c*xhi+ap.d*yhi; g.drawLine(hafwd+(int)x1,hafht-(int)y1, hafwd+(int)x2,hafht-(int)y2); // image of segment from (xlo,yhi) to (xhi,yhi) x1 = ap.a*xlo+ap.b*yhi; x2 = ap.a*xhi+ap.b*yhi; y1 = ap.c*xlo+ap.d*yhi; y2 = ap.c*xhi+ap.d*yhi; g.drawLine(hafwd+(int)x1,hafht-(int)y1, hafwd+(int)x2,hafht-(int)y2); } } // end la.java