/* * dif1.java - revised 27 Apr 07 - width 241, height 241 * @author jackord@kw.igs.net * The Fraunhofer diffraction pattern for light from a helium-neon laser * striking a circular (d=.05 mm) aperture and falling on a screen 1 m away * (one pixel representing 1 mm on the screen). * The pattern is calculated directly by superposition of Huygens' wavelets. * The plot is overexposed 1000X to make lower intensity variations visible. * Three algorithms can be selected: A and B are general algorithms, * C is a specific algorithm for Fraunhofer diffraction. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class dif1 extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String b1s = "Alg A"; Button b1 = new Button(b1s); String b2s = "Alg B"; Button b2 = new Button(b2s); String b3s = "Alg C"; Button b3 = new Button(b3s); public void init() { add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); add(b3); b3.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(2, 2, 40, 20); b2.setBounds(44, 2, 40, 20); b3.setBounds(86, 2, 40, 20); first=1; } g.setColor(Color.black); g.drawRect(0, 0, 240, 240); double la=.633/1000; // Helium-neon red double d, dd, xg, ygsq, z, zz, pi, a, r, r0, sr, si, t, sc; int [] c=new int[169]; int n, nm, nr, x0, y0; x0=120; y0=120; pi=Math.PI; d=.05; n=70; dd=d/(2*n+1); z=1000.; zz=z*z; nm=(int)(pi*(2*n+1)*(2*n+1)/4); sc=255000./nm/nm; long tt=System.currentTimeMillis(); if (kk==1) { // Algorithm A for (int nx=0; nx<=168; nx=nx+1) { // Screen X loop sr=0; si=0; for (int j=-n; j<=n; j=j+1) { // Aperture Y loop ygsq=j*dd*j*dd; for (int i=-n; i<=n; i=i+1) { // Aperture X loop xg=i*dd; if ((xg*xg+ygsq)<=(d*d/4)) { r=Math.sqrt(zz+(nx-xg)*(nx-xg)+ygsq); t=2*pi*r/la; a=.5*(1+z/r)*z/r; sr=sr+a*Math.cos(t); si=si+a*Math.sin(t); } } } c[nx]=(int)((sr*sr+si*si)*sc); if (c[nx]>255) { c[nx]=255; } } } if (kk==2) { // Algorithm B for (int nx=0; nx<=168; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx); sr=0; si=0; for (int j=-n; j<=n; j=j+1) { // Aperture Y loop ygsq=j*dd*j*dd; for (int i=-n; i<=n; i=i+1) { // Aperture X loop xg=i*dd; if ((xg*xg+ygsq)<=(d*d/4)) { r=Math.sqrt(zz+(nx-xg)*(nx-xg)+ygsq); t=2*pi*(r-r0)/la; a=.5*(1+z/r)*z/r; sr=sr+a*Math.cos(t); si=si+a*Math.sin(t); } } } c[nx]=(int)((sr*sr+si*si)*sc); if (c[nx]>255) { c[nx]=255; } } } if (kk==3) { // Algorithm C for (int nx=0; nx<=168; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx); sr=0; si=0; for (int j=-n; j<=n; j=j+1) { // Aperture Y loop ygsq=j*dd*j*dd; for (int i=-n; i<=n; i=i+1) { // Aperture X loop xg=i*dd; if ((xg*xg+ygsq)<=(d*d/4)) { t=2*pi*nx*xg/r0/la; a=.5*(1+z/r0)*z/r0; sr=sr+a*Math.cos(t); si=si+a*Math.sin(t); } } } c[nx]=(int)((sr*sr+si*si)*sc); if (c[nx]>255) { c[nx]=255; } } } if (kk>0) { long tc=System.currentTimeMillis(); for (int ny=0; ny<=119; ny=ny+1) { // Screen Y loop for (int nx=0; nx<=119; nx=nx+1) { // Screen X loop nr=(int)(Math.sqrt(nx*nx+ny*ny)); Color col=new Color(c[nr], 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); } } long tp=System.currentTimeMillis(); g.setColor(Color.white); g.drawString("Calculate "+(tc-tt)+" ms Display "+(tp-tc)+" ms", 5, 235); kk=0; } } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } if (b2s.equals(tst)) { kk=2; } if (b3s.equals(tst)) { kk=3; } repaint(); } }