4

I plan to have an animated character (the character's image changing multiple times to make it appear to be moving), and I would like to know the best way 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, missing a few things, but I'm sure you get what I mean. Are there any problems doing it this way? (Note: the for loop would repeat again straight after finishing, and there would be files called "picture0.png", "picture1.png", etc. in the "images" folder)

Filburt
  • 17,626
  • 12
  • 64
  • 115

1 Answers1

4

If the images are not huge and don't require a lot of memory for storing them, I would rather read the images first and cache them. When they need to be displayed, I would read them from memory rather than from disk.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 1
    How would I go about doing this? Create BufferedImage objects of the images when the program starts? –  Apr 04 '13 at 17:04
  • Ok, thanks, I'll store all the loaded images in an array and draw those. –  Apr 04 '13 at 17:07