Carregando gif animado do arquivo JAR no ImageIcon
Eu estou tentando criar um ImageIcon de um gif animado armazenado em um arquivo jar.
ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif")));
A imagem carrega, mas apenas o primeiro quadro do gif animado. A animação não é reproduzida.
Se eu carregar o gif animado de um arquivo no sistema de arquivos, tudo funcionará como esperado. A animação reproduz todos os quadros. Então isso funciona:
ImageIcon imageIcon = new ImageIcon("/path/on/filesystem/animated.gif");
Como posso carregar um gif animado em um ImageIcon a partir de um arquivo jar?
EDIT: Aqui está um caso de teste completo, por que isso não exibe a animação?
import javax.imageio.ImageIO;
import javax.swing.*;
public class AnimationTest extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AnimationTest test = new AnimationTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
});
}
public AnimationTest() {
super();
try {
JLabel label = new JLabel();
ImageIcon imageIcon = new ImageIcon(ImageIO.read(AnimationTest.class.getClassLoader().getResourceAsStream("animated.gif")));
label.setIcon(imageIcon);
imageIcon.setImageObserver(label);
add(label);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
}