/* * df3.java - revised 4 Feb 10 - width 286, height 241 * @author jackord@kw.igs.net * The diffraction pattern produced when light from a helium-neon laser * strikes either * (1) an equilateral triangular aperture (side d=.05 mm), * (2) a rectangular aperture (.05x.025 mm), or * (3) a square aperture (side .05 mm) * and falls on a * screen 1000 mm away (125 mm if "z/8" is chosen for the triangle). * One pixel represents 1 mm on the screen. * The numerical calculation uses * (1) a close-packed array for the triangle with 50 points along d * (25 if "n/2" is chosen) * (2) a square 1024x512 grid for the rectangle * (3) a square 1024x1024 grid for the square. * The rectangle and square use the fast algorithm. * The plot is "overexposed" to make lower intensity variations visible. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class df3 extends Applet implements ActionListener { int kk=0; // Declarations String bb1s="Tri"; Button bb1=new Button(bb1s); String bb2s="Rct"; Button bb2=new Button(bb2s); String bb3s="Sqr"; Button bb3=new Button(bb3s); Checkbox ch1=new Checkbox("n/2"); Checkbox ch2=new Checkbox("z/8"); public void init() { add(bb1); bb1.addActionListener(this); add(ch1); add(ch2); add(bb2); bb2.addActionListener(this); add(bb3); bb3.addActionListener(this); setBackground(new Color(211, 211, 211)); ch1.setBackground(getBackground()); ch2.setBackground(getBackground()); } public void paint(Graphics g) { // Main Program bb1.setBounds(3, 5, 35, 20); ch1.setBounds(3, 30, 35, 20); ch2.setBounds(3, 55, 35, 20); bb2.setBounds(3, 80, 35, 20); bb3.setBounds(3, 105, 35, 20); double la=.633/1000; // Helium-neon red double b, by, d, dd, z, zz, a, r0, qr, qi, rr, ri, sr, si, t, sc; int c, n, nb, ni, nm, x0, y0; double pi=Math.PI; g.setColor(Color.black); // Initial screen g.drawRect(0, 0, 280, 240); d=.05; x0=160; y0=120; long tt=System.currentTimeMillis(); if (kk==1) { if (ch1.getState()==true) { n=25; } else { n=50; } if (ch2.getState()==true) { z=125; } else { z=1000; } dd=d/n; zz=z*z; nm=n*(n+1)/2; sc=255000./nm/nm; for (int ny=-120; ny<=120; ny=ny+1) { // Screen Y loop for (int nx=0; nx<=120; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx+ny*ny); sr=0; si=0; a=.5*(1+z/r0)*z/r0; b=2*pi*dd/r0/la; by=b*Math.sqrt(3)/2; // Close-packed array for (int j=0; j<(n-1); j=j+1) { // Aperture Y loop for (int i=1; i<=n-j; i=i+1) { // Aperture X loop t=b*nx*(i+.5*j)+by*ny*j; sr=sr+Math.cos(t); si=si+Math.sin(t); } } c=(int)((sr*sr+si*si)*a*a*sc); // Overexpose if (c>255) { c=255; } // and cut off max Color col=new Color(c, 0, 0); g.setColor(col); g.drawLine(x0+nx, y0-ny, x0+nx, y0-ny); // Plot pattern g.drawLine(x0-nx, y0-ny, x0-nx, y0-ny); // assming x symmetry } } g.setColor(Color.white); // Show g.drawLine(x0-20, y0+12, x0+20, y0+12); // aperture orientation g.drawLine(x0+20, y0+12, x0, y0-24); g.drawLine(x0, y0-24, x0-20, y0+12); g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 42, 235); } if (kk>1) { ch1.setState(false); ch2.setState(false); nb=10; n=1024; dd=d/n; z=1000.; zz=z*z; sc=255000./n/n/n/n; if (kk==2 ) { sc=sc*4; } for (int ny=0; ny<=119; ny=ny+1) { // Screen Y loop for (int nx=0; nx<=119; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx+ny*ny); a=.5*(1+z/r0)*z/r0; b=2*pi*dd/r0/la; sr=1; si=0; ni=1; for (int i=1; i<=nb; i=i+1) { // Aperture X loop t=b*ni*nx; rr=Math.cos(t); ri=Math.sin(t); qr=sr+rr*sr-ri*si; si=si+rr*si+ri*sr; sr=qr; ni=2*ni; } ni=1; // Aperture Y loop for (int i=1; i<=nb-1+(int)((kk-1)/2); i=i+1) { t=b*ni*ny; rr=Math.cos(t); ri=Math.sin(t); qr=sr+rr*sr-ri*si; si=si+rr*si+ri*sr; sr=qr; ni=2*ni; } c=(int)((sr*sr+si*si)*a*a*sc); if (c>255) { c=255; } // Overexpose Color col=new Color(c, 0, 0); g.setColor(col); g.drawLine(x0+nx, y0+ny, x0+nx, y0+ny); // Plot pattern g.drawLine(x0+nx, y0-ny, x0+nx, y0-ny); // Plot pattern g.drawLine(x0-nx, y0+ny, x0-nx, y0+ny); // Plot pattern g.drawLine(x0-nx, y0-ny, x0-nx, y0-ny); // Plot pattern } } g.setColor(Color.white); // Show if (kk==2) { g.drawRect(x0-20, y0-10, 40, 20); } // Rectangle orientation else { g.drawRect(x0-20, y0-20, 40, 40); } // Square orientation g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 42, 235); } kk=0; } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (bb1s.equals(tst)) { kk=1; } if (bb2s.equals(tst)) { kk=2; } if (bb3s.equals(tst)) { kk=3; } repaint(); } }