How to draw a gif animation in java?
15,443
"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("http://i.stack.imgur.com/KSnus.gif")).getImage();
...
g.drawImage(icon, 20, 20, this);
Image with ImageIO
Image icon = ImageIO.read(new URL("http://i.stack.imgur.com/KSnus.gif"));
...
g.drawImage(icon, 20, 20, this);
Author by
java-love
Learned java my self by books, Programming for fun or for friends, I apologise if my English is not correct (it’s not my native language),
Updated on June 10, 2022Comments
-
java-love almost 2 years
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? -
MadProgrammer about 10 yearsOr, if you're really crazy (or mad), you can roll your own, like the example linked in the comments to OQ :P
-
Paul Samsotha about 10 years@MadProgrammer maybe you should change you SN to CrazyProgrammer :P
-
Paul Samsotha about 10 yearsI'll just be LazyProgrammer :)
-
java-love about 10 years@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,: (
-
Paul Samsotha about 10 years@java-love Wait, so it's not one gif file? You want to animate multiple files into one animated image?
-
java-love about 10 yearsno i have the same file on my computer, and when i copy your code into the same class i have 2, 1+1 = 2,:)
-
Paul Samsotha about 10 years@java-love Can you post a simple runnable example replicating this. I may not be understanding what's happening.
-
java-love about 10 yearswhere can i post code?
-
Paul Samsotha about 10 years@java-love click the edit link under your post. And below your current post description, just write EDIT and post your code below it.
-
java-love about 10 yearsLet’s make it simple, how do I load the image from my computer and not from the internet?
-
Paul Samsotha about 10 years@java-love OH ok,
new ImageIcon(getClass().getResource("/resources/image.gif")).getImage();
whereresources
is in yoursrc
. -
java-love about 10 yearsthanks for saving me from MadProgrammer's answer,: )