Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2005

Tip: Looking for answers? Try searching our database.

Animated GIF Freezes in the first cycle

Thread view: 
Bud Curtis - 12 May 2005 05:32 GMT
I added an animated GIF to a JLabel's Icon, but the animation doesn't
complete a full cycle.  It gets through half of the frames and then freezes
in that position.  Can someone tell me what I'm doing wrong or point me to
an example on how to add an animated GIF to either a JLabel or JPane.

       private javax.swing.JLabel searchingAnimation;

       searchAnimation =
JarItemRenderer.createImageIcon("/com/CurtisSoft/images/search3.gif");
       if (searchAnimation != null)
       {
           searchingAnimation.setIcon(searchAnimation);
       }

Signature

Bud Curtis
Colorado Springs, CO

Steve W. Jackson - 12 May 2005 15:41 GMT
> I added an animated GIF to a JLabel's Icon, but the animation doesn't
> complete a full cycle.  It gets through half of the frames and then freezes
[quoted text clipped - 9 lines]
>             searchingAnimation.setIcon(searchAnimation);
>         }

an animated GIF isn't going to continuously update when used on any
Swing component.  Instead, each time that component is repainted, it
will advance to the next image.  So to get it updating continuously, you
may need to extend the JLabel and add some form of timer that handles
this.  But remember, that's going to be costly since it requires
repeated actions on the EDT.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Roland - 12 May 2005 16:12 GMT
>>I added an animated GIF to a JLabel's Icon, but the animation doesn't
>>complete a full cycle.  It gets through half of the frames and then freezes
[quoted text clipped - 18 lines]
>
> = Steve =
That's not true. If you use java.awt.Toolkit to create an image of an
animated GIF, the image will automatically keep playing (if the GIF
specifies to repeat forever).

Demo:

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AnimatedGIF {
   public static void main(String[] args) throws IOException {

      URL animgifURL = new URL(
      "http://upload.wikimedia.org/wikipedia/en/b/be/Mashimaro.gif");

      Image animgifImg =
         Toolkit.getDefaultToolkit().createImage(animgifURL);

      Icon animgifIco = new ImageIcon(animgifImg);

      JLabel animgifLbl = new JLabel();
      animgifLbl.setIcon(animgifIco);
      animgifLbl.setHorizontalAlignment(JLabel.CENTER);

      JLabel copyrightLbl = new JLabel();
      copyrightLbl.setText("An animation of Mashimaro");
      copyrightLbl.setHorizontalAlignment(JLabel.CENTER);

      JPanel jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(animgifLbl, BorderLayout.CENTER);
      jContentPane.add(copyrightLbl, BorderLayout.SOUTH);

      JFrame app = new JFrame();
      app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      app.setContentPane(jContentPane);
      app.pack();
      app.setLocationRelativeTo(null); // center on screen
      app.setTitle("Animated GIF");
      app.setVisible(true);
   }
}

Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Steve W. Jackson - 12 May 2005 16:55 GMT
> >>I added an animated GIF to a JLabel's Icon, but the animation doesn't
> >>complete a full cycle.  It gets through half of the frames and then freezes
[quoted text clipped - 68 lines]
>     }
> }

I wrote my response based on an experience in our application, in which
animated images did not update without a nudge when we used one in a
JButton.  Either we've corrected whatever we did wrong in the time since
that was implemented, or it's been affected by our use of a newer Java
release, or maybe my memory is just faulty.  But it does indeed continue
to update on the JButton in our application.  Oddly enough, it stops
when rolling the mouse over the button, but that shouldn't be an issue
with the OP's attempted use in a JLabel.

Thanks for the insight.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Roland - 12 May 2005 17:49 GMT
>>>>I added an animated GIF to a JLabel's Icon, but the animation doesn't
>>>>complete a full cycle.  It gets through half of the frames and then freezes
[quoted text clipped - 24 lines]
>>
>>Demo:

[snip]

> I wrote my response based on an experience in our application, in which
> animated images did not update without a nudge when we used one in a
[quoted text clipped - 8 lines]
>
> = Steve =
I've noted that if you use ImageIO to create an image of an animated
GIF, the animation does not work (I think it only displays the first
frame of the GIF).
Using Toolkit creatImage or getImage, the animation is handled correctly
(at least on Windows platform, using the sun.awt.windows.WToolkit
implementation). Maybe there are issues with the Toolkit implementations
on other platforms.
To OP: verify that your GIF does indeed repeat.
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Bud Curtis - 13 May 2005 04:55 GMT
Thank you, Roland, for the example.  I tried your example, and your URL GIF
works fine.  I then tried several of my animated GIFS and found a real
mixture of display results.  The GIF I wanted to use did exactly what I was
experiencing witrh the original code (stoped after the first few frames).
One of the five GIFs worked just like I wanted.  A couple GIFs animated in a
very jerky repeated fashion.

Is there something special about each of these GIFS?  Each was loaded into
GIF Movie Gear (an animated GIF editor) and all animated just fine.  Can you
suggest some thing I should look at.

Signature

Bud Curtis
Colorado Springs, CO

>
> >>I added an animated GIF to a JLabel's Icon, but the animation doesn't
[quoted text clipped - 69 lines]
>     }
> }
Roland - 13 May 2005 08:29 GMT
> Thank you, Roland, for the example.  I tried your example, and your URL GIF
> works fine.  I then tried several of my animated GIFS and found a real
[quoted text clipped - 6 lines]
> GIF Movie Gear (an animated GIF editor) and all animated just fine.  Can you
> suggest some thing I should look at.

You could try to display them in another image viewer (e.g. IrfanView)
or in your browser, and see how they behave in them.
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Bud Curtis - 14 May 2005 16:51 GMT
I tried IrfanView and all of them display fine.
Signature

Bud Curtis
Colorado Springs, CO

>
> > Thank you, Roland, for the example.  I tried your example, and your URL GIF
[quoted text clipped - 10 lines]
> You could try to display them in another image viewer (e.g. IrfanView)
> or in your browser, and see how they behave in them.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.