How do I make an image animation?

I plan on having an animated character (the character image changes several times to make it seem moving), and I would like to know how to do it. I am currently planning to do something like this:

String fileLocation = "./images/picture"; BufferedImage img; int numImages = 10; for(int i = 0; i < numImages; i++){ img = ImageIO.read(new File(fileLocation + i + ".png")); Thread.sleep(100); g.drawImage(img, 0, 0, null); } 

This is an incredibly simplified version, skipping a few things, but I'm sure you understand what I mean. Are there any problems with this? (Note: the for loop will be repeated again after completion, and in the "images" folder there will be files called "picture0.png", "picture1.png", etc.)

+4
source share
1 answer

If the images are not huge and do not require large memory to store them, I would prefer to read the images first and cache them. When they should be displayed, I will read them from memory, not from disk.

+5
source

All Articles