// SuperPosition1.java (J2SDK 1.4.2) // /************************************************************************* * * * Copyright (c) 1998, 2004 Greg W. Anderson * * * * This program is free software. You can copy, modify, or redistribute * * this software under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version, provided that both * * the above copyright notice appears in all copies and that this * * permission notice appear in any supporting documentation. * * * * This software is provided "as is" in the hope that it will be useful, * * but WITHOUT any expressed or implied warranty of merchantability or * * fitness for any particular purpose. Your milage may vary. * * See the GNU General Public License for more details. * * * *************************************************************************/ /* Modification History * Version Date Initials Change * 0.1 2/01/98 GWA Initial release * 0.1.5 1/09/99 GWA Added Phase constant * 0.2 10/09/04 GWA Updated to J2SDK 1.4.2 * * Abstract: * * This is a simple applet class to allow Students to * add two simple harmonic waves. Students can vary * the amplitude and angular frequency of two simple * harmonic waves. * * @author Greg Anderson * @version 0.2, October 2004 */ import java.awt.Graphics; import java.awt.*; import java.applet.*; public class Superposition1 extends java.applet.Applet { Superposition1Controls controls; Superposition1Banner banner; Label appletName = new Label ("The Virtual Physics Laboratory"); Label department = new Label ("Physics & Astronomy"); Label university = new Label ("Northeastern Illinois University"); Label author = new Label ("G. Anderson"); public void init(){ setLayout(new BorderLayout()); banner = new Superposition1Banner (2,2); banner.add("Center",department); banner.add(appletName); banner.add("Center",university); banner.add("South",author); add ("North",banner); Superposition1Canvas c = new Superposition1Canvas(); add("Center",c); add("South", controls = new Superposition1Controls(c)); } public void start(){ controls.setEnabled(true); } public void stop(){ controls.setEnabled(false); } public boolean handleEvent(Event e){ if (e.id == Event.WINDOW_DESTROY){ System.exit(0); } return false; } } class Superposition1Canvas extends Canvas { int halfWidth = 200; int spacing = 30; int shiftr = 25; int vcenter1 = 3*spacing; /* Vertical Center for graph 1 */ int vcenter2 = 6*spacing; /* Vertical Center for graph 1 */ int vcenter3 = 10*spacing; /* Vertical Center for graph 1 */ int t = 0; double x = 0; double phi1 = 0; /* phase constant */ double phi2 = 0; double k1 = 10.; /* k = w/v */ double k2 = 10.; /* k = 2pi/lambda */ double w1 = 1./2.; double w2 = 1./2.1; double f1; double f2; int A0 = 30; /* Amplitude */ int A1 = A0; /* Amplitude */ int A2 = A0; /* Amplitude */ int A3 = A0; /* Amplitude */ double fun1(double t) { return ( -A1*Math.sin(k1*x + w1*t + phi1) + vcenter1 ); } double fun2(double t) { return ( -A2*Math.sin(k2*x + w2*t + phi2) + vcenter2); } double fun(double t) { return ( fun1(t) + fun2(t) + vcenter3 - vcenter2 - vcenter1); } static String firstWave = "A1(t) = A0_1*sin(w1*t + phi1):"; static String secondWave = "A2(t) = A0_2*sin(w2*t + phi2):"; static String combinedWave = "Adding the waves above: A(t) = A1(t) + A2(t)"; public void paint(Graphics g) { setBackground(Color.white); Color darkmagenta = new Color(150,0,150); /* axes */ g.setColor(Color.gray); g.drawLine(shiftr,vcenter1,getSize().width,vcenter1); g.drawLine(shiftr,vcenter2,getSize().width,vcenter2); g.drawLine(shiftr,vcenter3,getSize().width,vcenter3); A3 = A1 + A2; g.drawLine(shiftr,vcenter1 -12*A1/10, shiftr,vcenter1 + 12*A1/10); g.drawLine(shiftr,vcenter2 -12*A2/10, shiftr,vcenter2 + 12*A2/10); g.drawLine(shiftr,vcenter3 -12*A3/10, shiftr,vcenter3 + 12*A3/10); /* axis labels */ g.setColor(Color.darkGray); g.drawString(" 20",0,vcenter1-15); g.drawString(" 0",0,vcenter1+ 5); g.drawString("-20",0,vcenter1+25); g.drawString("t",getSize().width-15,vcenter1+10); g.drawString(" 20",0,vcenter2-15); g.drawString(" 0",0,vcenter2+ 5); g.drawString("-20",0,vcenter2+25); g.drawString("t",getSize().width-15,vcenter2+10); g.drawString(" 20",0,vcenter3-15); g.drawString(" 0",0,vcenter3+ 5); g.drawString("-20",0,vcenter3+25); g.drawString("t",getSize().width-15,vcenter3+10); int sn; g.setColor(darkmagenta); for (int n = 0 ; n < getSize().width - shiftr; n++) { sn = n + shiftr; g.drawLine(sn,(int)fun1(n),sn+1,(int)fun1(n+1)); } g.setColor(Color.blue); for (int n = 0 ; n < getSize().width - shiftr ; n++) { sn = n + shiftr; g.drawLine(sn,(int)fun2(n),sn+1,(int)fun2(n+1)); } g.setColor(Color.red); for (int n = 0 ; n < getSize().width -shiftr; n++) { sn = n + shiftr; g.drawLine(sn,(int)fun(n),sn+1,(int)fun(n+1)); } /* Text Labels */ g.setColor(Color.black); g.drawString(firstWave,shiftr+5, vcenter1 - 12*spacing/10); // g.drawString("w1 = " + w1, halfWidth-20, vcenter1 - 12*spacing/10); // g.drawString("A0_1 = " + A1, halfWidth+70, vcenter1 - 12*spacing/10); g.drawString(secondWave,shiftr+5, vcenter2 - 12*spacing/10); // g.drawString("w2 = " + w2, halfWidth-20, 48*spacing/10); // g.drawString("A0_2 = " + A2, halfWidth+70, 48*spacing/10); g.drawString(combinedWave,shiftr+5, 78*spacing/10); } public void redraw(Double w1in, Double w2in, Double a1, Double a2, Double p1, Double p2){ w1 = w1in.doubleValue(); w2 = w2in.doubleValue(); A1 = a1.intValue(); A2 = a2.intValue(); phi1 = p1.doubleValue(); phi2 = p2.doubleValue(); repaint(); } } class Superposition1Controls extends Panel{ Button btemp; TextField w1; TextField w2; TextField a1; TextField a2; TextField p1; TextField p2; Superposition1Canvas canvas; public Superposition1Controls(Superposition1Canvas canvas){ setLayout(new GridLayout(3,5,5,5)); /* nr,nc, vg, hg */ this.canvas = canvas; Color darkmagenta = new Color(150,0,150); this.setForeground(Color.black); add (new Label("Angular Freq. :",Label.LEFT)); btemp = new Button("W1 = "); btemp.setForeground(darkmagenta); add (btemp); add (w1 = new TextField("0.5",4)); btemp = new Button("W2 = "); btemp.setForeground(Color.blue); add (btemp); add (w2 = new TextField("0.476",4)); add (new Label("Amplitudes:",Label.LEFT)); btemp = new Button("A0_1 = "); btemp.setForeground(darkmagenta); add (btemp); add (a1 = new TextField("30",4)); btemp = new Button("A0_2 = "); btemp.setForeground(Color.blue); add (btemp); add (a2 = new TextField("30",4)); add (new Label("Phase Constants:",Label.LEFT)); btemp = new Button("Phi_1 = "); btemp.setForeground(darkmagenta); add (btemp); add (p1 = new TextField("0",4)); btemp = new Button("Phi_2 = "); btemp.setForeground(Color.blue); add (btemp); add (p2 = new TextField("0",4)); } public boolean action(Event ev, Object arg){ if(ev.target instanceof Button // Redispla on Button click || ev.id == ev.END ){ // Redisplay if Return String label = (String)arg; canvas.redraw(Double.valueOf( w1.getText().trim() ), Double.valueOf( w2.getText().trim() ), Double.valueOf( a1.getText().trim() ), Double.valueOf( a2.getText().trim() ), Double.valueOf( p1.getText().trim() ), Double.valueOf( p2.getText().trim() ) ); return true; } return false; } } class Superposition1Banner extends Panel{ public Superposition1Banner(int n, int m){ Color darkMagenta = new Color(150,0,150); setBackground(darkMagenta); setForeground(Color.white); setLayout(new GridLayout(n,m,40,-5)); /* nr,nc, vg?, hg? */ } }