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?
Asked
Active
Viewed 1.8k times
5

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 Answers
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);
Image with ImageIO
Image icon = ImageIO.read(new URL("https://i.stack.imgur.com/KSnus.gif"));
...
g.drawImage(icon, 20, 20, this);

Paul Samsotha
- 205,037
- 37
- 486
- 720
-
Or, if you're really crazy (or mad), you can roll your own, like the example linked in the comments to OQ :P – MadProgrammer Mar 07 '14 at 02:53
-
@MadProgrammer maybe you should change you SN to CrazyProgrammer :P – Paul Samsotha Mar 07 '14 at 02:55
-
-
@peeskillet thanks, it works but i have a funny situation, when I'm drawing the image like your example, the other image i load from my computer is also drawing, but when i comment it out the first one isn't drawing,**: (** – java-love Mar 07 '14 at 03:11
-
@java-love Wait, so it's _not_ one gif file? You want to animate multiple files into one animated image? – Paul Samsotha Mar 07 '14 at 03:13
-
no i have the same file on my computer, and when i copy your code into the same class i have 2, *1+1 = 2*,**:)** – java-love Mar 07 '14 at 03:16
-
@java-love Can you post a simple runnable example replicating this. I may not be understanding what's happening. – Paul Samsotha Mar 07 '14 at 03:18
-
-
@java-love click the edit link under your post. And _below_ your current post description, just write **EDIT** and post your code below it. – Paul Samsotha Mar 07 '14 at 03:22
-
Let’s make it simple, how do I load the image from my computer and not from the internet? – java-love Mar 07 '14 at 03:22
-
1@java-love OH ok, `new ImageIcon(getClass().getResource("/resources/image.gif")).getImage();` where `resources` is in your `src`. – Paul Samsotha Mar 07 '14 at 03:24
-