2008-09-29 39 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動畫和它的工作就像一個魅力。