/* * mw2.java - revised 22 Apr 07 - width 481, height 241 * @author jackord@kw.igs.net * wave striking an absorbing medium, n(left)=1 and n(right)=2-.2i */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class mw2 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 n, k, ph1, ph2, dt, dx, a, r, t; g.drawRect(0, 0, 480, 240); g.drawLine(0, 120, 480, 120); g.drawLine(240, 0, 240, 240); if (first==0) { bim=createImage(481, 241); // Animation buffer bgr=bim.getGraphics(); first=1; } c=2; dx=Math.PI/20; dt=dx/c; a=60; n=2.; k=.2; r=a*Math.sqrt((1-n)*(1-n)+k*k)/Math.sqrt((1+n)*(1+n)+k*k); ph1=Math.atan2(2*k, 1-n*n-k*k); t=2*a/Math.sqrt((1+n)*(1+n)+k*k); ph2=Math.atan2(k, 1+n); 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, 480, 240); bgr.setColor(Color.black); bgr.drawRect(0, 0, 480, 240); bgr.drawLine(0, 120, 480, 120); bgr.drawLine( 240, 0, 240, 240); if (kk==1) { bgr.setColor(Color.magenta); // Plot incident wave x1=0; y1=120-(int)(a*Math.sin(i*dt)); for (int j=1; j<=80; j=j+1) { x2=3*j; y2=120-(int)(a*Math.sin(i*dt-j*dx)); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } bgr.setColor(Color.red); // Plot reflected wave x1=0; y1=120-(int)(r*Math.sin(i*dt-80*dx+ph1)); for (int j=1; j<=80; j=j+1) { x2=3*j; y2=120-(int)(r*Math.sin(i*dt+j*dx+ph1)); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } } if (kk==2) { bgr.setColor(Color.blue); // Plot resultant x1=0; y1=120-(int)(a*Math.sin(i*dt)+r*Math.sin(i*dt+ph1)); for (int j=1; j<=80; j=j+1) { x2=3*j; y2=120-(int)(a*Math.sin(i*dt-j*dx)+r*Math.sin(i*dt+j*dx+ph1)); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } } bgr.setColor(Color.blue); // Plot transmitted wave x1=240; y1=120-(int)(t*Math.sin(i*dt+ph2)); for (int j=1; j<=80; j=j+1) { x2=240+3*j; y2=120-(int)(t*Math.exp(-j*k*dx)*Math.sin(i*dt-j*n*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(); } }