Re: Need some help with animation

From: ak (k.andrei_at_gmx.de.spam)
Date: 11/26/03


Date: Wed, 26 Nov 2003 12:33:29 +0100

You have some mistakes here.

a) you load only FIRST frame from your GIF, try to use ImageIO to get all of
them
b) use MemoryImageSource to create animation. See javadoc for details.

hope it helps

"Rhino" <rhino1@NOSPAM.sympatico.ca> schrieb im Newsbeitrag
news:0iQwb.12685$dt2.786959@news20.bellglobal.com...
> 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: Need some help with animation
    ... > a) you load only FIRST frame from your GIF, try to use ImageIO to get all ... an animated GIF so it contains all the frames. ... Rhino ... >> I need some help with animation. ...
    (comp.lang.java.gui)
  • Re: MouseClicked <-> Thread doesnt repaint...
    ... After each mouse click I'd like to start an animation on my panel. ... I used repaint() to call the paintmethod. ... public void mousePressed{ ...
    (comp.lang.java.programmer)
  • Re: how do I add animated GIF file in my powerpoint presentation?
    ... > default application that Windows uses for GIF's. ... > Photo Editor which displays the first frame of an animated GIF as a still ... > GIF as an animation. ...
    (microsoft.public.powerpoint)
  • Few Image class (and related) questions
    ... walls just to simulate some kind of animation. ... maze geometry on the screen. ... public void paintComponent{ ... Image ScaledSprite; ...
    (comp.lang.java.programmer)
  • Re: Need some help with animation
    ... >> a) you load only FIRST frame from your GIF, try to use ImageIO to get ... > an animated GIF so it contains all the frames. ... Yes, file contains all frames, but java picks only the first of them. ... >> b) use MemoryImageSource to create animation. ...
    (comp.lang.java.gui)