/* * mw6.java - revised 23 Apr 07 - width 481, height 241 * @author jackord@kw.igs.net * wave incident (k) on a barrier (ik/n) wave transmitted (k) * (n can have any value, but is set equal to 4 in the example) * the barrier width equals the incident wavelength * plot shows ONE (real or imaginary) component of the wavefunction */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class mw6 extends Applet implements ActionListener, Runnable { int kk=0; int jd=40; double n=4; int first=0; // Declarations double a, r, a1, r1, a2, r2, t, dt, dx; double ph1, ph2, ph3, ph4; 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 pin(Graphics g) { double c, s, b, f; g.setColor(Color.black); g.drawRect(0, 0, 480, 240); g.drawLine(0, 120, 480, 120); g.drawLine(240, 0, 240, 240); g.drawLine(360, 0, 360, 240); if (first==0) { bim=createImage(481, 241); // Animation buffer bgr=bim.getGraphics(); first=1; } dx=Math.PI/20; dt=dx/2; b=2*Math.PI/n; c=(Math.exp(-b)+Math.exp(b))/2; // Complex arithmetic s=(Math.exp(-b)-Math.exp(b))/2; f=s*(n-1/n)/2; t=1/Math.sqrt(c*c+f*f); ph1=Math.atan2(f, c); r1=(n+1/n)*s*t/2; ph2=ph1-Math.PI/2; a2=Math.sqrt(1+n*n)*t/2; r2=a2; ph4=ph1+Math.atan2(n, 1); ph3=ph1-Math.atan2(n, 1); a1=60; r1=a1*r1; a2=a1*a2; r2=a1*r2; t=a1*t; // Amplitudes (finally) } public void paint(Graphics g) { int x1, y1, x2, y2; if (kk==0) { pin(g); } else { long tt=System.currentTimeMillis(); int del=15; for (int i=0; i<=320; 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(360, 0, 360, 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+ph2)); for (int j=1; j<=80; j=j+1) { r=r1*Math.sin(i*dt+j*dx+ph2); x2=3*j; y2=120-(int)r; bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } bgr.setColor(Color.orange); // Plot barrier region x1=240; y1=120-(int)(a2*Math.sin(i*dt+ph3)*Math.exp(-jd*dx/n)); for (int j=1; j<=jd; j=j+1) { a=a2*Math.sin(i*dt+ph3)*Math.exp((j-jd)*dx/n); 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+ph4)*Math.exp(jd*dx/n)); for (int j=1; j<=jd; j=j+1) { r=r2*Math.sin(i*dt+ph4)*Math.exp((jd-j)*dx/n); 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+ph2)); 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+ph2); 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 barrier region a=a2*Math.sin(i*dt+ph3)*Math.exp((j-jd)*dx/n); r=r2*Math.sin(i*dt+ph4)*Math.exp((jd-j)*dx/n); 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=240+3*jd; y1=120-(int)(t*Math.sin(i*dt+ph1)); for (int j=jd+1; j<=80; j=j+1) { x2=240+3*j; y2=120-(int)(t*Math.sin(i*dt-(j-jd)*dx+ph1)); 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(); } }