/* * dif5.java - revised 24 Sep 06 - width 241, height 241 * @author jackord@kw.igs.net * The diffraction pattern for a circular aperture of diameter d viewed on a * screen 10*d from the aperture for various values of the wavelength. * The pattern is calculated directly by superposition of Huygens' wavelets. * The plot is overexposed 2X. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class dif5 extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String bs = "Run"; Button bb = new Button(bs); Choice ch=new Choice(); public void init() { setBackground(new Color(211, 211, 211)); ch.setBackground(getBackground()); setFont(new Font("Helvetica", Font.PLAIN, 12)); ch.addItem("la=d/5"); ch.addItem("la=d/10"); ch.addItem("la=d/20"); ch.addItem("la=d/40"); ch.addItem("la=d/80"); ch.addItem("la=d/120"); ch.addItem("la=d/160"); ch.addItem("la=d/200"); add(bb); add (ch); bb.addActionListener(this); ch.select(0); } public void paint(Graphics g) { // Main Program if (first==0) { bb.setBounds(5, 30, 30, 20); // Screen layout ch.setBounds(5, 5, 70, 5); first=1; } g.setColor(Color.black); g.drawRect(0, 0, 240, 240); g.drawArc(72, 72, 96, 96, 0, 360); double [] la=new double[8]; double pi, a, d, dd, r, r0, sr, si, t, xg, yg, z, zz; int [] c=new int[169]; int cm, n, nr, nd, xorg, yorg; pi=Math.PI; n=48; d=96.; dd=d/(2*n+1); z=10*d; zz=z*z; cm=0; xorg=120; yorg=120; la[0]=d/5; la[1]=d/10; la[2]=d/20; la[3]=d/40; la[4]=d/80; la[5]=d/120; la[6]=d/160; la[7]=d/200; nd=ch.getSelectedIndex(); // Wavelength index if (kk>0) { long tt=System.currentTimeMillis(); for (int nx=0; nx<=168; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx); sr=0; si=0; // Algorithm B 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)) { r=Math.sqrt(zz+(nx-xg)*(nx-xg)+yg*yg); t=2*pi*(r-r0)/la[nd]; 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); if (c[nx]>cm) { cm=c[nx]; } } for (int nx=0; nx<=168; nx=nx+1) { // Overexpose 2X c[nx]=(int)(510.*c[nx]/cm); if (c[nx]>255) { c[nx]=255; } } 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); // Plot intensity g.drawLine(xorg+nx, yorg+ny, xorg+nx, yorg+ny); g.drawLine(xorg-nx, yorg+ny, xorg-nx, yorg+ny); g.drawLine(xorg+nx, yorg-ny, xorg+nx, yorg-ny); g.drawLine(xorg-nx, yorg-ny, xorg-nx, yorg-ny); } } long tp=System.currentTimeMillis(); g.setColor(Color.white); g.drawArc(72, 72, 96, 96, 0, 360); 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 (bs.equals(tst)) { kk=1; } repaint(); } }