2008-09-29 40 views
9

我试图创建存储在一个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(); 
     } 
    } 
} 

回答

8

这从的inputStream

InputStream in = ...; 
Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(in)); 
6

您必须使用getClass()。getResource(imgName);获取图像文件的URL。从Real的HowTo中检查出this tutorial

编辑:一旦加载图像,你必须set the ImageObserver property让动画运行。

+0

我使用代码的第一部分从jar文件加载png图像就好了。而GIF加载 - 它只是没有动画。 – 2008-09-29 15:34:30

+0

我现在看到你的问题。查看我的编辑了解更多详情。 – 2008-09-29 16:19:25

+0

设置ImageObserver在我的情况下没有帮助。看来ImageIO没有正确读取动画GIF。如果我为ImageIcon使用不同的构造函数,它将起作用。我用一个完整的代码示例更新了这个问题。你需要在你的类路径中使用animated.gif。 – 2008-09-29 17:41:48

3

由于该线程是刚刚从一个较新的线程有一点做与GIF动画,但得到拖OT联系,我想我会添加这个平凡的来源,“对我的作品”。

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

class AnimatedGifInLabel { 

    public static void main(String[] args) throws Exception { 
     final URL url = new URL("http://i.stack.imgur.com/OtTIY.gif"); 
     Runnable r = new Runnable() { 
      public void run() { 
       ImageIcon ii = new ImageIcon(url); 
       JLabel label = new JLabel(ii); 
       JOptionPane.showMessageDialog(null, label); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
1

读取GIF动画但愿这不是太晚了这一点。

我设法让gif动画我的JPanel里面是这样的:

private JPanel loadingPanel() { 
    JPanel panel = new JPanel(); 
    BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS); 
    panel.setLayout(layoutMgr); 

    ClassLoader cldr = this.getClass().getClassLoader(); 
    java.net.URL imageURL = cldr.getResource("img/spinner.gif"); 
    ImageIcon imageIcon = new ImageIcon(imageURL); 
    JLabel iconLabel = new JLabel(); 
    iconLabel.setIcon(imageIcon); 
    imageIcon.setImageObserver(iconLabel); 

    JLabel label = new JLabel("Loading..."); 
    panel.add(iconLabel); 
    panel.add(label); 
    return panel; 
} 

这种方法的几点:
1.图像文件是罐子内;
2. ImageIO.read()返回一个BufferedImage,它不更新ImageObserver;
3.查找捆绑在jar文件中的图像的另一种方法是要求Java类加载器(加载程序的代码)获取文件。它知道事情在哪里。

因此,通过这样做,我能得到我的JPanel在我的gif动画和它的工作就像一个魅力。