/* * dif6java - revised 24 Sep 06 - width 241, height 241 * @author jackord@kw.igs.net * The intensity graph for a circular aperture of diameter d or a slit * width 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. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class dif6 extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String bs1 = "Circle"; Button bb1 = new Button(bs1); String bs2 = "Slit"; Button bb2 = new Button(bs2); 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(bb1); add(bb2); add (ch); bb1.addActionListener(this); bb2.addActionListener(this); ch.select(0); } public void paint(Graphics g) { // Main Program if (first==0) { // Screen Layout bb1.setBounds(4, 28, 40, 20); bb2.setBounds(4, 52, 40, 20); ch.setBounds(4, 4, 80, 4); first=1; } double [] la=new double[8]; double [] c =new double[120]; double d, dd, xg, yg, z, zz, pi, a, r, r0, sr, si, t, cm; int n, nd, x0, x2; pi=Math.PI; d=96.; n=70; dd=d/(2*n+1); z=10*d; zz=z*z; a=1; cm=0; x0=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 g.setColor(Color.black); g.drawRect(0, 0, 240, 240); g.drawLine(72, 0, 72, 240); g.drawLine(168, 0, 168, 240); if (kk>0) { g.setColor(Color.red); for (int nx=0; nx<=119; nx=nx+1) { // Screen loop r0=Math.sqrt(zz+nx*nx); sr=0; si=0; // Algorithm B if (kk==1) { for (int j=-n; j<=n; j=j+1) { // Circle Y loop yg=j*dd; for (int i=-n; i<=n; i=i+1) { // Circle 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;// Angle and distance factors sr=sr+a*Math.cos(t); si=si+a*Math.sin(t); } } } } if (kk==2) { for (int j=-n; j<=n; j=j+1) { // Slit loop xg=j*dd; r=Math.sqrt(zz+(nx-xg)*(nx-xg)); t=2*pi*(r-r0)/la[nd]; a=.5*(1+z/r)*Math.sqrt(z/r); // Angle and distance factors sr=sr+a*Math.cos(t); si=si+a*Math.sin(t); } } c[nx]=sr*sr+si*si; if (c[nx]>cm) { cm=c[nx]; } } for (int nx=0; nx<=119; nx=nx+1) { x2=240-(int)(c[nx]/cm*239); if ((nx!=48)&&(x2<240)) { g.drawLine(x0+nx, x2, x0+nx, 239); // Plot intensity g.drawLine(x0-nx, x2, x0-nx, 239); // assuming symmetry } } } kk=0; } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (bs1.equals(tst)) { kk=1; } if (bs2.equals(tst)) { kk=2; } repaint(); } }