import java.applet.Applet; import java.util.Random; import java.awt.*; public class RANDrw2d extends Applet { Panel mypanel = new Panel(); RW2 mycanvas = new RW2(this); Label mylabel = new Label("#steps:",Label.RIGHT); Button redrawButton = new Button("New Walk"); Button contButton = new Button("Cont"); Button stopButton = new Button("Stop"); GridBagLayout mylayout = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); TextField mytextfield = new TextField(6); Checkbox mycheckbox0 = new Checkbox("Wrap",null,true); Checkbox mycheckbox1 = new Checkbox("Random Start",null,true); public void init() { // First of all, construct the GUI setLayout(mylayout); setFont(new Font("Helvetica", Font.PLAIN, 10)); mycanvas.resize(150,150); c.fill = GridBagConstraints.VERTICAL; c.gridheight = 10; c.gridwidth = 10; c.gridx = 0; c.gridy = 0; mylayout.setConstraints(mycanvas,c); add(mycanvas); c.fill = GridBagConstraints.HORIZONTAL; c.gridheight = 1; c.gridwidth = 5; c.gridx = 11; c.gridy = 0; c.insets = new Insets(0,2,0,0); mylayout.setConstraints(mycheckbox0,c); add(mycheckbox0); c.gridy = 1; mylayout.setConstraints(mycheckbox1,c); add(mycheckbox1); c.gridy = 2; mylayout.setConstraints(mypanel,c); mypanel.add(mylabel); mytextfield.setText("1000"); mycanvas.nbpoints = 1000; mypanel.add(mytextfield); add(mypanel); c.gridy = 3; mylayout.setConstraints(redrawButton,c); add(redrawButton); c.gridy = 4; mylayout.setConstraints(stopButton,c); add(stopButton); c.gridy = 5; mylayout.setConstraints(contButton,c); add(contButton); show(); validate(); } public boolean action(Event e, Object arg) { Object target = e.target; String str; if(target==this) { repaint(); return false; } if(target==mytextfield) { // Get new value of nbpoints str = mytextfield.getText(); mycanvas.nbpoints = (Integer.parseInt(str)); return true; } if(target==redrawButton) { // Start a new walk mycanvas.stop(); mycanvas.currentpoint=0; str = mytextfield.getText(); mycanvas.nbpoints = (Integer.parseInt(str)); mycanvas.frozen = false; mycanvas.start(); return true; } if(target==contButton) { // stop and restart where we were mycanvas.stop(); if(mycanvas.currentpoint>=mycanvas.nbpoints) mycanvas.currentpoint=1; mycanvas.frozen = false; mycanvas.start(); return true; } if(target==stopButton) { // stop walking mycanvas.stop(); return true; } // change some parameters... if(target==mycheckbox0) { mycanvas.wrap = !mycanvas.wrap; return true; } if(target==mycheckbox1) { mycanvas.randstart = !mycanvas.randstart; return true; } return false; } } // Here is the part concerning the random walk class RW2 extends Canvas implements Runnable { // Walk variables int nbpoints = 0; int currentpoint = 0; boolean randstart = true; boolean wrap = true; int x0,y0; //starting points int ix,iy,nx,ny; //current points int mx,my; //max values // Image buffers Dimension offDimension; Image offImage,offImage1; Graphics offGraphics,offGraphics1; // thread controls boolean frozen = true; Thread drawThread = null; // container from where to get events Container control; // Random generator Random rand = new Random(45682); public RW2(Container control) { super(); this.control = control; } public void init() { Graphics g = getGraphics(); // Draw something in canvas g.setColor(Color.pink); g.fillRect(1,1,size().width - 2, size().height - 2); g.setColor(Color.black); g.drawRect(0, 0, size().width - 1, size().height - 1); } public boolean mouseDown(Event e, int x, int y) { Graphics g = getGraphics(); Object target = e.target; // New starting point in canvas if(!randstart) { // store new starting point x0=x; y0=y; // start walk stop(); currentpoint = 0; frozen = false; start(); } return true; } // (Re)Start the walk public void start() { Graphics g = getGraphics(); if(frozen) { //Do nothing. The user has requested that we //stop changing the image. } else { //start walk if (drawThread == null) { drawThread = new Thread(this,"walk"); } drawThread.start(); } } public void stop() { //Stop the drawing thread. drawThread = null; } // Random walk iteration public void run() { float r; Dimension d = size(); //Just to be nice, lower this thread's priority //so it can't interfere with other processing going on. Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //This is the animation loop. while (Thread.currentThread() == drawThread) { // If first iteration if(currentpoint==0) { // erase previous walk update(getGraphics()); mx = d.width; my = d.height; if(randstart) { x0 = 1 + (int) (rand.nextFloat()*(mx-1)); y0 = 1 + (int) (rand.nextFloat()*(my-1)); } currentpoint = 0; ix = x0; iy = y0; nx = x0; ny = y0; } // the walk if(currentpoint0.75) {nx++;} else if(r>0.5) {nx--;} else if(r>0.25) {ny++;} else {ny--;} if(wrap) { if(nx<1) {nx=mx-2; ix = nx;} if(ny<1) {ny=my-2; iy = ny;} if(nx>mx-2) {nx=1; ix = nx;} if(ny>my-2) {ny=1; iy = ny;} } else { if(nx<1) nx=2; if(ny<1) ny=2; if(nx>mx-2) nx=mx-3; if(ny>my-2) ny=my-3; } } repaint(); try { drawThread.sleep(10); } catch (InterruptedException e) { } } } public void paint(Graphics g) { update(g); } public void update(Graphics g) { Dimension d = size(); //Create the offscreen graphics context, if no good one exists. if ( (offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height) ) { offDimension = d; offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); offImage1 = createImage(d.width, d.height); offGraphics1 = offImage1.getGraphics(); } if(currentpoint==0) { offGraphics.setColor(Color.pink); offGraphics.fillRect(1,1,d.width - 2,d.height - 2); offGraphics.setColor(Color.black); offGraphics.drawRect(0, 0,d.width - 1,d.height - 1); offGraphics.setColor(Color.red); g.drawImage(offImage, 0, 0, this); } else { //write on second buffer offGraphics1.drawImage(offImage,0,0, this); //add starting and end points on second buf. offGraphics1.setColor(Color.orange); offGraphics1.fillOval(x0-3,y0-3,6,6); offGraphics1.fillOval(ix-3,iy-3,6,6); offGraphics1.setColor(Color.black); offGraphics1.drawRect(0, 0, size().width - 1, size().height - 1); g.drawImage(offImage1, 0, 0, this); } } }