/* * 1.1 code. (EK, Oct 26, 2000) */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; /* * Based on Arthur van Hoff's animation examples, this applet * can serve as a template for all animation applets. */ public class AnimatorApplet_1_1 extends Applet implements Runnable { // EK: java.lang.Runnable must implement the run() method // AnimatorApplet_1_1 can be supplied as arg to Thread constructor int frameNumber = -1; int delay; Thread animatorThread; boolean frozen = false; public void init() { // EK: overwrites Applet.init() String str; int fps = 10; //How many milliseconds between frames? str = getParameter("fps"); // EK: Applet.getParameter try { if (str != null) { fps = Integer.parseInt(str); } } catch (Exception e) {} delay = (fps > 0) ? (1000 / fps) : 100; this.addMouseListener(new MouseAdapter (){ public void mouseClicked(MouseEvent e) {if (frozen) { frozen = false; start(); } else { frozen = true; stop(); } } } ); } public void start() { // EK: Applet.start() if (frozen) { //Do nothing. The user has requested that we //stop changing the image. } else { //Start animating! if (animatorThread == null) { animatorThread = new Thread(this); } animatorThread.start(); // EK: Thread.start() // calls this.run() } } public void stop() { // EK: Applet.stop(); terminates the while loop in run() //Stop the animating thread. animatorThread = null; } public void run() { // EK: needed for Runnable //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); //Remember the starting time. long startTime = System.currentTimeMillis(); //Remember which thread we are. Thread currentThread = Thread.currentThread(); //This is the animation loop. while (currentThread == animatorThread) { //Advance the animation frame. frameNumber++; //Display it. repaint(); //Delay depending on how far we are behind. try { startTime += delay; Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) { } // end try/catch }// end while }// end run() //Draw the current frame of animation. public void paint(Graphics g) { g.setFont(new Font("TimesRoman", Font.BOLD+Font.ITALIC, 24)); g.drawString("Frame " + frameNumber, 0, 30); } }// end class AnimatorApplet