/* * mw1.java - revised 20 Apr 07 - width 481, height 241 * @author jackord@kw.igs.net * wave striking an interface between n[left]=1 and n[right]=2 * revision adds timing thread */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class mw1 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, a, r, t, n1, n2; 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; n1=1; n2=2; a=60; r=a*(n1-n2)/(n1+n2); t=a+r; // Amplitudes 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) { // Plot components bgr.setColor(Color.magenta); 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*n1*dx)); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } bgr.setColor(Color.red); x1=0; y1=120-(int)(r*Math.sin(i*dt)); for (int j=1; j<=80; j=j+1) { x2=3*j; y2=120-(int)(r*Math.sin(i*dt+j*n1*dx)); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } } if (kk==2) { // Plot resultant bgr.setColor(Color.blue); x1=0; y1=120-(int)((a+r)*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*n1*dx)+r*Math.sin(i*dt+j*n1*dx)); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } } bgr.setColor(Color.blue); x1=240; y1=120-(int)(t*Math.sin(i*dt)); for (int j=1; j<=80; j=j+1) { // Plot transmitted wave x2=240+3*j; y2=120-(int)(t*Math.sin(i*dt-j*n2*dx)); 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(); } }