/* 1D random walk from Random starts. */
import java.awt.*;
import java.applet.Applet;
import java.text.DecimalFormat;
/** Walker performs a random walk. */
class Walker {
// Constants.
public static final Color WALKER_COLOR = Color.blue;
public static final int WALKER_DIAMETER = 50;
public int xpos ;
public int plus = 0 ;
public int minus = 0 ;
// Instance variables accessible to objects in the same package.
int ypos;
// Private instance variables.
private int h;
private Random p;
private int oldsleep =0;
/** Construct the Walker */
Walker (Random pen) {
xpos = WALKER_DIAMETER;
ypos = 240;
h = ypos;
p = pen;
}
/** The primary method that steps the walker. */
public void step() {
int dir;
if ((Math.random())> (double) 0.5){
dir=1;
plus++;
} else {
dir=-1;
minus--;
}
// System.out.println(" In step p.fall " +
// p.fall);
if(!p.fall){
xpos = xpos +
(dir*(p.size().width- 2*WALKER_DIAMETER)/p.NSTEPS);
}
// ypos = ypos + 10;
}
/** Set the Walker to where the user drags it. */
public void newPosition(int x, int y) {
xpos = x;
ypos = y;
}
/** Draw the Walker. */
public void draw(Graphics g) {
int radius = WALKER_DIAMETER/2;
int start = 0;
int step = 0;
g.setColor(WALKER_COLOR);
// g.drawString("Fall "+String.valueOf(p.fall),200,390);
if(!p.fall){
g.fillOval(xpos - radius, ypos - radius, WALKER_DIAMETER,
WALKER_DIAMETER); }
/* If falls off the end, show it */
else {
if(p.fallsteps>9){oldsleep=p.sleep;}
p.fallsteps--;
p.sleep=40;
if(p.fallsteps<1){p.fall=false;p.fallsteps=10;p.sleep=oldsleep;}
if(p.fall){
if(xpos+p.step
",370,10);
g.drawString("Set Bases ",25,30);
// g.drawString("start "+String.valueOf(p.start),100,300);
// g.drawString("step "+String.valueOf(p.step),100,310);
// g.drawString("end "+String.valueOf(p.end),100,320);
// g.drawString("xpos "+String.valueOf(xpos),100,330);
// g.drawString("rand "+String.valueOf((int)((Math.random())*(double)p.NSTEPS)),100,340);
// g.drawString("rand st "+String.valueOf(p.ranst),100,350);
// g.drawString("N trials "+String.valueOf(p.ntrial),100,360);
// g.drawString("Dist "+String.valueOf(p.dist),100,370);
// g.drawString("Distsum "+String.valueOf(p.distsum),100,380);
// g.drawString("Distav "+String.valueOf(p.distav),100,390);
for (int j=0; j 0){
newt=0;
// walker.xpos=start+(int)((Math.random())*(double)NSTEPS)*step;
if(!fall){
if(lrand){walker.xpos=start+ ((NSTEPS-1)/2)*step +step;}
else{
walker.xpos=start+(int)((Math.random())*(double)NSTEPS)*step +step;
}
// System.out.println(" Xpos on restart " +walker.xpos);
dist=-1;
}
}
/* If walker within bounds, step - else, set falling sequence */
if(!fall&&(walker.xpos>start+step&&walker.xpos-1){walker.step();}
myRepaint();
dist++;
t3.setText("Distance this trial "+String.valueOf(dist));
} else {
if(dist>0){distsum=distsum+dist;}
// distsum=distsum+dist;
dist=0;
if(!fall){ntrial++;}
distav = (double)distsum/(double)ntrial;
t2.setText("Average Steps per Trial "+twoDigits.format(distav));
t4.setText("Trial no "+String.valueOf(ntrial+1));
newt = 1;
fall = true;
myRepaint();
}
}
}
}
}
/** Stop the walk. */
public void stop() {
myThread.stop();
myThread = null;
}
/** Draw the walker. */
public void paint(Graphics g) {
walker.draw(g);
}
/** Reset the walker to this position as the user drags it around. */
public boolean mouseDrag(Event e, int x, int y) {
// walker.newPosition(x, y);
return false; // pass this event up the window hierarchy
}
/** Called when the mouse goes down. Stop random. */
public boolean mouseDown(Event e, int x, int y) {
freeze = true;
return false; // pass this event up the window hierarchy
}
public boolean action(Event e, Object o) {
if( e.target.equals(ntry)){
sntry=ntry.getSelectedItem();
changentry=true;
}
if( e.target == startButton){
lstart = !lstart;
if (lstart) {
startButton.setLabel("Stop");
} else {
startButton.setLabel("Start");
}
}
if( e.target == randButton){
lrand = !lrand;
if (lrand) {
randButton.setLabel("Middle");
} else {
randButton.setLabel("Random");
}
}
if( e.target == stepButton){
lstep = !lstep;
}
return false;
}
/** Called when the mouse goes up. Start random walk again. */
public boolean mouseUp(Event e, int x, int y) {
freeze = false;
// Do a total repaint after the user drags the walker
// to clean up the screen.
repaint();
return false; // pass this event up the window hierarchy
}
public boolean handleEvent(Event e) {
if(e.target instanceof Scrollbar){
if( e.target == upDown){
sleep=upDown.getValue();
}
return true;
}
return super.handleEvent(e);
}
/** Focus the repaint to the walker. */
private void myRepaint() {
int x;
int y;
int w;
int h;
int border = Walker.WALKER_DIAMETER * 2;
// y = Cord.CORD_PIVOT_Y;
// h = walker.ypos + Walker.WALKER_DIAMETER;
// x = (walker.xpos > Cord.CORD_PIVOT_X) ?
// Cord.CORD_PIVOT_X - border : walker.xpos - border;
// w = Math.abs(Cord.CORD_PIVOT_X - walker.xpos) + (border * 2);
y = 0;
h = 400;
x = 0;
w = 500;
repaint(x, y, w, h);
}
/** Provide a main routine so that we can run stand-alone. */
public static void main(String args[]) {
Random p = new Random();
p.init();
// Create a window hierarchy and hook it up.
Frame f = new Frame("Random");
f.add(p);
f.resize(500,400);
f.show();
// Start the app (this is done by the appletviewer or Web browser
// for us automatically, but it's not done if we're running the app
// from the command line).
p.start();
}
}