views:

196

answers:

2

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.

+2  A: 

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).

objects
If I use a single component to the paint the tile, how would I go about repainting only the visible Tiles? Wouldn't I have to just call repaint() on the single component which would repaint the entire thing? Thanks for the suggestions.
Wes
You would define your model to store the details of the board, the component (view) would then get the details of each tile from the model. Adding the concept of a viewport (a view of part of the board) may also help break the problem down.
objects
+2  A: 

This tile-based game plays well on large screens: 1900x1200 pixels, 60x36 tiles. Scaling images to the destination rectangle seemed to consume most of the rendering budget. There's more discussion here.

trashgod
Thanks, I'll check it out.
Wes