質問

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

アニメーション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とはほとんど関係のない最新のスレッドからリンクされただけで、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);
    }
}

うまくいけば、これで手遅れではない。

この方法で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.画像ファイルはjar内にあります;
2. ImageIO.read()は、ImageObserverを更新しないBufferedImageを返します;
3. jarファイルにバンドルされているイメージを見つける別の方法は、プログラムをロードしたコードであるJavaクラスローダーにファイルを取得するように依頼することです。物事がどこにあるかを知っています。

これを行うことで、JPanel内でアニメーションGIFを取得することができ、魅力的に機能しました。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top