I'm creating a graphical roguelike game using Java. In the game, I'm painting a 2d array of Tile objects onto a JPanel. These Tile objects represent the ground. I have a .bmp sprite sheet that contains all of the textures I want to use to paint with. Every time the player moves, the tiles that are visible to the player need to be redrawn.
My question is a performance question. I have implemented this in the past to where I have the Tiles extend JPanel and each Tile just displays the appropriate segment of the sprite sheet using the bufferedImage.getSubImage(), while the parent JPanel simply calls paint() on all of the Tiles in the 2d array. This worked fine for the small 30x20 maps in the previous project, but I'm not sure it would work on the current game.
Should I use the same approach or is there some other possible solution that will speed up draw times? Should the Tile class extend some other Swing or AWT component such as BufferedImage or will that not have an effect?
Thanks.
You could use a single component for the board (instead of one per tile) and have it handle painting each tile. A separate renderer could handle individual tile painting to make the solution more modular. This is a similar approach used by a JTable and the rendering of its cells.
Not sure on the performance of getSubImage(), might be worth extracting (and caching) the individual images up front.
Also look at only repainting cells that change (if you're not already).