Playing an Animated GIF
I am trying to play an animated gif in my little maze game. It's the player's gif, which should play an animation to make it look like the player has spawned into the game (thus it should only play once).
So i have stored it in a BufferedImage, drawn it and sent the panel as the imageObserver, but it only puts the first frame of the gif and doesnt go on to the next..
In the code, that is:
And in the paintComponent (along with many other things being painted):
String a = System.getProperty("user.dir");
File dir = new File(a + "\\player2_mfmaze.gif");
plr = ImageIO.read(dir);
Any idea? Or maybe i should just use an lib to do the job? (Suggest one if thats the case)
g.drawImage(plr, (int) (playerX * blocksize+1),
(int) (playerY * blocksize+1),
(int) (blocksize-1), this);// this == JPanel
Thanks in anticipation!!
Try one of the following:
1) Load the image using Toolkit instead of ImageIO.
2) Construct an ImageIcon and call the paintIcon method from your paintComponent override.
If neither suggestion solves the problem, post back and I'll take another look.
Using Image instead of Buffered Image and Toolkit instead of ImageIO worked.
I guess the buffered image or imageIO is for only one static frame of a gif.
Thank you for your help! :)