Despite ImageIO
be used to upload images, and a gif also be an image, the problem of using this class for gifs is that it will only load the first frame.
To properly load the gif, you need to create a ImageIcon
, retrieve a guy Image
and move on to the method drawImage()
in the paintComponent()
of the panel or other component where you will draw the gif.
In addition, you also need to set an image observer to the drawImage
, it is he who will take care of the animation.
I made an example for you to see in practice:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class DrawGiFTest extends JComponent {
public void createAndShowGUI() {
JFrame window = new JFrame();
window.getContentPane().add(new GifPanel(loadImage("http://introcs.cs.princeton.edu/java/15inout/duke.gif")));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setPreferredSize(new Dimension(300, 300));
window.pack();
window.setVisible(true);
}
//Este método recebe uma string com a url da imagem e
//cria um ImageIcon e retorna um tipo Image, com o
//gif já carregador corretamente
private Image loadImage(String url){
ImageIcon icon;
try {
icon = new ImageIcon(new URL(url));
Image img = icon.getImage();
return img;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
}
class GifPanel extends JComponent{
Image image;
public GifPanel(Image image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 50, 50, this);
}
}
public static void main(String[] a) {
SwingUtilities.invokeLater(() -> {
new DrawGiFTest().createAndShowGUI();
});
}
}
The result:
In this case, I passed the this
, for the JComponent
implements a ImageObserver
. If drawing on any other component (such as a JPanel
, Jbutton, etc...), you can pass the this
also, because all swing components(except the top-level containers) inherit from JComponent
directly or indirectly.
References:
What error does it present?
– Isac
No, the problem is that when I put a file
.GIF
he simply draws all the images on top of each other, without the animation of GIF.– Lucas Caresia
Exactly this my question, how can I draw animations using the code I have now.
– Lucas Caresia
Returns
The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (BufferedImage, int, int, Map)
; Remembering that the code is in a class calledmap
.– Lucas Caresia
You must do this inside the paintcomponent, the
this
is to point to the own component where the gif will be drawn.– user28595
@Lucascarezia you erased the question, and I expected it to decrease a little the scope, for example, restricting to the swing API, which would already be able to answer. However, I was developing a solution based on this api, but you deleted it, so that you do not miss the opportunity of what has already been done, test it and see if it serves you: https://github.com/diegofelipem/stackoverflow/blob/master/src/swing/examples5/ChessBoardTest.java
– user28595
@Articuno It is that searching the internet I found something that served me, but I reopened the question for you answer.
– Lucas Caresia