/* * matopt.java - revised 19 Apr 05 - width 601, height 301 * matrix optics ray tracing program * rays traced from an object in H(-) to its image in H'(-) * @author jack@ord.ca */ import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class matopt extends java.applet.Applet implements ActionListener { int kk=0; int cont=0; int opt=1; int first=0; // Declarations int ncont=0; int nmat=0; int matn=0; int done=0; double nleft=1; double nright=1; double r=1; double d=1; double a11, a12, a21, a22; double b11, b12, b21, b22; double x11, x12, x21, x22; double [] p=new double[10]; TextField txtb; public void init() { txtb=new TextField(15); setBackground(new Color(211, 211, 211)); txtb.setBackground(getBackground()); add(txtb); txtb.addActionListener(this); } public void pin(Graphics g) { // Initialize Plot g.setColor(Color.black); g.drawRect(0, 0, 600, 300); if (first==0) { g.setFont(new Font("TimesRoman", Font.PLAIN, 14)); txtb.setBounds(5, 25, 120, 20); txtb.setText("Click and hit Enter"); first=1; } if (done==0) { g.drawString("1 Translation", 140, 15); g.drawString("2 Curved Surface", 140, 30); g.drawString("3 Plane Surface", 140, 45); g.drawString("4 Thin Lens", 140, 60); } } public void paint(Graphics g) { // Paint double d1, d2, f1, f2, m, det; double xmin, xmax, xsc; int q [] = new int[10]; int cr; pin(g); if ((kk>3)&&(cont==0)) { // Print Matrices g.setColor(Color.blue); g.drawString("Matrix "+matn, 320, 15); g.drawString(Float.toString((float)b11), 380, 15); g.drawString(Float.toString((float)b12), 480, 15); g.drawString(Float.toString((float)b21), 380, 30); g.drawString(Float.toString((float)b22), 480, 30); g.setColor(Color.red); g.drawString("System", 320, 45); g.drawString("Matrix", 320, 60); g.drawString(Float.toString((float)a11), 380, 45); g.drawString(Float.toString((float)a12), 480, 45); g.drawString(Float.toString((float)a21), 380, 60); g.drawString(Float.toString((float)a22), 480, 60); } if (done>0) { det=a11*a22-a21*a12; // Determinant g.drawString("Determinant = "+(float)det, 380, 90); g.setColor(Color.black); if ((Math.abs(a21))<.000001) { // Telescopic system m=1/a22; g.drawString("Telescopic System", 140, 30); g.drawString("Angular M = "+(float)m, 140, 45); } else { // Cardinal points d1=a11/a21; d2=a22/a21; f1=1/a21; f2=f1*det; p[0]=-d1-f1; p[1]=-d1; p[2]=-d1+f1; p[3]=-d1+f2; p[4]=p[9]+d2-f1; p[5]=p[9]+d2-f2; p[6]=p[9]+d2; p[7]=p[9]+d2+f2; g.drawString("Focal points relative to O-O'", 140, 15); g.drawString("OF = "+(float)d1, 160, 30); g.drawString("O'F' = "+(float)d2, 160, 45); g.drawString("The focal lengths f and f'", 140, 60); g.drawString("f = "+(float)f1, 160, 75); g.drawString("f' = "+(float)f2, 160, 90); g.drawString("Principal points relative to O-O'", 140, 105); g.drawString("OH = "+(float)(d1-f1), 160, 120); g.drawString("O'H' = "+(float)(d2-f2), 160, 135); xmin=p[0]; xmax=p[0]; // p[0] H(-) for (int i=1; i<=9; i=i+1) { // p[1] F if (p[i]xmax) { // p[5] H' xmax=p[i]; // p[6] F' } // p[7] H'(-) } // p[8] O xsc=500/(xmax-xmin); // p[9] O' for (int i=0; i<=9; i=i+1) { q[i]=50+(int)((p[i]-xmin)*xsc+.5); } g.setColor(Color.lightGray); g.fillRect(q[8], 140, q[9]-q[8], 159); g.setColor(Color.black); g.drawLine(25, 220, 575, 220); g.setColor(Color.blue); g.drawLine(q[2], 145, q[2], 295); g.drawLine(q[5], 145, q[5], 295); g.drawString("H", q[2]-12, 155); g.drawString("H'", q[5]+4, 155); g.setColor(Color.cyan); g.drawLine(q[0], 160, q[2], 280); g.drawLine(q[5], 280, q[7], 280); // object at H(-) g.setColor(Color.green); // three rays to g.drawLine(q[0], 160, q[2], 160); // inverted image g.drawLine(q[5], 160, q[7], 280); // at H'(-) g.setColor(Color.yellow); g.drawLine(q[0], 160, q[3], 220); g.drawLine(q[4], 220, q[7], 280); g.setColor(Color.red); g.drawLine(q[0], 160, q[0], 220); g.drawLine(q[0], 160, q[0]-2, 170); g.drawLine(q[0], 160, q[0]-1, 170); g.drawLine(q[0], 160, q[0]+1, 170); g.drawLine(q[0], 160, q[0]+2, 170); g.drawLine(q[7], 280, q[7], 220); g.drawLine(q[7], 280, q[7]-2, 270); g.drawLine(q[7], 280, q[7]-1, 270); g.drawLine(q[7], 280, q[7]+1, 270); g.drawLine(q[7], 280, q[7]+2, 270); cr=3; g.fillArc(q[1]-cr, 220-cr, 2*cr, 2*cr, 0, 360);// F g.fillArc(q[3]-cr, 220-cr, 2*cr, 2*cr, 0, 360);// N g.fillArc(q[4]-cr, 220-cr, 2*cr, 2*cr, 0, 360);// N' g.fillArc(q[6]-cr, 220-cr, 2*cr, 2*cr, 0, 360);// F' g.drawString("F", q[1]-12, 235); g.drawString("F'", q[6]+4, 215); g.drawString("N", q[3]-12, 235); g.drawString("N'", q[4]+4, 215); g.setColor(Color.black); } txtb.setText("Incident Index"); txtb.selectAll(); kk=1; p[8]=0; p[9]=0; done=0; cont=0; ncont=0; matn=0; } } public void actionPerformed(ActionEvent ev) { String txt; int er=0; txt=txtb.getText(); if ((txt.equals("q"))||(txt.equals("Q"))) { kk=0; } if (kk==1) { try { nleft=Double.valueOf(txt).doubleValue(); } catch(NumberFormatException e) { er=1; txtb.setText("ReTry Incident Index"); } if (er==0) { txtb.setText("# of Matrices"); } } if (kk==2) { try { nmat=Integer.parseInt(txt); } catch(NumberFormatException e) { er=1; txtb.setText("ReTry # of Matrices"); } if (er==0) { txtb.setText("Choose ->"); } } if (kk>2) { if (cont==0) { try { opt=Integer.parseInt(txt); } catch(NumberFormatException e) { er=1; txtb.setText("ReTry Choose ->"); } if (er==0) { switch (opt) { case 1 : txtb.setText("Displacement (+)"); break; case 2 : txtb.setText("Radius (+/-)"); break; case 3 : txtb.setText("nright"); break; case 4 : txtb.setText("f (+/-)"); break; } ncont=1; } } if (cont>0) { try { d=Double.valueOf(txt).doubleValue(); } catch(NumberFormatException e) { er=1; txtb.setText("Error - ReTry"); } } if (er==0) { if (cont==1) { switch (opt) { case 1 : b11=1; b12=-d; b21=0; b22=1; ncont=0; p[9]=p[9]+d; break; case 2 : r=d; txtb.setText("nright"); ncont=2; break; case 3 : nright=d; b11=1; b12=0; b21=0; b22=nright/nleft; nleft=nright; ncont=0; break; case 4 : b11=1; b12=0; b21=1/d; b22=1; ncont=0; break; } } if (cont==2) { nright=d; b11=1; b12=0; b21=(nright-nleft)/(nleft*r); b22=nright/nleft; nleft=nright; ncont=0; } cont=ncont; if ((cont==0)&&(kk>2)) { matn=matn+1; if (matn==1) { a11=b11; a12=b12; a21=b21; a22=b22; } else { x11=a11*b11+a12*b21; x12=a11*b12+a12*b22; x21=a21*b11+a22*b21; x22=a21*b12+a22*b22; a11=x11; a12=x12; a21=x21; a22=x22; } if (matn==nmat) { done=1; } else { txtb.setText("Choose ->"); } } } } if (kk==0) { txtb.setText("Incident Index"); p[8]=0; p[9]=0; done=0; cont=0; ncont=0; matn=0; } if (er==0) { kk=kk+1; } txtb.selectAll(); repaint(); } }