1

I think I've just pinpointed the problem, but I'm still not sure what to do about it.

I've created various animated gifs using Photoshop and I wanted to display them in my java application. Using ImageIcon, some of them display as they should. Those are the ones with frames that have the same frame rate (such as each frame is 1 second long). However, the gifs that have varying frame rates (ex. one frame is 1 second, the other is .5 seconds, the next is .2) doesn't seem to be showing correctly. Once the gif reaches the frames that vary in seconds the image messes up. Some examples include the background briefly changing color and half of the image disappearing, or most of the image disappearing with the gif still animating.

I'm having a difficult time articulating, but do I need to use something else other than ImageIcon to correctly load the gif with varying frame rates? Like something with BufferedImage?

Edit: Added complete code below. Again, it works for the image with equal frame rates, but not for one with varying frame rates.

here is the image that works fine

and here is the image that messes up

import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;


public class Creature extends JWindow {
    public void initCreature() {

        JPanel contentPane = (JPanel) getContentPane();
        ImageIcon idleImage = new ImageIcon(getClass().getResource("img.gif"));

        // Set window properties
        getRootPane().putClientProperty("Window.shadow", false);
        setBackground(new Color(0,0,0,0));
        setAlwaysOnTop(true);
        contentPane.setOpaque(false);

        // Get size of user's screen
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
        Rectangle screen = defaultScreen.getDefaultConfiguration().getBounds();
        int taskbarheight = (int) (Toolkit.getDefaultToolkit().getScreenSize().height 
        - GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getHeight());

        int x = (int) screen.getMaxX() - idleImage.getIconWidth();
        int y = (int) screen.getMaxY() - idleImage.getIconHeight() - taskbarheight;

        JLabel imageLabel = new JLabel(idleImage);
        setSize(idleImage.getIconWidth(), idleImage.getIconHeight());
        contentPane.add(imageLabel);
        setLocation(x, y);
        setVisible(true);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                Creature cr = new Creature();
                cr.initCreature();          
            }
        });

    }
}
6
  • Post a minimal example of your code to know what could be wrong. Aug 12, 2015 at 23:39
  • Added to original post. I'm starting to think I probably should use something else other than ImageIcon
    – ChipMonger
    Aug 13, 2015 at 2:23
  • 1) For better help sooner, post a minimal reproducible example or Short, Self Contained, Correct Example. 2) Upload a misbehaving image to an image share site and link to it. Aug 13, 2015 at 3:35
  • Thanks Andrew, fixed my post again and added the complete program + link to pic. I would have figured I'd know how best to post given I lurk here so much.
    – ChipMonger
    Aug 13, 2015 at 4:51
  • The frame rates seem the same in both versions (one seen in a browser, the other in a JLabel). At a guess I'd say the problem is that PhotoShop is using an advanced encoding to ensure that only the parts of the frame that change, are updated. Note that although Java supports a particular file type like GIF, PNG or JPEG, does not mean it correctly understands every encoding type for each file type. A simpler image that, for every change, changes the entire frame should work better, but also be larger in bytes. Aug 13, 2015 at 5:00

2 Answers 2

3

The frame rates seem the same in both versions (one seen in a browser, the other in a JLabel). The actual problem is that the rendering in Java becomes 'clipped' in the areas of the animation that do not change.

At a guess I'd say the problem is that PhotoShop is using an advanced encoding to ensure that only the parts of the frame that change, are updated. Note that although Java supports a particular file type like GIF, PNG or JPEG, does not mean it correctly understands every encoding type for each file type.

A simpler image that, for every change, changes the entire frame should work better, but also be larger in bytes. In fact, you can see that working in the other image. Because the little dragon is hunching down then rising back up, all parts of the visible image need to change, so the animation of that cannot be optimized in the same way as the first image can.

2
  • I tried a different gif maker (Easy Gif Animator) and made another animation. It works as it should if each frame has the same delay, like the photoshop-made gif (such as frame 1 is 0.5 seconds, frame 2 is 0.5 seconds, etc), but it also shows messed up if there are different delays for each frame (I wanted the blinking frame to be 0.2 seconds long with the other frames being 0.5 seconds. that's where it gets screwy). It still seems to do that even if I changed the 0.2 second frame with an entirely different image. Oh well. Thanks for the help! I'll start tinkering with the animation itself.
    – ChipMonger
    Aug 14, 2015 at 21:20
  • Never mind, I have no idea what's going on. I made a second animation in Easy Gif Animator using the picture with the dragon sitting. The blinking frames still has a shorter delay than the other ones. It ended up working. I don't even know. This is becoming way more confusing than I wanted it to be.
    – ChipMonger
    Aug 14, 2015 at 21:42
1

The imageLabel doesn't know to repaint when the GIF's frame changes. You need to tell the ImageIcon to notify the JLabel of frame changes, by passing the JLabel to setImageObserver:

idleImage.setImageObserver(imageLabel);

From that method's documentation:

Set this property if the ImageIcon contains an animated GIF, so the observer is notified to update its display.

3
  • Am I only supposed to add that? Or something else too? Nothing seems to change if I just add that line of code after initializing the JLabel. I'll read more about it regardless
    – ChipMonger
    Aug 13, 2015 at 4:54
  • Now that I have access to the GIF, I see from running a simple test that my answer doesn't address the problem. Andrew Thompson's answer is the best: If at all possible, change the image to contain full frames instead of incremental frames. This probably should be reported as a Java bug.
    – VGR
    Aug 13, 2015 at 14:33
  • I did a bit more experimenting and it seems to be how the art programs encode the animation with different delay times...and maybe java automatically assumes each frame has the same delay time and repaints it based on that? Though I JUST got one animation working that has different frame delays on Easy Gif Animator (vs when I first used Photoshop) even if my other animation on Easy Gif Animator was messed up. I have no idea what's going on. Thank you for the help anyway!
    – ChipMonger
    Aug 14, 2015 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.