/* * df1.java - revised 28 Jan 10 - width 481, height 241 * @author jackord@kw.igs.net * The diffraction pattern and intensity graph 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 overexposed 2X. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class df1 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("d/10"); ch.addItem("d/20"); ch.addItem("d/40"); ch.addItem("d/80"); ch.addItem("d/120"); ch.addItem("d/160"); setLayout(new FlowLayout(FlowLayout.LEFT)); add(ch); add (bb); bb.addActionListener(this); ch.select(0); } public void paint(Graphics g) { // Main Program g.setColor(Color.black); g.drawRect(0, 0, 480, 240); g.drawLine(240, 0, 240, 240); g.drawLine(312, 0, 312, 240); g.drawLine(408, 0, 408, 240); g.drawArc(72, 72, 96, 96, 0, 360); double [] la=new double[6]; double pi, a, d, dd, r, r0, sr, si, t, xg, ygsq, z, zz, cm; double [] c=new double[169]; int cc, n, na, nr, nd, xorg, yorg, x0, x2; pi=Math.PI; n=70; d=96.; dd=d/(2*n+1); z=10*d; zz=z*z; cm=0; xorg=120; yorg=120; x0=360; la[0]=d/10; la[1]=d/20; la[2]=d/40; la[3]=d/80; la[4]=d/120; la[5]=d/160; 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; for (int j=0; j<=n; j=j+1) { // Aperture Y loop ygsq=j*j*dd*dd; na=(int)(Math.sqrt(n*n-j*j)); for (int i=-na; i<=na; 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[nd]; a=(1+z/r)*z/r; if (j==0) { a=a/2; } 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 ny=0; ny<=119; ny=ny+1) { // Pattern Y loop for (int nx=0; nx<=119; nx=nx+1) { // Pattern X loop nr=(int)(Math.sqrt(nx*nx+ny*ny)); cc=(int)(510.*c[nr]/cm); if (cc>255) { cc=255; } Color col=new Color(cc, 0, 0); g.setColor(col); // Plot pattern 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); } } g.setColor(Color.white); g.drawArc(72, 72, 96, 96, 0, 360); g.setColor(Color.red); for (int nx=0; nx<=119; nx=nx+1) { // Intensity X loop x0=360; x2=240-(int)(239*c[nx]/cm); if ((nx!=48)&&(x2<240)) { g.drawLine(x0+nx, x2, x0+nx, 239); // Plot intensity g.drawLine(x0-nx, x2, x0-nx, 239); } } } kk=0; } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (bs.equals(tst)) { kk=1; } repaint(); } }