Reputation: 70
I'm trying to draw a GIF using the Java Graphics API, but I'm not able to successfully draw a GIF using my below code. Only the first image or thumbnail of the GIF is drawn but it doesn't play.
public void paintComponent(Graphics g){
super.paintComponent(g);
BufferedImage img = null;
try {
URL url = new URL("GIF URL");
img = ImageIO.read(url);
} catch (Exception e) {
}
g.drawImage(img, 5, 5, this);
}
Essentially I am creating graphics for a login screen and I want to draw a GIF that loops.
EDIT: Updated my code and changed the question a bit.
Upvotes: 1
Views: 6478
Reputation: 3019
It's perfectly possible to do this, you just need to have a proper way to load the frames for the image. The code I use to do this, is as so:
private static Image load(final String url) {
try {
final Toolkit tk = Toolkit.getDefaultToolkit();
final URL path = new URL(url); // Any URL would work here
final Image img = tk.createImage(path);
tk.prepareImage(img, -1, -1, null);
return img;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
This uses the Toolkit
to load a gif image, since ImageIO
can't properly load gifs at this time, if I recall correctly.
From there, it's so simple as doing the following in a (for example) JPanel
:
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g); // clear up render
//...
g.drawImage(IMAGE, x, y, this); // ImageObserver necessary here to update
//...
}
Example:
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class GifAnimation {
public GifAnimation(){
JFrame frame = new JFrame("Gif Animation");
GifPanel panel = new GifPanel(load("http://www.thisiscolossal.com/wp-content/uploads/2013/01/3.gif"));
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private static Image load(final String url) {
try {
final Toolkit tk = Toolkit.getDefaultToolkit();
final Image img = tk.createImage(new URL(url));
tk.prepareImage(img, -1, -1, null);
return img;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
new GifAnimation();
}
}
}
public class GifPanel extends JPanel {
private final Image image;
public GifPanel(Image image){
this.image = image;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image, 10, 10, this);
}
@Override
public Dimension getPreferredSize(){
return new Dimension(660, 660);
}
}
}
Upvotes: 1
Reputation: 1548
You can load a gif into a BufferedImage object. Then we paint the buffered image onto your swing component
Also one must better override the paintComponent method
Upvotes: 1
Reputation: 7057
GIF animation is not directly possible by using paint method of JPanel.
I would suggest inserting a JEditorPane in the panel whenever you want to display it and show GIF in it using HTML.
Refer showing images on jeditorpane (java swing)
Although some might criticize it as a crude way, the animation works perfectly.
Hope this helps.
Upvotes: 0