/* * dif4.java - revised 31 Jan 07 - width 241, height 241 * @author jackord@kw.igs.net * The diffraction pattern for light from a helium-neon laser * striking a rectangular (w=0.05 mm, h=.033 mm) or equilateral triangular * aperture (side=.05 mm) and falling on a screen 1 m away * (one pixel representing 1 mm on the screen). * The plot is "overexposed" to make lower intensity variations visible. * When "AlgD" is selected, Algorithm D replaces Algorithm C for rectangle * When "la/6" is selected, the wavelength is divided by 6. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class dif4 extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String b1s = "Rect"; Button b1 = new Button(b1s); String b2s = "Tri"; Button b2 = new Button(b2s); Checkbox cha=new Checkbox("AlgD"); Checkbox chb=new Checkbox("la/6"); public void init() { add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); add(cha); add(chb); setBackground(new Color(211, 211, 211)); cha.setBackground(getBackground()); chb.setBackground(getBackground()); 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); cha.setBounds(5, 55, 50, 20); chb.setBounds(5, 80, 50, 20); first=1; } 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, nc, nm, nr, x0, y0; double pi=Math.PI; g.setColor(Color.black); // Initial screen g.drawRect(0, 0, 240, 240); n=20; nc=13; nr=0; d=.05; dd=d/(2*n+1); x0=120; y0=120; z=1000.; zz=z*z; if (chb.getState()==true) { la=la/6; } // lambda/6 long tt=System.currentTimeMillis(); if (kk==1) { // Rectangle nm=(2*n+1)*(2*nc+1); sc=255000./nm/nm; 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; a=.5*(1+z/r0)*z/r0; b=2*pi*dd/r0/la; if (cha.getState()==true) { // Algorithm D for (int i=-n; i<=n; i=i+1) { // Aperture X loop t=b*(i*nx-nc*ny); sr=sr+Math.cos(t); si=si+Math.sin(t); } qr=sr; qi=si; for (int i=1; i<=2*nc; i=i+1) { // Aperture Y loop rr=Math.cos(i*b*ny); ri=Math.sin(i*b*ny); qr=qr+rr*sr-ri*si; qi=qi+rr*si+ri*sr; } c=(int)((qr*qr+qi*qi)*a*a*sc); } else { // Algorithm C for (int j=-nc; j<=nc; j=j+1) { // Aperture Y loop for (int i=-n; i<=n; i=i+1) { // Aperture X loop t=b*(i*nx+j*ny); sr=sr+Math.cos(t); si=si+Math.sin(t); } } 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 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); // Show g.drawRect(x0-n, y0-nc, 2*n, 2*nc); // aperture orientation g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 5, 235); } if (kk==2) { // Triangle nm=(2*n+1)*(n+1); sc=255000./nm/nm; // Algorithm C for (int ny=-119; 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; nr=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=-2*n/3; j<=4*n/3; j=j+1) { // Aperture Y loop for (int i=-n; i<=n-nr; i=i+1) { // Aperture X loop t=b*nx*(i+.5*nr)+by*ny*j; sr=sr+Math.cos(t); si=si+Math.sin(t); } nr=nr+1; } 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 intensity g.drawLine(x0-nx, y0-ny, x0-nx, y0-ny); // assming x symmetry } } g.setColor(Color.white); // Show g.drawLine(x0-n, y0+12, x0+n, y0+12); // aperture orientation g.drawLine(x0+n, y0+12, x0, y0-24); g.drawLine(x0, y0-24, x0-n, y0+12); 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(); } }