Re: Need some help with animation

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

  • Next message: Rhino: "Re: a simple java timer"
    Date: Thu, 27 Nov 2003 08:32:54 -0500
    
    

    "ak" <k.andrei@gmx.de.spam> wrote in message news:bq2ssn$u2a$1@online.de...
    >
    > "Rhino" <rhino1@NOSPAM.sympatico.ca> schrieb im Newsbeitrag
    > news:fd2xb.13868$dt2.1004213@news20.bellglobal.com...
    > >
    > > "ak" <k.andrei@gmx.de.spam> wrote in message
    > news:bq2364$12j$1@online.de...
    > > > You have some mistakes here.
    > > >
    > > > a) you load only FIRST frame from your GIF, try to use ImageIO to get
    > all
    > > of
    > > > them
    > >
    > > Not really. You are right that I am loading only only file but that file
    > is
    > > an animated GIF (GIF89a) so it contains all the frames.
    >
    > Yes, file contains all frames, but java picks only the first of them.
    >
    Try running my code with any animated GIF of your choice. You'll see that it
    is playing all the frames in the file.

    > >
    > > > b) use MemoryImageSource to create animation. See javadoc for details.
    > > >
    > > I'll have a look at this.
    > >
    > > Thanks!
    > >
    > > Rhino
    > > >
    > > >
    > > > "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."
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >


  • Next message: Rhino: "Re: a simple java timer"

    Relevant Pages

    • Re: loop in gnuplot ?
      ... For an animated gif, I will need about 25 frames. ... need to make an external script? ...
      (comp.graphics.apps.gnuplot)
    • Re: screamernet question - Yeeeaaahhh Baaaaaby!
      ... Anyway, as for the frames into animation, just do what I always do. ... How ever many Codecs you have installed will determine what types of animation formats you can choose from. ... I.E., AVI or QT and whatever those would be under the avi, like DivX or what ever you like. ...
      (comp.graphics.apps.lightwave)
    • Re: screamernet question - Yeeeaaahhh Baaaaaby!
      ... I have checked my content structure, re-imported all the plugin folders, checked the content directory etc. etc. ... The remote node finds the objects correctly via the share, deposits the output file correctly - via the share but will not attempt to get the image maps from the share. ... Thanks for your idea for converting frames - thanks to Jim too with the QT tip. ... Anyway, as for the frames into animation, just do what I always do. ...
      (comp.graphics.apps.lightwave)
    • Re: (Video) Joe Showers & Wes Peden Tween
      ... between each shot but with a connection at the beginning and end. ... Basic tweening is a well known animation process and software packages like ImageReady offer tweening functions, in which the software interpolates one or more new frames between two previously adjacent frames. ...
      (rec.juggling)
    • Re: Animation bleibt stehen
      ... Frames keine 5 sec. ... Ich hatte mit Office 2002 eigentlich die besseren Erfahrungen mit der ... > Die Animation habe ich selber in SmartMorph hergestellt (120 Frames, ... Next by Date: ...
      (microsoft.public.de.german.powerpoint)