/* * dif3.java - revised 16 Oct 06 - width 481, height 241 * @author jackord@kw.igs.net * the diffraction grating: 1, 3, or 5 slits of width w=2 microns * with a separation between slits of w, 2w, or 3w (at a separation of w * the pattern is that of a single slit of width n*w) * the intensity pattern is that viewed on a screen 1 m from the grating * with one pixel representing 1 mm in the x direction * the wavelengths are red C, yellow D, green E, and blue F * a line plot (in magenta) of the analytic intensity expression for red light * is superimposed for the case of 3 slits spaced 2w apart */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class dif3 extends Applet implements ActionListener { int kk=0; // Declarations String bs = "Plot"; Button bb = new Button(bs); Choice chd=new Choice(); Choice chn=new Choice(); Color col[]={ Color.red, Color.yellow, Color.green, Color.blue }; public void init() { // Layout setBackground(new Color(211, 211, 211)); chd.setBackground(getBackground()); chn.setBackground(getBackground()); chd.addItem("d=w"); chd.addItem("d=2w"); chd.addItem("d=3w"); chn.addItem("n=1"); chn.addItem("n=3"); chn.addItem("n=5"); setLayout(new FlowLayout(FlowLayout.LEFT)); add(chd); add(chn); add(bb); bb.addActionListener(this); chd.select(1); chn.select(1); setFont(new Font("Helvetica", Font.PLAIN, 12)); } public void paint(Graphics g) { // Main Program double [] lambda=new double[4]; double w, xg, z, zz, a, r0, pi, p0, p1, sy, sr, si, t; int n, nd, nw, x0, x1, x2; n=1+2*chn.getSelectedIndex(); nd=1+chd.getSelectedIndex(); lambda[0]=.6563/1000; // Red C lambda[1]=.589/1000; // Yellow D lambda[2]=.5269/1000; // Green E lambda[3]=.4861/1000; // Blue F pi=Math.PI; w=.002; a=1; g.setColor(Color.black); g.drawRect(0, 0, 480, 240); if (kk>0) { nw=5; x0=240; z=1000.; zz=z*z; for (int j=-n/2; j<=n/2; j=j+1) { // Show slit pattern for (int i=-nw/2; i<=nw/2; i=i+1) { x1=x0+j*nd*nw+i; g.drawLine(x1, 10, x1, 30); } } nw=141; // Use 141 point model sy=200./(n*n*nw*nw); for (int nl=0; nl<=3; nl=nl+1) { // Wavelength loop g.setColor(col[nl]); g.drawLine(x0, 40, x0, 239); for (int nx=1; nx<=239; nx=nx+1) { // Screen loop r0=Math.sqrt(zz+nx*nx); sr=0; si=0; // Algorithm C for (int j=-n/2; j<=n/2; j=j+1) { // Slit number loop for (int i=-nw/2; i<=nw/2; i=i+1) { // Slit loop xg=(j*nd*nw+i)*w/nw; t=2*pi*nx*xg/r0/lambda[nl]; a=.5*(1+z/r0)*Math.sqrt(z/r0); // Angle and distance sr=sr+a*Math.cos(t); si=si+a*Math.sin(t);// factors } } x2=240-(int)(sy*(sr*sr+si*si)); if (x2<240) { g.drawLine(x0+nx, x2, x0+nx, 239); // Plot intensity g.drawLine(x0-nx, x2, x0-nx, 239); // assuming symmetry } } } if ((n==3)&&(nd==2)) { // If d=2w and 3 slits g.setColor(Color.magenta); x1=40; // Magenta curve for (int nx=1; nx<=239; nx=nx+1) { t=Math.atan(nx/z); // Analytic calculation p0=2*pi*w*Math.sin(t)/lambda[0]; p0=Math.sin(p0/2)/(p0/2); p1=2*pi*nd*w*Math.sin(t)/lambda[0]; p1=Math.sin(n*p1/2)/Math.sin(p1/2); x2=240-(int)(200.*p0*p0*p1*p1/n/n); if ((x1<240)||(x2<240)) { g.drawLine(x0+nx-1, x1, x0+nx, x2); // Analytic plot g.drawLine(x0-nx+1, x1, x0-nx, x2); } x1=x2; } } } kk=0; } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (bs.equals(tst)) { kk=1; } repaint(); } }