import java.awt.*; /** Program: Advert11 Purpose: @author: Paul Garrett, garrett@math.umn.edu @version: 11, Sun Jan 26 15:15:53 CST 1997 */ public class Advert11 extends java.applet.Applet implements Runnable { final Color bgColor = Color.darkGray; final Color fgColor = Color.white; final int totalTime = 300; final int waveSpeed = 1; final int numberIntervals = 80; final double theFrac = 0.65; // = wave height/half theHeight final double m = 0.005; // change: edit final double b = 0.005; // ad hoc Thread timerThread; int theTime; boolean queryMouseDown, queryRunMovie; // int theIndex; // of controls being affected by mouse double[][] theWave; // the native numbers /* * Really do want to keep the whole picture, in case we generalize * to the 2-D-space case, in which one would want to have * varying views of the same native data. * */ int[][] theFrames; // to screen: [time][space] int[] xs; Image offScreenImage; Graphics offScreenGraphics; Label theLabel, secondLabel; // the equation int theWidth, theHeight; // double maxValue, minValue; // u_tt - u_xx + m^2u + bu^3 =0 is Klein-Gordon... // and \int_x (1/2)(u_t^2 + u_x^2) + m^2 u^2/2 + b u^4/4 dx is conserved public void init() { initConstants(); initWave(); initFrame(); computeWave(); computeFrames(); repaint(); } final void initConstants() { theTime=0; theLabel = new Label("non-linear Klein-Gordon wave equation"); secondLabel = new Label("u_tt - u_xx + (.005)^2 u + (0.005) u^3 = 0"); add(theLabel); add(secondLabel); queryMouseDown = false; queryRunMovie = false; theHeight = this.size().height; // to save time... theWidth = this.size().width; offScreenImage = createImage(theWidth, theHeight); offScreenGraphics = offScreenImage.getGraphics(); // minValue = maxValue = 0.0; // b = 0; // very temporary ................................******************* initXs(); // after get theWidth, theHeight theWave = new double[totalTime+1][numberIntervals + 1]; theFrames = new int[totalTime+1][numberIntervals + 1]; } final void initWave() { int w = numberIntervals/4; for (int i=1; i < 2 * w; i++) { theWave[0][i] = Math.exp(- ((float)((i-w)*(i-w)) * .02 ) ); } } /* * So the initial wave has values |"|<=1 ... : ad hoc * * */ public void paint(Graphics g) { drawOneFrame(); g.drawImage(offScreenImage, 0, 0, this); } final void drawOneFrame() { offScreenGraphics.setColor(bgColor); offScreenGraphics.fillRect(0, 0, theWidth, theHeight); offScreenGraphics.setColor(fgColor); for (int i=0; i=minValue) { } else {minValue = theWave[t][x]; } } else {maxValue = theWave[t][x]; } } } } * Gotta compute the first frame() * just to have a picture on the screen... * prior to attempting normalization! * * */ final void initFrame() { // initial max is 1... for (int x=0; x<= numberIntervals; x++) { theFrames[0][x] = (int) ((float)(theHeight/2) - ((float)(theHeight/2)) * theFrac * theWave[0][x]); } } final void computeFrames() { // crude initial version for (int t=0; t<=totalTime; t++) { for (int x=0; x<=numberIntervals; x++) { theFrames[t][x] = (int)( ((float) theHeight/2) * (1.0 - theFrac * theWave[t][x]) ); } } } public void start() { if (timerThread == null) { timerThread = new Thread(this); timerThread.start(); } repaint(); } public void stop() { if (timerThread != null) { timerThread.stop(); try { timerThread.join(); } catch (InterruptedException e) { } timerThread = null; } } void pause(int time) { try { Thread.sleep(time); } catch ( InterruptedException e) { } } public void run() { while (true) { repaint(); theTime= (theTime +1) % totalTime; pause(50); } } public void update(Graphics g) { paint(g); } } // the end of the class