简体   繁体   中英

Best performance option for drawing from a sprite sheet using Java Swing

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM