0

Hello, I have a gif in a JFrame. It all works fine except the gif is frozen on the first time as though it is an jpeg or png.

Also, the stackoverflow is telling me I need to add more details even though I have added all the details required in order to state my problem, so feel free to ignore this.

Here is the code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

 public class LoadImageApp extends Component {

BufferedImage img;

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, this);
}

public LoadImageApp() {
    try {
       img = ImageIO.read(new File("spooky.gif"));
   } catch (IOException e) {
   }

}

public Dimension getPreferredSize() {
    if (img == null) {
         return new Dimension(100,100);
    } else {
       return new Dimension(img.getWidth(null), img.getHeight(null));
   }
}

public static void main(String[] args) {

    JFrame f = new JFrame("Load Image Sample");

    f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    f.add(new LoadImageApp());
    f.pack();
    f.setVisible(true);
}

}

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Ryan Dwyer
  • 49
  • 2
  • 7

2 Answers2

4

I'm pretty sure ImageIO has a few problems with animated gifs.

Try using a JLabel and and Icon, like so:

Icon icon = new ImageIcon(filename);
JLabel gif = new JLabel(icon);

JFrame f = new JFrame("Load Image Sample");

f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

f.getContentPane().add(gif);
f.pack();
f.setVisible(true);
Tetramputechture
  • 2,911
  • 2
  • 33
  • 48
  • 1
    You could also obtain an `Image` to paint with from `ImageIcon.getImage()`, as seen [here](http://stackoverflow.com/a/22240487/2587435) and [here](http://stackoverflow.com/q/10836832/2587435). Or if you _must_ use a `BufferedImage`, you could follow [these crazy instructions](http://stackoverflow.com/a/22190844/2587435) from @MadProgrammer :-) – Paul Samsotha Oct 23 '14 at 04:53
  • Replace windowListener with `f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` – Sergii Lagutin Oct 23 '14 at 07:00
  • 1
    I'm afraid this option doesn't work, I've already tried a JLabel, the issue here is with the certain gif not working in the java program, I put up the bounty because I wanted to figure out why some gifs work in some Java programs, but others don't even though we know they work in other programs or languages – DreadHeadedDeveloper Oct 23 '14 at 13:33
  • Rather let me explain my problem, it does solve the original issue of making the gif animate, but now the image is so garbled up with static everywhere that it really doesn't do much more to help. Perhaps you could edit your answer to deal with this issue? If you need the reference gif I am using, it is https://camo.githubusercontent.com/bc0998c24e15d593e310b16a34e8dc091919d453/687474703a2f2f736c65782e6d652f696d6167652f3256317931673354336a33362f6769666966792e6d6f762e676966 – DreadHeadedDeveloper Oct 23 '14 at 14:00
3

As mentioned, ImageIO will not correctly load all the frames of an animated GIF. But one of the Toolkit methods will. E.G.

import java.awt.*;
import javax.swing.*;
import java.net.URL;

// Ach AWT!
//public class LoadImageApp extends Component {
public class LoadImageApp extends JPanel {

    Image img;

    // should be paintComponent for a JComponent
    //public void paint(Graphics g) {
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // call super method first
        g.drawImage(img, 0, 0, this);
    }

    public LoadImageApp() {
        setBackground(Color.BLUE.darker().darker());
        try {
            URL url = new URL("https://i.stack.imgur.com/OtTIY.gif");
            // as mentioned, ImageIO will not load animated GIFs correctly.
            //img = ImageIO.read(url));
            // but the toolkit method will, OTOH..
            img = Toolkit.getDefaultToolkit().createImage(url);
            // ..we need a MediaTracker
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(img, 0);
            mt.waitForAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Dimension getPreferredSize() {
        if (img == null) {
            return new Dimension(100, 100);
        } else {
            return new Dimension(img.getWidth(this), img.getHeight(this));
        }
    }

    public static void main(String[] args) {
        // should be donw on the EDT - BNI
        JFrame f = new JFrame("Load Image Sample");
        f.add(new LoadImageApp());
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

For further discussion, see Show an animated BG in Swing.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Phenomenal work Mr. Thompson! As soon as the Bounty award time limit opens, the 50 is yours! Now perhaps you could put a bit more explanation? I myself am trying to understand why one option worked and the other didn't... – DreadHeadedDeveloper Oct 23 '14 at 13:42
  • 1
    Or you could buy a Mac, on which animated gifs seems to 'just work'. Encountered that myself in [another question](http://stackoverflow.com/a/10055387/1076463) – Robin Oct 23 '14 at 16:28
  • @DreadHeadedDeveloper *"Now perhaps you could put a bit more explanation?"* The best explanation I have access to is in the accepted answer to the linked question! (I really don't understand *why* `ImageIO` should ***fail*** to load an animated GIF correctly.) – Andrew Thompson Oct 23 '14 at 23:31