/* * Walker.java * Created on Feb 21, 2005 */ package random; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.Timer; import ptolemy.plot.Plot; /** * A random walker. */ public final class WalkerApplet extends JApplet implements ActionListener { private static final int NUM_POINTS = 1000; private static final int MAX_Y_DEV = 60; private static final int NUM_DATASETS = 10; private static final int STEPS_PER_INTERVAL = 100; private static final int INTERVAL_MS = 25; private LFSR rng = new LFSR(); private Plot plot = new Plot(); private int time = 0; private int[] xValues = new int[NUM_DATASETS]; private Timer timer = new Timer(INTERVAL_MS, this); public void init() { plot.setTitle("1D Random Walkers"); plot.setXLabel("t"); plot.setXRange(0, NUM_POINTS); plot.setYLabel("x"); plot.setYRange(-MAX_Y_DEV, MAX_Y_DEV); Container contentPane = getContentPane(); contentPane.add(plot); } public void start() { timer.start(); } public void actionPerformed(ActionEvent e) { // add an error bar if (time > 0 && time <= NUM_POINTS) { double barDev = 1.5 * Math.sqrt(time); plot.addPointWithErrorBars(0, time, xValues[0], -barDev, barDev, true); } // stop the timer if time is up if (time >= NUM_POINTS) timer.stop(); // plot some points for(int t = 0; t < STEPS_PER_INTERVAL && time < NUM_POINTS; t++, time++) { for(int i = 0; i < NUM_DATASETS; i++) { plot.addPoint(i, time, xValues[i], true); int delta = rng.nextBoolean() ? 1 : -1; xValues[i] += delta; } } // repaint the plot repaint(); } }