/* * mw3.java - revised 22 Apr 07 - width 481, height 241 * @author jackord@kw.igs.net * wave incident (n=1) on a film (n=2) on a substrate (n=1.5) * 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 mw3 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 c, x1, y1, x2, y2; // Declarations double dt, dx, n1, n, n2, a, r, a1, r1, a2, r2, t, phi; double pi=Math.PI; g.drawRect(0, 0, 480, 240); g.drawLine(0, 120, 480, 120); g.drawLine(240, 0, 240, 240); g.drawLine(285, 0, 285, 240); if (first==0) { bim=createImage(481, 241); // Animation buffer bgr=bim.getGraphics(); first=1; } c=2; dx=pi/20; dt=dx/c; phi=pi/2; n1=1; n=2; n2=1.5; r1=(n1*n2-n*n)/(n1*n2+n*n); t=n/n2*(1+r1); a2=t*(n+n2)/2/n; r2=t-a2; a1=60; r1=r1*a1; t=t*a1; a2=a2*a1; r2=r2*a1; long tt=System.currentTimeMillis(); int del=15; if (kk!=0) { 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(285, 0, 285, 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*n1*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)); for (int j=1; j<=80; j=j+1) { r=r1*Math.sin(i*dt+j*n1*dx); x2=3*j; y2=120-(int)r; bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } bgr.setColor(Color.yellow); // Plot n=2 region x1=240; y1=120-(int)(a2*Math.sin(i*dt+15*n*dx+phi)); for (int j=1; j<=15; j=j+1) { a=a2*Math.sin(i*dt-(j-15)*n*dx+pi/2); 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-15*n*dx+phi)); for (int j=1; j<=15; j=j+1) { r=r2*Math.sin(i*dt+(j-15)*n*dx+pi/2); 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+r1)*Math.sin(i*dt)); for (int j=1; j<=80; j=j+1) { a=a1*Math.sin(i*dt-j*n1*dx); r=r1*Math.sin(i*dt+j*n1*dx); x2=3*j; y2=120-(int)(a+r); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } for (int j=1; j<=15; j=j+1) { // Plot n=2 region a=a2*Math.sin(i*dt-(j-15)*n*dx+phi); r=r2*Math.sin(i*dt+(j-15)*n*dx+phi); 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=285; y1=120-(int)(t*Math.sin(i*dt+phi)); for (int j=16; j<=80; j=j+1) { x2=240+3*j; y2=120-(int)(t*Math.sin(i*dt-(j-15)*n2*dx+phi)); 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(); } }