How to draw GIF with Image and drawImage

Asked

Viewed 368 times

1

I need to draw a GIF, I have the following code:

public BasicBlock(String path, String name, int id, boolean gif){
    this.path = path;
    this.name = name;
    this.id = id;   

    File img = new File(path);
    try {
        image = ImageIO.read(img);
    } catch (IOException e) {}
}

public static BufferedImage getBlockById(int id){
    return blocks[id].getImage();
}

g.drawImage(Blocks.getBlockById(id), x, y, null);
  • What error does it present?

  • 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.

  • Exactly this my question, how can I draw animations using the code I have now.

  • 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 called map.

  • You must do this inside the paintcomponent, the this is to point to the own component where the gif will be drawn.

  • @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

  • @Articuno It is that searching the internet I found something that served me, but I reopened the question for you answer.

Show 2 more comments

1 answer

2


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:

inserir a descrição da imagem aqui

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:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.