/* * df2.java - revised 4 Jan 10 - width 301, 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 1000 mm * away (125 mm if "z/8" is chosen). One pixel represents 1 mm on the screen. ) * The plot is "overexposed" to make lower intensity variations visible. * The numerical calculation uses either a square grid with 41 points along d (21 if "n/2" is chosen), or a random array of the same number of points. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class df2 extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String bbs="Plot"; Button bb = new Button(bbs); Checkbox ch1=new Checkbox("n/2"); Checkbox ch2=new Checkbox("z/8"); Checkbox ch3=new Checkbox("Rnd"); public void init() { add(bb); bb.addActionListener(this); add(ch1); add(ch2); add(ch3); setBackground(new Color(211, 211, 211)); ch1.setBackground(getBackground()); ch2.setBackground(getBackground()); ch3.setBackground(getBackground()); setFont(new Font("Helvetica", Font.PLAIN, 12)); } public void paint(Graphics g) { // Main Program bb.setBounds(10, 5, 40, 20); ch1.setBounds(10, 30, 40, 20); ch2.setBounds(10, 55, 40, 20); ch3.setBounds(10, 80, 40, 20); double la=.633/1000; // Helium-neon red double a, b, d, dd, r0, r2, xg, yg, z, zz, sr, si, t, sc; int c, cg, m, n, nm, ns, x0, y0; double pi=Math.PI; g.setColor(Color.black); // Initial screen g.drawRect(0, 0, 300, 240); if (ch1.getState()==true) { n=10; } else { n=20; } if (ch2.getState()==true) { z=125.; } else { z=1000.; } x0=180; y0=120; pi=Math.PI; d=.05; dd=d/(2*n+1); zz=z*z; r2=0.25*(2*n+1)*(2*n+1); nm=(int)(pi*r2); sc=255000./nm/nm; ns=0; long tt=System.currentTimeMillis(); if (kk>0) { if (ch3.getState()==false) { 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/r0/la; for (int j=-n; j<=n; j=j+1) { // Aperture Y loop yg=j*dd; m=(int)(Math.sqrt(r2-j*j)); for (int i=-m; i<=m; i=i+1) { // Aperture X loop xg=i*dd; t=b*(nx*xg+ny*yg); 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); } } } if (ch3.getState()==true) { double[] x=new double[nm+1]; double[] y=new double[nm+1]; sc=sc/2; 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.black); g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 2, 235); kk=0; } } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (bbs.equals(tst)) { kk=1; } repaint(); } }