I am writing a 2D, tile-based game in Java. Characters are to be placed on the tiles according to the game logic. Each Character is associated with a picture that would be drawn to the screen using java.awt.Graphics2D
. However, there could be more than one instance of each character on the map.
What is the recommended practice in storing and accessing the images of the characters?
There are three possible implementations I could think of:
- Having each Character assigned a private variable of type
java.awt.image.BufferedImage
. Each time the object needs to be drawn, the image is accessed using a getter. However, would this cause the size of the object to be excessively large? - Having another utility Class to store all of the images in a static variable. Each time the character needs to be drawn, the image can be called by passing an id/name to a static getter method in the utility Class. Would this be more efficient or would it be rather slower than the previous method?
- Simply load the picture each time the character needs to be drawn. This would provoke
ImageIO.read(new File(character.getImagePath())
every time the character needs to be drawn. Could this be the fastest?