import java.applet.Applet; import java.util.Random; import java.awt.*; public class RANDbm2d extends Applet { Panel mypanel = new Panel(); BM2 mycanvas = new BM2(this); Label mylabel = new Label("#steps:",Label.RIGHT); Button redrawButton = new Button("New Path"); 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() { // Fisrt 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==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 BM2 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; //current points float fx,fy; int mx,my; //max coord. of the canvas // 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 BM2(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; fx = x; fy = 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 walking around. } 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(); int nx,ny; //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; fx = x0; fy = y0; } //Advance the walk if(currentpointmx-2) { ix = (int) (mx - 2 - fx + 1); fx = fx - mx + 2 + 1; nx = (int) fx; offGraphics.drawLine(ix,iy,nx,ny); } if(fy<1) { iy = (int) (my - 2 + 1 - fy); fy = my - 2 - 1 + fy; ny = (int) fy; offGraphics.drawLine(ix,iy,nx,ny); } if(fy>my-2) { iy = (int) (my - 2 - fy + 1); fy = fy - my + 2 + 1; ny = (int) fy; offGraphics.drawLine(ix,iy,nx,ny); } } else { if(fx<1) { ix = (int) fx; fx = 1 - fx; nx = (int) fx; offGraphics.drawLine(ix,iy,nx,ny); } if(fx>mx-2) { ix = (int) fx; fx = mx - 2 - (fx - (mx - 2)); nx = (int) fx; offGraphics.drawLine(ix,iy,nx,ny); } if(fy<1) { iy = (int) fy; fy = 1 - fy; ny = (int) fy; offGraphics.drawLine(ix,iy,nx,ny); } if(fy>my-2) { iy = (int) fy; fy = my - 2 - (fy - (my - 2)); ny = (int) fy; offGraphics.drawLine(ix,iy,nx,ny); } } ix = nx; iy = ny; } 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); } } }