Need some help with animation

From: Rhino (rhino1_at_NOSPAM.sympatico.ca)
Date: 11/25/03


Date: Tue, 25 Nov 2003 17:00:28 -0500

I need some help with animation.

Specifically, I am trying to create a class that will play an animated GIF
(GIF89a) in a JPanel and stop or start when the user clicks the mouse. This
seems like something that should be pretty straight forward but I've already
put several hours into this without getting the results I want.

My class *almost* works satisfactorily. The animation starts and stops when
I click (actually, I do two clicks to start it the very first time, after
that the mouse toggles the animation with each click.) Unfortunately, the
animation also disappears whenever it is not running. That's the part I need
help with.

I would like the animated GIF to be visible and initially stopped on the
first frame when it is started the first time, then start when I've clicked
the mouse (twice if it's the very first time since the panel was
instantiated). The animation should stop but leave its current frame visible
when I click the mouse again, then restart from where it left off on the
next click.

I've tried everything I can think of and researched the issue as thoroughly
as I could in Usenet but I can't find an example that does this or any
discussion of the relevant principles so that I can figure it out for
myself.

Here is my code with most of the comments removed so that the post is not
overly long. Please feel free to tell me to do things differently.
Personally, I find the logic in the paintComponent() method a little
odd-looking but I can't think of anything else to try there that hasn't
already failed to work.

package racer;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.MediaTracker;

import java.awt.Toolkit;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.io.File;

import java.net.URL;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class AnimationPanel extends JPanel implements Runnable {

    final String CLASS_NAME = getClass().getName();

    static final boolean DEBUG = false;

    Thread animationThread = null;

    boolean keepDrawing;

    boolean frozen = false;

    Image animation = null;

    MediaTracker tracker = null;

    int stillWidth = 0;

    int stillHeight = 0;

public static void main(String[] args) {

    JFrame myFrame = new JFrame("Drawing Panel");

    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    AnimationPanel drawingPanel = new AnimationPanel("racer/AnimatedGIFs",
"catwalk.gif", true);

    myFrame.getContentPane().add(drawingPanel, BorderLayout.CENTER);

    myFrame.pack();

    myFrame.setVisible(true);

}

public AnimationPanel(String animationImageFilePath, String
animationImageFile, boolean controlWithMouse) {

    super();

/* Get the URL for the animated image file. */

    URL animationGIF =
this.getClass().getClassLoader().getResource(animationImageFilePath +
File.separator + animationImageFile);

/* Get the actual image file. */

    animation = Toolkit.getDefaultToolkit().getImage(animationGIF);

    try {

        tracker = new MediaTracker(this);

        tracker.addImage(animation, 0);

        tracker.waitForAll();

        }

    catch (Exception excp) {

        excp.printStackTrace();

        }

/* Assuming that all of the stills in the animation are the same size,
determine the

height and width of the panel that will be needed to display them. */

    stillWidth = animation.getWidth(this);

    stillHeight = animation.getHeight(this);

/* If the user wishes to control the animation with the mouse, activate this
listener. */

    if (controlWithMouse) {

        addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent evt) {

                if (frozen) {

                    frozen = false;

                    startAnimation();

                    }

                else {

                    frozen = true;

                    stopAnimation();

                    }

                }

            });

        } //end if

} //end AnimationPanel()

public void startAnimation() {

if (animationThread == null) {

    animationThread = new Thread(this);

    keepDrawing = true;

    animationThread.start();

    }

} //end startAnim()

public void stopAnimation() {

if (animationThread != null) {

    keepDrawing = false;

    animationThread = null;

    }

} //end stopAnim()

public void run() {

while (keepDrawing) {

    repaint(); //call paint

    try {

        Thread.sleep(10); //sleep a little to slow it down

        }

    catch (InterruptedException ie_excp) {

        System.err.println("Error: " + ie_excp);

        }

    } //end while

} // end run()

public void paintComponent(Graphics graphics) {

if (!keepDrawing) {

/* If we DON'T paint the parent class (super), other parts of the GUI which
imbed this

* panel overwrite the area where the animation is drawn. If we DO paint the
parent

* class, the animation is blanked out so it disappears when the animation is
stopped

* rather than freezing on the last frame.

*/

        super.paintComponent(graphics);

        return;

        }

    else {

        super.paintComponent(graphics);

        graphics.drawImage(animation, 0, 0, this);

        }

} //end paint()

public Dimension getPreferredSize() {

    return new Dimension(stillWidth, stillHeight);

}

} //end of class

-- 
Rhino
---
rhino1 AT sympatico DOT ca
"If you want the best seat in the house, you'll have to move the cat."


Relevant Pages

  • Re: Is this code ok ?
    ... That's the first time ever I write code in javascript. ... moved around in a microfilm viewer. ... animation was accelerating. ... and use one of the transition effects that libraries ...
    (comp.lang.javascript)
  • Re: Three dimensions pictures?
    ... the brain can maintain the illusion between animation frames. ... perhaps could produce a wobbly shark. ... the first time. ... out a Basic program to make one of these things and has a nice framed ...
    (alt.html)
  • Is this code ok ?
    ... That's the first time ever I write code in javascript. ... As the image is much larger than the window, ... moved around in a microfilm viewer. ... animation was accelerating. ...
    (comp.lang.javascript)
  • Re: Guyver TV - Episode 1
    ... >of remaking this series and yet do such a poor job with the animation, ... >in this sort of series the animation ought to be a big part of the ... was watching it the first time with my old tape-trading group back when, ...
    (rec.arts.anime.misc)
  • Re: Save video as image sequence
    ... > Lightwave and remove the background (by saving the alpha channel?). ... > and using Lightwave as a "cheap" compositing program. ... Fairly easy if you have Adobe Premiere - render the animation out as a ... Save the frame sequences as a animated gif. ...
    (comp.graphics.apps.lightwave)