import java.applet.Applet; import java.util.Random; import java.awt.*; public class RANDeds1d extends Applet { Panel mypanel = new Panel(); EDS1 mycanvas = new EDS1(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 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(300,300); 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(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==mycheckbox1) { mycanvas.randstart = !mycanvas.randstart; return true; } return false; } } // Here is the part concerning the EDS class myedsclass extends Eds { float b(float x, float t) /* Ito drift */ { return (float) 0.0; } float sig(float x,float t) { if (x>=0.0) { return (float) Math.sqrt(x); } else { return (float) 0.0; } } float dsigdx(float x,float t) { if (x>=0.0) { return (float) (-0.5/Math.sqrt(x)); } else { return (float) 0.0; } } float a(float x, float t) { return t; //not used here } } class EDS1 extends Canvas implements Runnable { // Class EDS1 // Walk variables int nbpoints = 0; int currentpoint = 0; boolean randstart = true; int y0; //starting point int ix,iy,iy1; //current point float fx,fy,fy1; int mx,my; //max coord. of the canvas float h,sd; // Image buffers Dimension offDimension; Image offImage,offImage1,offImage2; Graphics offGraphics,offGraphics1,offGraphics2; // thread controls boolean frozen = true; Thread drawThread = null; // container from where to get events Container control; // Random generator Random rand = new Random(45682); //Eds themselves... myedsclass eds1 = new myedsclass(),eds2 = new myedsclass(); public EDS1(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 y0=y; fy = y; fy1 = 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,dw,dwc; Dimension d = size(); int ny,ny1; int i; //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); h = (float) 0.001; sd = (float) Math.sqrt(h); eds1.method = Eds.MILSHTEIN; eds2.method = Eds.MILSHTEIN; //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) { y0 = 1 + (int) (rand.nextFloat()*(my-1)); } currentpoint = 0; ix = mx-10; iy = y0; iy1 = y0; fy = y0; fy1 = y0; } //Advance the walk if(currentpoint