// This began from the book _Java in a Nutshell_ by David Flanagan. // but I doubt they would claim it, now. // Written by mf1i@virginia.edu and ajd2m@virginia.edu // // This applet shows two sine waves of different wavelengths interfering // as they travel at different velocities. There is a scrollbar and a // textbox which change the velocity of the longer wavelength. import java.applet.*; import java.awt.*; import java.lang.*; import java.util.*; public class NewtonianMountain extends Applet { // This is an animation derived from Canvas. It shows the waves. EarthCanvas globe; Button bStop,bGo; TextSlide slideField; // Shows the value of the velocity. GridBagLayout gbl = new GridBagLayout(); double minVal = -0.7, maxVal = 0.7; // Min and max values for velocity from scrollbar. AudioClip crash,fire,oops; // Initializtion called by the applet container. public void init() { Image earth; GridBagConstraints c = new GridBagConstraints(); // We use a very simple layout manager. this.setLayout(gbl); earth=getImage(getDocumentBase(),"newtshrunk.gif"); crash = getAudioClip(getDocumentBase(),"crash.au"); fire = getAudioClip(getDocumentBase(),"fire.au"); oops = getAudioClip(getDocumentBase(),"oops.au"); // This object is a homegrown class, but it can be added b/c it is also a canvas. globe = new EarthCanvas(earth,crash,fire,oops); c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.NORTH; c.gridwidth = 3; c.gridheight = 1; c.gridx = 0; c.gridy = 0; c.weightx = 3; c.weighty = 40; gbl.setConstraints(globe,c); this.add(globe); bStop = new Button("Stop"); bGo = new Button("Go"); bStop.disable(); bGo.disable(); c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.EAST; c.gridwidth = 1; c.gridheight = 1; c.gridx = 2; c.gridy = 1; c.weightx = 1; c.weighty = 1; gbl.setConstraints(bStop,c); this.add(bStop); c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.EAST; c.gridwidth = 1; c.gridheight = 1; c.gridx = 1; c.gridy = 1; c.weightx = 1; c.weighty = 1; gbl.setConstraints(bGo,c); this.add(bGo); // Create a text field and add it to the layout. slideField = new TextSlide(this,"Projectile Velocity: ",minVal,maxVal); // Set the value of the slider and scrollbar. slideField.setValue(-0.2); c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.SOUTH; c.gridwidth = 3; c.gridheight = 1; c.gridx = 0; c.gridy = 3; c.weightx = 3; c.weighty= 1; gbl.setConstraints(slideField,c); add(slideField); // You have to call validate to convince the canvas to adjust its layout. validate(); } // Start the animation. Called by applet's environment. The applet calls the sines thread. public void start() { globe.enable(); bGo.enable(); bStop.enable(); } // Stop it. public void stop() { globe.disable(); } public void changeParam(TextSlide which) { if (which == slideField) { globe.setVelocity(slideField.getValue()); } } public boolean action(Event e, Object arg) { if (e.target==bGo) { bGo.disable(); bStop.enable(); globe.fireButton(); } else if (e.target==bStop) { bGo.enable(); bStop.disable(); globe.stopButton(); } return true ; } // This method draws the background and text at its current position. // There is nothing to do b/c each component draws itself. public void paint(Graphics g) { ; } public void update(Graphics g) { ; } }