/* * dif2.java - revised 31 Jan 07 - width 241, height 241 * @author jackord@kw.igs.net * The diffraction pattern produced when light from a helium-neon laser * strikes a circular (d=.05 mm) aperture and falls on a screen 1 m away * (one pixel representing 1 mm on the screen). * The plot is "overexposed" to make lower intensity variations visible. * The numerical calculation uses either a square grid with n=41 points along d, or a random array of n*n points spread over a square of side d. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class dif2 extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String b1s = "Square"; Button b1 = new Button(b1s); String b2s = "Random"; Button b2 = new Button(b2s); public void init() { add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); setBackground(new Color(211, 211, 211)); setFont(new Font("Helvetica", Font.PLAIN, 12)); } public void paint(Graphics g) { // Main Program if (first==0) { b1.setBounds(5, 5, 50, 20); b2.setBounds(5, 30, 50, 20); first=1; } double la=.633/1000; // Helium-neon red double a, d, dd, r0, xg, yg, z, zz, sr, si, t, sc; int c, n, nm, ns, x0, y0; double pi=Math.PI; g.setColor(Color.black); // Initial screen g.drawRect(0, 0, 240, 240); x0=120; y0=120; pi=Math.PI; d=.05; n=20; dd=d/(2*n+1); z=1000.; zz=z*z; nm=(int)(pi*(2*n+1)*(2*n+1)/4); sc=255000./nm/nm; ns=0; long tt=System.currentTimeMillis(); if (kk==1) { 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); sr=0; si=0; // Algorithm C for (int j=-n; j<=n; j=j+1) { // Aperture Y loop yg=j*dd; for (int i=-n; i<=n; i=i+1) { // Aperture X loop xg=i*dd; if ((xg*xg+yg*yg)<=(d*d/4)) { t=2*pi*(nx*xg+ny*yg)/r0/la; a=.5*(1+z/r0)*z/r0; sr=sr+a*Math.cos(t); si=si+a*Math.sin(t); } } } c=(int)((sr*sr+si*si)*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 intensity g.drawLine(x0-nx, y0+ny, x0-nx, y0+ny); // assuming symmetry g.drawLine(x0+nx, y0-ny, x0+nx, y0-ny); g.drawLine(x0-nx, y0-ny, x0-nx, y0-ny); } } g.setColor(Color.white); g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 5, 235); } if (kk==2) { // Random array double[] x=new double[nm+1]; double[] y=new double[nm+1]; while (ns255) { 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 intensity } } g.setColor(Color.white); g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 5, 235); } kk=0; } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } if (b2s.equals(tst)) { kk=2; } repaint(); } }