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);

enter image description here

Image with ImageIO

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

enter image description here

Share:
15,443
java-love
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, 2022

Comments

  • java-love
    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?gif animation

  • MadProgrammer
    MadProgrammer about 10 years
    Or, if you're really crazy (or mad), you can roll your own, like the example linked in the comments to OQ :P
  • Paul Samsotha
    Paul Samsotha about 10 years
    @MadProgrammer maybe you should change you SN to CrazyProgrammer :P
  • Paul Samsotha
    Paul Samsotha about 10 years
    I'll just be LazyProgrammer :)
  • java-love
    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
    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
    java-love about 10 years
    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,:)
  • Paul Samsotha
    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
    java-love about 10 years
    where can i post code?
  • Paul Samsotha
    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
    java-love about 10 years
    Let’s make it simple, how do I load the image from my computer and not from the internet?
  • Paul Samsotha
    Paul Samsotha about 10 years
    @java-love OH ok, new ImageIcon(getClass().getResource("/resources/image.gif")).ge‌​tImage(); where resources is in your src.
  • java-love
    java-love about 10 years
    thanks for saving me from MadProgrammer's answer,: )