문제

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");

Anqualited GIF를 JAR 파일에서 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();
        }
    }
}
도움이 되었습니까?

해결책

InputStream에서 GIF 애니메이션을 읽습니다

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

다른 팁

getClass (). getResource (imgname)를 사용해야합니다. 이미지 파일에 URL을 얻으려면 체크 아웃 이 튜토리얼 Real의 Howto에서.

편집 : 이미지가로드되면 ImageObserver 속성을 설정하십시오 애니메이션이 실행됩니다.

이 스레드는 애니메이션 GIF와 거의 관련이없는 더 최신 스레드에서 연결되어 있었지만 구약을 끌었으므로 '나를 위해 작동하는 사소한 소스를 추가 할 것이라고 생각했습니다.

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);
    }
}

바라건대 이것은 너무 늦지 않았습니다.

나는이 방법으로 내 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;
}

이 접근법의 일부 요점 :
1. 이미지 파일은 항아리 안에 있습니다.
2. ImageIO.Read () imageObserver를 업데이트하지 않는 BufferedImage를 반환합니다.
3. JAR 파일에 번들로 연결된 이미지를 찾는 또 다른 대안은 프로그램을로드 한 코드 인 Java 클래스 로더에게 파일을 가져 오도록 요청하는 것입니다. 상황이 어디에 있는지 알고 있습니다.

그래서이 작업을 수행함으로써 나는 JPANEL 내에서 애니메이션 GIF를 얻을 수 있었고 매력처럼 작동했습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top