1

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ama291
  • 70
  • 1
  • 10
  • This is [an XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You probably do not need to paint a gif. You need to _display_ a gif somehow. I recommend avoiding overriding `paint` unless it's really necessary. – Robin Green Jan 04 '14 at 17:36
  • possible duplicate of [Java Swing: how to add an image to a JPanel?](http://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel) – Robin Green Jan 04 '14 at 17:37
  • Well it's not a duplicate for sure. OP wants GIF not PNG or JPEG. – Tanmay Patil Jan 04 '14 at 17:47
  • possible duplicate of [Why gif animation doesn't animate when using it in paintComponent()?](http://stackoverflow.com/questions/11648696/why-gif-animation-doesnt-animate-when-using-it-in-paintcomponent) – ama291 Jan 04 '14 at 18:55
  • @peeskillet Your answer did not loop or play the GIF like I asked for. Also I figured out that this is a duplicate of: http://stackoverflow.com/questions/11648696/why-gif-animation-doesnt-animate-when-using-it-in-paintcomponent – ama291 Jan 04 '14 at 18:57
  • What do you mean loop? Is it an animated gif? And what don't you understand about those answer from that question? Did you try them? – Paul Samsotha Jan 04 '14 at 19:01
  • @peeskillet yeah it's animated, and the drawing method you showed me only drew the thumbnail/first frame. I found the solution of making the image an ImageIcon an then painting it using the .paintIcon() method. – ama291 Jan 04 '14 at 19:02

3 Answers3

1

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

user3041058
  • 1,520
  • 2
  • 12
  • 16
1

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

    }

}
Obicere
  • 2,999
  • 3
  • 20
  • 31
0

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.

Community
  • 1
  • 1
Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45