import javax.swing.JApplet; import java.awt.*; /** * WalkerApplet is the container for the Walker graphics * project. * * Walker 3. Drawing your Walker. * * Add a new constructor to your Walker class that will take * a Graphics2D object as the first parameter. Give the Walker * a new private instance variable p (for page) that is also * a Graphics2D object. Start by drawing a simple figure. We * will discuss in class how to reference the figure to a location * on the page and then add complexity with other methods. * * Walker 4. Animating your walker and drawing a background. * * @author Mrs. White * @version Spring 2003 */ public class WalkerApplet extends JApplet { // instance variables - replace the example below with your own // private int x=400; private Walker sleepy; private Walker stupid; private int dir=1; /** * Called by the browser or applet viewer to inform this Applet that it * has been loaded into the system. It is always called before the first * time that the start method is called. */ public void init() { // provide any initialisation necessary for your Applet } /** * Called by the browser or applet viewer to inform this Applet that it * should start its execution. It is called after the init method and * each time the Applet is revisited in a Web page. */ public void start() { setVisible (true); Graphics2D g = (Graphics2D)getGraphics(); sleepy =new Walker(g,200); stupid=new Walker(g,0); paint(g); /* for (int count = 0; count < 15; count++) { paint(g); sleepy.move(); pause(500); } */ } /** * This may be the most important method in your applet: Here, the * drawing of the applet gets done. "paint" gets called everytime the * applet should be drawn on the screen. So put the code here that * shows the applet. * * @param g the Graphics object for this applet */ public void paint(Graphics2D g) { for(int x=0;x<5000;x++) { sleepy.drawBackground(); // clear area sleepy.draw(); sleepy.move(); stupid.scriptDraw(dir,0); if(x%500==0) dir*=-1; pause(5); } } /** * Used between drawing the different Walkers to provide * animation */ private static void pause (int wait) { long timeToQuit = System.currentTimeMillis() + wait; while (System.currentTimeMillis() < timeToQuit) { } // take no action } //====================== }