How can I create animated gif in Java?

MaXal picture MaXal · Mar 18, 2011 · Viewed 19.8k times · Source

I would like to create a gif image from the set of BufferedImages. How can I do this? Is there such library in pure Java (ImageMagick is not an option)? I've found Gif4J library but it's not royality-free.

Answer

Antonio picture Antonio · Apr 27, 2014

I just answer a similar question here, but I think that my solution can help.

'ImageIcon' class allows you to load gif animations. I load the image with 'getResource()'. For doing this I normally us URL class to pass the file path. The path does not need to be necessary in a remote machine as the name URL may suggest.

URL url = This.class.getResource(path);
Icon myImgIcon = new ImageIcon(url);
JLabel imageLbl = new JLabel(myImgIcon);
component.add(imageLbl, BorderLayout.CENTER);

path will be the path of the gif inside of the class folder.

References: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource