import java.awt.*; import java.applet.*; public class hill_climb extends Applet implements Runnable { Thread animator_thread = null; Image image; int p,q,len = 18; int count = 0; Button hill,foot,plateau,ridge,start,stop; int X1[]={261,261,261,261,261,261,261,261,261,261,261,261,261,261,261}; int Y1[]={261,261,261,261,261,261,261,261,261,261,261,261,261,261,261}; int x1=261;int y1=256;int x2=277;int y2=249; //For Hill Climbing int X[]={261,277,288,298,309,320,334,347,355,366,374,380,384,390,395,396,399,401}; int Y[]={256,249,238,229,221,213,201,197,185,175,161,149,134,115,100,86,76,70}; // for foothill problem int X2[]={302,307,315,325,341,353,371,387,398,406,415,424,433,439,432}; int Y2[]={252,245,244,241,238,236,229,222,213,205,194,184,176,166,155}; //for plateau problem int X3[]={165,171,176,186,191,196,202,208,214,222,231,237,245,252,258,265, 275,275,268,260,254,235,230,215,208,201,206,211,204}; int Y3[]={253,249,244,242,243,243,243,241,239,240,238,235,234,233,232,230, 226,216,210,208,208,207,206,204,203,195,192,186,183}; //for ridge problem int X4[]={234,240,246,251,257,263,269,276,280,283,286,290,294,299,302,305,308,310,311,313}; int Y4[]={255,254,253,251,249,247,246,245,243,241,239,238,236,235,234,232,230,229,228,227}; public void init() { hill = new Button("Hill Climbing");add(hill); foot = new Button("Foothill Problem");add(foot); plateau=new Button("Plateau Problem");add(plateau); ridge = new Button("Ridge Problem");add(ridge); // start = new Button("Start");add(start); // stop = new Button("Stop");add(stop); Graphics g = getGraphics(); image=getImage(getDocumentBase(),getParameter("image")); g.drawImage(image,100,0,this); animator_thread = new Thread(this); animator_thread.start(); } //private Thread animator_thread=new Thread(this); public void start() { Graphics g = getGraphics(); image=this.getImage(this.getDocumentBase(),this.getParameter("image")); g.drawImage(image,100,0,this); repaint(); // X1=X;Y1=Y; } public void stop() { animator_thread.stop(); } public boolean action(Event e,Object arg){ /* if (e.target == start) { animator_thread = new Thread(this); animator_thread.start(); } if (e.target == stop) { stop(); } */ if(e.target == hill) { animator_thread = new Thread(this); animator_thread.start(); X1 = X; Y1 = Y; len = 18; } else if(e.target == foot) { animator_thread = new Thread(this); animator_thread.start(); X1 = X2; Y1 = Y2; len = 15; } else if(e.target == plateau) { animator_thread = new Thread(this); animator_thread.start(); X1 = X3; Y1 = Y3; len=28; } else if(e.target == ridge) { animator_thread = new Thread(this); animator_thread.start(); X1 = X4; Y1 = Y4; len=20; } return true; } public void run() { Graphics g = getGraphics(); g.drawImage(image,0,0,this); for(int i=0;i<=len;i++){ try{ p = x2-x1; q = y1-y2; p= Math.max(p,q); q=p; try {Thread.sleep(220);} catch(InterruptedException e){;} draw1(); x1=X1[i+1]; y1 = Y1[i+1]; x2=X1[i+2]; y2=Y1[i+2]; }catch(ArrayIndexOutOfBoundsException o){;} catch(NullPointerException npe){;} }//end for }//end run public void destroy() { image.flush(); } public void draw1(){ Graphics g = getGraphics(); g.drawImage(image,0,0,this); g.setColor(Color.green); g.fillRect(401,65,8,8); draw(); g.setColor(Color.blue); g.drawLine(x1,y1,x1+p,y1); try {Thread.sleep(220);} catch(InterruptedException e){;} g.drawLine(x1,y1,x1+p,y1-q); try {Thread.sleep(220);} catch(InterruptedException e){;} g.drawLine(x1,y1,x1,y1-q); try {Thread.sleep(220);} catch(InterruptedException e){;} g.drawLine(x1,y1,x1-p,y1-q); try {Thread.sleep(220);} catch(InterruptedException e){;} g.drawLine(x1,y1,x1-p,y1); try {Thread.sleep(220);} catch(InterruptedException e){;} g.drawLine(x1,y1,x1-p,y1+q); try {Thread.sleep(220);} catch(InterruptedException e){;} g.drawLine(x1,y1,x1,y1+q); try {Thread.sleep(220);} catch(InterruptedException e){;} g.drawLine(x1,y1,x1+p,y1+q); try {Thread.sleep(220);} catch(InterruptedException e){;} g.setColor(Color.green); g.drawLine(x1,y1,x2,y2); // try {Thread.sleep(220);} catch(InterruptedException e){;} } public void draw(){ Graphics gr = getGraphics(); gr.setColor(Color.red); count++; for(int i=0;i