Question

J'essaie de créer un ImageIcon à partir d'un gif animé stocké dans un fichier jar.

ImageIcon imageIcon = new ImageIcon(ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("animated.gif")));

L'image est chargée, mais uniquement la première image du gif animé. L'animation ne joue pas.

Si je charge le gif animé à partir d'un fichier sur le système de fichiers, tout fonctionne comme prévu. L'animation joue à travers toutes les images. Donc ça marche:

ImageIcon imageIcon = new ImageIcon("/path/on/filesystem/animated.gif");

Comment puis-je charger un gif animé dans un ImageIcon à partir d'un fichier jar?

EDIT: Voici un scénario de test complet. Pourquoi ne pas afficher l'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();
        }
    }
}
Était-ce utile?

La solution

Ceci lit l'animation gif de inputStream

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

Autres conseils

Vous devez utiliser getClass (). getResource (imgName); pour obtenir une URL vers le fichier image. Consultez ce didacticiel du guide pratique de Real.

EDIT: une fois l'image chargée, vous devez définissez la propriété ImageObserver pour que l'animation s'exécute.

Étant donné que ce fil venait juste d'être lié à un fil plus récent qui n'avait pas grand-chose à voir avec les GIF animés mais était traîné en OT, j'ai pensé ajouter cette source triviale qui fonctionne pour moi.

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

Espérons qu'il ne soit pas trop tard pour cela.

J'ai réussi à insérer le gif animé dans mon JPanel de la manière suivante:

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

Quelques points de cette approche:
1. Le fichier image se trouve dans le pot.
2. ImageIO.read () renvoie une image BufferedImage, qui ne met pas à jour ImageObserver;
3. Une autre alternative pour rechercher les images regroupées dans le fichier jar consiste à demander au chargeur de classes Java, le code qui a chargé votre programme, d’obtenir les fichiers. Il sait où sont les choses.

En faisant cela, j'ai pu insérer mon gif animé dans mon JPanel et cela a fonctionné comme un charme.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top