Results 1 to 2 of 2
  1. #1
    Enterprize is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default How to get a normally-repeating gif to only display once

    I am working on a game using Swing for the GUI, and part of the dialog requires a 'surprised' sprite gif to run.

    I have the normal talking gif in a JLabel, and can replace it with the 'surprised' gif, but I am unable to find a way to have it only display once. I have thought about getting each frame as rotating still jpg's or png's and then using the display time between the gif's frames, but I cannot find a way to get the display times, and it would be a terrible amount of work.

    Also, if I could find a value of the total length of the gif, I might be able to let the rest of the thread Sleep for that length while it runs.

    If any of these solutions is possible, it would be great for you to help. Thanks.
    Last edited by Enterprize; 02-11-2011 at 02:21 AM. Reason: Added detail

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power


    In regards to doing it frame by frame (thats what I do) it isn't hard at all. the javax.swing.Timer is made for this reason!

    You can do something like this:

    Java Code:
        public void Animate() {
            //250ms refresh rate
            Timer t = new Timer(250, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
        int currentFrame = 0;
        //Initialize this with ImageIO to load the frames
        BufferedImage[] frames;
        private void advanceFrame() {
            if(currentFrame < frames.length-1){
            } else{
                currentFrame = 0;
    Make sense?

Similar Threads

  1. jxl repeating rows
    By mermaid in forum New To Java
    Replies: 0
    Last Post: 01-14-2011, 03:59 PM
  2. Repeating
    By AbdulAziz Bader in forum New To Java
    Replies: 3
    Last Post: 05-05-2010, 11:35 PM
  3. Repeating Do-While Statement
    By adamg106 in forum New To Java
    Replies: 10
    Last Post: 04-22-2010, 07:09 AM
  4. Non-Repeating Random Integers
    By Psyclone in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 09:04 PM
  5. adjacent repeating letters
    By artemis_r2 in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 04:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts