// AnimationThread.java // This file provides the class AnimationThread which is needed by // the applet whose main class is in Animate.java. // S. Tanimoto, 10 April 1999. // An animation thread is responsible for updating the location // of one animation object according to the points in its trajectory. // It does one update approximately every SLEEP_PERIOD milliseconds. // If it is the "repainting thread" then it also calls repaint, // so that the screen will get updated. // Each thread is told when it is created whether it is the repainting // thread or not. The boolean parameter repaintingThread controls this. // This class provides a static method animationInProgress() which returns // true if any animation threads are still running. import java.util.*; import java.awt.*; public class AnimationThread extends Thread { AnimationObject theObj; // The anim. obj. whose location needs updating. Canvas theAC; boolean repaintingThread = false; static int numActiveThreads = 0; static final int SLEEP_PERIOD = 150; // milliseconds. public AnimationThread(AnimationObject theObj, Canvas theAC, boolean repaintingThread) { this.theObj = theObj; this.theAC = theAC; this.repaintingThread = repaintingThread; } public void run() { if (theObj == null) return; if (theObj.trajectory == null) return; oneMore(); // Add one to the number of running anim. threads. for (Enumeration locations = theObj.trajectory.elements(); locations.hasMoreElements();) { theObj.currentLocation = (Point) locations.nextElement(); try { Thread.sleep(SLEEP_PERIOD); } catch (InterruptedException e) {} if (repaintingThread) theAC.repaint(); } oneLess(); // Reduce num. of running threads by one. } // Prevent concurrent writes to this shared variable when // incrementing and decrementing the number of active anim. threads. synchronized void oneMore() { numActiveThreads++; } synchronized void oneLess() { numActiveThreads--; } public static boolean animationInProgress() { if (numActiveThreads > 0) return true; return false; } }