/* * mw4.java - revised 23 Apr 07 - width 481, height 241 * @author jackord@kw.igs.net * wave incident (n=1) on a film (n=5/3) on a substrate (n=2-.2i) * the film thickness is 3/4 of the wavelength (in the film) */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class mw4 extends Applet implements ActionListener, Runnable { int kk=0; int first=0; // Declarations String b1s="Component"; Button b1=new Button(b1s); String b2s="Resultant"; Button b2=new Button(b2s); Image bim; Graphics bgr; Thread tmr; public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); setBackground(new Color(211, 211, 211)); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void start() { if (kk>0) { if (tmr==null) { tmr=new Thread(this); } tmr.start(); } } public void stop() { tmr=null; } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); repaint(); } public void paint(Graphics g) { int jd, c, x1, y1, x2, y2; // Declarations double a, r, a1, r1, r1r, r1i, ip, rp, a2, r2; double ph1, ph2, ph3, ph4, phn, phd; double t, dt, dx, n, n2, k2, num, den; g.drawRect(0, 0, 480, 240); g.drawLine(0, 120, 480, 120); g.drawLine(240, 0, 240, 240); g.drawLine(294, 0, 294, 240); if (first==0) { bim=createImage(481, 241); // Animation buffer bgr=bim.getGraphics(); first=1; } c=2; dx=Math.PI/20; dt=dx/c; jd=18; a1=60; n=5./3.; n2=2; k2=.2; // Refractive indices rp=n2-n*n; ip=-k2; // Complex arithmetic num=Math.sqrt(rp*rp+ip*ip); phn=Math.atan2(ip, rp); rp=n2+n*n; den=Math.sqrt(rp*rp+ip*ip); phd=Math.atan2(ip, rp); r1=num/den; ph1=phn-phd; r1r=r1*Math.cos(ph1); r1i=r1*Math.sin(ph1); num=Math.sqrt((1+r1r)*(1+r1r)+r1i*r1i); phn=Math.atan2(r1i, 1+r1r); // (ugh!) den=Math.sqrt(n2*n2+k2*k2); phd=Math.atan2(-k2, n2); t=n*num/den; ph2=Math.PI/2+phn-phd; a2=t/2/n*Math.sqrt((n+n2)*(n+n2)+k2*k2); ph3=ph2+Math.atan2(-k2, n+n2); r2=t/2/n*Math.sqrt((n-n2)*(n-n2)+k2*k2); ph4=ph2+Math.atan2(k2, n-n2); r1=a1*r1; a2=a1*a2; r2=a1*r2; t=a1*t; // Amplitudes (finally) if (kk!=0) { long tt=System.currentTimeMillis(); int del=15; for (int i=0; i<=160*c; i=i+1) { bgr.setColor(getBackground()); // Clear plot buffer bgr.fillRect(0, 0, 481, 241); bgr.setColor(Color.black); bgr.drawRect(0, 0, 480, 240); bgr.drawLine(0, 120, 480, 120); bgr.drawLine(240, 0, 240, 240); bgr.drawLine(294, 0, 294, 240); if (kk==1) { bgr.setColor(Color.magenta); // Plot incident wave x1=0; y1=120-(int)(a1*Math.sin(i*dt)); for (int j=1; j<=80; j=j+1) { a=a1*Math.sin(i*dt-j*dx); x2=3*j; y2=120-(int)a; bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } bgr.setColor(Color.red); // Plot reflected wave x1=0; y1=120-(int)(r1*Math.sin(i*dt+ph1)); for (int j=1; j<=80; j=j+1) { r=r1*Math.sin(i*dt+j*dx+ph1); x2=3*j; y2=120-(int)r; bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } bgr.setColor(Color.orange); // Plot n=5/3 region x1=240; y1=120-(int)(a2*Math.sin(i*dt+n*jd*dx+ph3)); for (int j=1; j<=jd; j=j+1) { a=a2*Math.sin(i*dt-n*(j-jd)*dx+ph3); x2=240+3*j; y2=120-(int)a; bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } bgr.setColor(Color.green); x1=240; y1=120-(int)(r2*Math.sin(i*dt-n*jd*dx+ph4)); for (int j=1; j<=jd; j=j+1) { r=r2*Math.sin(i*dt+n*(j-jd)*dx+ph4); x2=240+3*j; y2=120-(int)r; bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } } if (kk==2) { // Plot resultant wave bgr.setColor(Color.blue); x1=0; y1=120-(int)(a1*Math.sin(i*dt)+r1*Math.sin(i*dt+ph1)); for (int j=1; j<=80; j=j+1) { a=a1*Math.sin(i*dt-j*dx); r=r1*Math.sin(i*dt+j*dx+ph1); x2=3*j; y2=120-(int)(a+r); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } for (int j=1; j<=jd; j=j+1) { // Plot n=5/3 region a=a2*Math.sin(i*dt-n*(j-jd)*dx+ph3); r=r2*Math.sin(i*dt+n*(j-jd)*dx+ph4); x2=240+3*j; y2=120-(int)(a+r); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } } bgr.setColor(Color.blue); // Plot transmitted wave x1=294; y1=120-(int)(t*Math.sin(i*dt+ph2)); for (int j=jd+1; j<=80; j=j+1) { x2=240+3*j; y2=120-(int)(t*Math.exp(-k2*(j-jd)*dx)*Math.sin(i*dt-n2*(j-jd)*dx+ph2)); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } g.drawImage(bim, 0, 0, null); // Show plot buffer while (System.currentTimeMillis()-tt<30*i) { try { Thread.sleep(del); } catch (InterruptedException e) { stop(); } } } } kk=0; stop(); } public void actionPerformed(ActionEvent e) { String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } if (b2s.equals(tst)) { kk=2; } start(); } }