0

I am trying to display an ImageIcon (an animated .gif image file) on my canvas in Java. When I draw the ImageIcon like this:

this.character = new ImageIcon("resources/test.gif");

public void render(Point position, GuiComponent gui, Container observer) {
    Graphics graphics = gui.getGraphics();
    character.paintIcon(observer,  graphics, position.x, position.y);
}

the gif only loops once, while I would like to get my gif loop indefinitely. Does anyone know how to do this?

JasperTack
  • 4,337
  • 5
  • 27
  • 39
  • You need to call character.paintIcon(observer, graphics, position.x, position.y); in a loop on a new thread. – StackFlowed Sep 16 '14 at 21:25
  • You could use a JLabel, which will do it automatically, but the question lacks context to know what solution would be best to suggest – MadProgrammer Sep 16 '14 at 21:34
  • @Aeshang and how do I know at which intervals I have to redraw the icon? or can you only do this by manually installing a specific timeout? – JasperTack Sep 16 '14 at 21:51
  • @MadProgrammer the animated gif is an image of a player in a platform game and is redrawn in each repaint operation. – JasperTack Sep 16 '14 at 21:52
  • 1
    You should be able to repeatedly call paintIcon, from my limited experimentation, as long as your refreshing at least at the GIFs frame rate it should paint fine – MadProgrammer Sep 16 '14 at 21:57
  • 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) – StackFlowed Sep 16 '14 at 22:21
  • @Aeshang I indeed have found the answer for showing the gif in that topic, but when I use the .paintIcon the gif only loops once. My question is how to get it looping all the time. – JasperTack Sep 17 '14 at 11:08

1 Answers1

0

On the newly created icon try using:

icon.getImage().flush();

and then do

label1.setIcon(icon); 
StackFlowed
  • 6,664
  • 1
  • 29
  • 45