you can try below code, it is loading gif image with animation.
I think you need only this part of the code:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("gif.gif").getFile());
Image image = Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file)));
ImageIcon icon = new ImageIcon(image);
Full code example added below:
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
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();
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("gif.gif").getFile());
Image image =
Toolkit.getDefaultToolkit().createImage(org.apache.commons.io.IOUtils.
toByteArray(new FileInputStream(file)));
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
icon.setImageObserver(label);
add(label);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
}
added same code with jave project to github, you can get full code from there.
loading gif image to java with animation
Additionally, you need to use below apache commons dependency for the sample code
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>