我试图创建存储在一个jar文件中的gif动画一个的ImageIcon。加载动画从JAR文件GIF到ImageIcon的
ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif")));
图像加载,但只是动画gif的第一帧。动画不播放。
如果我从文件系统中的文件加载gif动画,按预期工作的一切。动画播放所有帧。所以这个作品:
ImageIcon imageIcon = new ImageIcon("/path/on/filesystem/animated.gif");
如何从jar文件加载动画gif到ImageIcon中?
编辑:这里是,为什么不这样显示的动画完整的测试案例?
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();
}
}
}
我使用代码的第一部分从jar文件加载png图像就好了。而GIF加载 - 它只是没有动画。 – 2008-09-29 15:34:30
我现在看到你的问题。查看我的编辑了解更多详情。 – 2008-09-29 16:19:25
设置ImageObserver在我的情况下没有帮助。看来ImageIO没有正确读取动画GIF。如果我为ImageIcon使用不同的构造函数,它将起作用。我用一个完整的代码示例更新了这个问题。你需要在你的类路径中使用animated.gif。 – 2008-09-29 17:41:48