Home
|
Course Outline
|
Lectures
|
Homework
|
Files
// RandomWalk2.java
import comphys.*;
import comphys.graphics.*;
public class RandomWalk2 extends Animation {
int nwalkers = 100; // number of walkers
int N; // number of steps
int[] xWalker, yWalker; // walkers' current positions
void initial () {
if (xWalker == null || xWalker.length < nwalkers) {
xWalker = new int[nwalkers];
yWalker = new int[nwalkers];
}
for (int n = 0; n < nwalkers; n++)
xWalker[n] = yWalker[n] = 0;
N = 0;
}
void move () {
for (int n = 0; n < nwalkers; n++)
choice(n);
++N;
}
void choice (int n) {
double p = Math.random();
if (p <= 0.25) {
++xWalker[n];
} else if (p <= 0.5) {
--xWalker[n];
} else if (p <= 0.75) {
--yWalker[n];
} else {
++yWalker[n];
}
}
double rMax = 20;
boolean needToClear = true;
class Movie extends Plot {
public void paint () {
if (needToClear) {
clear();
needToClear = false;
}
setColor("gray");
drawAxes(-rMax, rMax, -rMax, rMax);
setColor("green");
for (int n = 0; n < nwalkers; n++)
boxArea(xWalker[n] - 0.5, xWalker[n] + 0.5,
yWalker[n] - 0.5, yWalker[n] + 0.5);
plotOverlay();
setPaintMode();
setColor("red");
for (int n = 0; n < nwalkers; n++)
boxArea(xWalker[n] - 0.5, xWalker[n] + 0.5,
yWalker[n] - 0.5, yWalker[n] + 0.5);
setColor("blue");
plotStringCenter("Step number = " + N, 0, -1.15 * rMax);
}
}
Movie movie;
Reader nwalkersReader, rMaxReader;
public void init () {
initial();
add(movie = new Movie());
add(nwalkersReader = new Reader("n walkers = ", nwalkers));
add(rMaxReader = new Reader("r_max = ", rMax, 4));
addControlPanel();
}
public void step () {
move();
movie.repaint();
}
public void reset () {
nwalkers = nwalkersReader.readInt();
rMax = rMaxReader.readDouble();
initial();
needToClear = true;
movie.repaint();
}
public static void main (String[] args) {
RandomWalk2 rw2 = new RandomWalk2();
rw2.frame("Random Walk in 2 Dimensions", 500, 600);
}
}
This program uses two comphys.graphics classes:
add(rMaxReader = new Reader("r_max = ", rMax, 4));
adds a text input field with label "r_max = "
associated with the double
rMax which will be displayed with 4 digits precision,
and also add a built-in animation control panel by calling the method
addControlPanel();
rMax = rMaxReader.readDouble();
class Movie extends Plot {
public void paint () {
}
}
Movie movie;
Plot requires you to define the method public void paint () in
which you place all your plotting commands. Note the following features
of Plot:
drawAxes(-rMax, rMax, -rMax, rMax);sets up a coordinate system with the origin at the center of the default 400 x 400 pixel canvas, and x_min = -rMax, x_max = rMax, y_min = -rMax, y_max = rMax given as arguments to the method. Plot methods such as boxArea (which fills a rectangular box with the current foreground color) and plotStringCenter (which draws a string centered at the given coordinate values, automatically use this user-specified coordinate system: thus you do not have to translate from physical coordinates to pixel coordinates!
plotOverlay();All plotting commands after this invocation are "temporary", in contrast with plotting commands before this invocation which persist from frame to frame.
Note, finally, that an Animation can be run as an applet in a Web browser or as an application from the command line. When invoked as an applet, the appletviewer or Web browser initializes your program by invoking the public void init () method. To run the program as an application from the command line, you need to define a main method, for example:
public static void main (String[] args) {
RandomWalk2 rw2 = new RandomWalk2();
rw2.frame("Random Walk in 2 Dimensions", 500, 600);
}
Inside the main method you need to do two things: