home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 3.5 KB | 151 lines |
- // AnimatedPanel.java
- // 28.02.96
- //
- // an animation
-
- package cybcerone.utils;
-
- import java.awt.Image;
- import java.awt.Graphics;
- import java.awt.Event;
- import java.awt.MediaTracker;
-
- /**
- * An extension of IdPanel that animates a sequence of images.
- */
- public class AnimatedPanel extends IdPanel implements Runnable {
- private Thread animation;
- private int beginDelay;
- private int delay;
- private Image frames[];
- private int currentFrame;
- private protected Image currentFrameImage;
- private int frameCount;
-
- private MediaTracker tracker;
-
- /**
- * @param beginDelay The delay at the beginning of each animation loop.
- * @param delay The delay between frames.
- */
- public AnimatedPanel (String id, String statusText, int beginDelay,
- int delay, Appletlike app) {
- super (id, statusText, app);
- this.beginDelay = beginDelay;
- this.delay = delay;
- tracker = new MediaTracker (this);
- }
-
- /** Sets the animation frames to frames[]. */
- public void setFrames (Image frames[]) {
- this.frames = frames;
- if (frames != null) {
- frameCount = frames.length;
- for (int i = 0; i < frameCount; i++)
- tracker.addImage (frames[i], i);
- } else {
- frameCount = 0;
- currentFrameImage = null;
- repaint ();
- }
- }
-
- /**
- * Sets the animation frames to the files beginning with framesPath,
- * appended with numbers from 1 to frameCount and '.gif'.
- */
- public void setFrames (String framesPath, int frameCount) {
- this.frameCount = frameCount;
- frames = new Image[frameCount];
-
- for (int i = 1; i <= frameCount; i++) {
- frames[i-1] = app.getImage (framesPath + i + ".gif", i);
- tracker.addImage (frames[i-1], i-1);
- }
- }
-
- /** Resets the animation and starts it. */
- public void start () {
- if (frameCount > 0) {
- currentFrame = 0;
- currentFrameImage = frames[0];
- repaint ();
- if (animation == null) {
- animation = new Thread (this);
- animation.start ();
- }
- }
- }
-
- /** Stops the animation. Duh. */
- public void stop () {
- if (animation != null && animation.isAlive ())
- animation.stop ();
- animation = null;
- }
-
- /** When start starts, this is what it starts. */
- public void run () {
- try {
- for (int i = 0; i < frameCount; i++) {
- tracker.waitForID (i);
- animation.yield ();
- }
- } catch (InterruptedException e) {
- }
-
- while (animation != null)
- this.animate ();
- }
-
- /** Does one cycle through the animation. */
- protected void animate () {
- int realDelay;
-
- do {
- repaint ();
-
- if (currentFrame == 0)
- realDelay = delay + beginDelay;
- else
- realDelay = delay;
-
- try {
- animation.sleep (realDelay);
- } catch (InterruptedException e) {
- }
-
- if ((currentFrame + 1) < frameCount) {
- currentFrame++;
- } else {
- currentFrame = 0;
- }
- currentFrameImage = frames[currentFrame];
- } while (currentFrame != 0);
- }
-
- /** Draw the current frame. */
- public void paint (Graphics g) {
- if (currentFrameImage != null)
- g.drawImage (currentFrameImage, 0, 0, this);
- }
-
- /**
- * If the user clicks and a next screen is defined, go there. Otherwise
- * stop the animation if it's running or start it if it isn't.
- */
- public boolean mouseDown (Event e, int x, int y) {
- Object next = getNext ();
-
- if (next != null) {
- return super.mouseDown (e, x, y);
- } else if (animation != null) {
- this.stop ();
- } else {
- this.start ();
- }
- return true;
- }
-
- }
-