5

I have some gif animated images (see sample image below)
and when I draw them with the graphics object, I am getting only the first image,
I know I can do this with JLabel (from other stackoverflow answers)
but I want to do that with the graphics object,
can anyone please tell me
an easy way how to draw the whole animation?gif animation

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
java-love
  • 516
  • 1
  • 8
  • 23
  • Sure, take a look at [this example](http://stackoverflow.com/questions/22188940/gif-image-doesnt-moves-on-adding-it-to-the-jtabbed-pane/22190844#22190844) and take your pick of how much work you'd like to do ;) - Just remember, this is an EXAMPLE, it's not a complete solution – MadProgrammer Mar 07 '14 at 02:34
  • @MadProgrammer does it really have to be so much code for one gif? – java-love Mar 07 '14 at 02:38
  • Actually, you'd probably need more, this is essentially what's going on in the background with `JLabel` and `ImageIcon`, it's just nicely hidden away for you. This is also why the `ImageObserver` and `ImageProducer` interfaces are so important – MadProgrammer Mar 07 '14 at 02:52
  • See also [Show an animated BG in Swing](http://stackoverflow.com/q/10836832/418556). – Andrew Thompson Mar 07 '14 at 05:13
  • A simplified example can be found here : https://tutoref.com/how-to-display-an-animated-gif-in-java-swing/ – Mehdi Mar 26 '18 at 20:31

1 Answers1

14

"please tell me an easy way how to draw the whole animation?!"

It may have to to do with how you're reading in the image. If you use ImageIO.read, it won't work. If you read it as an ImageIcon it seems to work

ImageIcon.getImage()

Image icon = new ImageIcon(new URL("https://i.stack.imgur.com/KSnus.gif")).getImage();
...
g.drawImage(icon, 20, 20, this);

enter image description here

Image with ImageIO

Image icon = ImageIO.read(new URL("https://i.stack.imgur.com/KSnus.gif"));
...
g.drawImage(icon, 20, 20, this);

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720