java 将 JAR 文件中的动画 gif 加载到 ImageIcon
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/149153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading animated gif from JAR file into ImageIcon
提问by Steve K
I'm trying to create a ImageIcon from a animated gif stored in a jar file.
我正在尝试从存储在 jar 文件中的动画 gif 创建一个 ImageIcon。
ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif")));
The image loads, but only the first frame of the animated gif. The animation does not play.
图像加载,但只有动画 gif 的第一帧。动画不播放。
If I load the animated gif from a file on the filesystem, everything works as expected. The animation plays through all the of frames. So this works:
如果我从文件系统上的文件加载动画 gif,一切都会按预期进行。动画播放所有帧。所以这有效:
ImageIcon imageIcon = new ImageIcon("/path/on/filesystem/animated.gif");
How can I load an animated gif into an ImageIcon from a jar file?
如何从 jar 文件将动画 gif 加载到 ImageIcon 中?
EDIT: Here is a complete test case, why doesn't this display the animation?
编辑:这是一个完整的测试用例,为什么不显示动画?
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();
}
}
}
采纳答案by Penkov Vladimir
This reads gif animation from inputStream
这从 inputStream 读取 gif 动画
InputStream in = ...;
Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(in));
回答by Bill the Lizard
You have to use getClass().getResource(imgName); to get a URL to the image file. Check out this tutorialfrom Real's HowTo.
你必须使用 getClass().getResource(imgName); 获取图像文件的 URL。从 Real 的 HowTo 中查看本教程。
EDIT: Once the image is loaded you have to set the ImageObserver propertyto get the animation to run.
编辑:加载图像后,您必须设置 ImageObserver 属性才能运行动画。
回答by Andrew Thompson
Since this thread was just linked from a more current thread that had little to do with animated GIFs but got dragged OT, I thought I'd add this trivial source that 'works for me'.
由于该线程只是从与动画 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);
}
}
回答by Paulo Pedroso
Hopefully it's not too late for this.
希望现在还为时不晚。
I managed to get the animated gif inside my JPanel this way:
我设法通过这种方式在我的 JPanel 中获取了动画 gif:
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;
}
Some points of this approach:
1. The image file is within the jar;
2. ImageIO.read() returns a BufferedImage, which doesn't update the ImageObserver;
3. Another alternative to find images that are bundled in the jar file is to ask the Java class loader, the code that loaded your program, to get the files. It knows where things are.
这种方法的一些要点:
1. 图像文件在 jar 内;
2. ImageIO.read() 返回一个BufferedImage,不更新ImageObserver;
3. 另一种查找捆绑在 jar 文件中的图像的方法是询问 Java 类加载器,即加载您的程序的代码,以获取这些文件。它知道东西在哪里。
So by doing this I was able to get my animated gif inside my JPanel and it worked like a charm.
所以通过这样做,我能够在我的 JPanel 中获得我的动画 gif,它就像一个魅力。

