使用 Java Swing 从 sprite 表绘图的最佳性能选项

发布于 2024-08-23 13:11:29 字数 435 浏览 1 评论 0原文

我正在使用 Java 创建一款图形 Roguelike 游戏。在游戏中,我将 Tile 对象的二维数组绘制到 JPanel 上。这些 Tile 对象代表地面。我有一个 .bmp 精灵表,其中包含我想要用于绘画的所有纹理。每次玩家移动时,玩家可见的图块都需要重新绘制。

我的问题是一个性能问题。我过去已经实现了这一点,让 Tiles 扩展 JPanel,每个 Tile 仅使用 bufferedImage.getSubImage() 显示精灵表的适当部分,而父 JPanel 只需在所有 Tiles 上调用 Paint()二维数组。这对于之前项目中的小型 30x20 地图效果很好,但我不确定它是否适用于当前的游戏。

我应该使用相同的方法还是有其他可能的解决方案可以加快绘制时间? Tile 类是否应该扩展其他一些 Swing 或 AWT 组件(例如 BufferedImage),或者这不会产生效果?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-08-30 13:11:29

您可以在板上使用单个组件(而不是每个图块一个)并让它处理每个图块的绘制。单独的渲染器可以处理单独的图块绘制,使解决方案更加模块化。这与 JTable 及其单元格的呈现使用的方法类似。

不确定 getSubImage() 的性能,可能值得预先提取(和缓存)单个图像。

另请仅查看重新绘制发生变化的单元格(如果您还没有这样做)。

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

逆夏时光 2024-08-30 13:11:29

这款基于图块的游戏在大屏幕上运行良好:1900x1200 像素、60x36 图块。将图像缩放到目标矩形似乎消耗了大部分渲染预算。 此处有更多讨论。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文