本文介绍了为 Java Swing 创建合成图像输出的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 java swing 开发 2d 游戏,其中程序生成的环境由低分辨率图像的平铺数组表示.目前,这一切都在 JLabel 的 html 标签内(用于换行).

Im working on a 2d game in java swing, where a procedurally generated environment is represented by a tiled array of low-res images. Currently this is all within html tags (for line breaks) in a JLabel.

最重要的是,我想显示字符精灵,它不会占据整个图块,但会有一些 alpha 组件.过去,在 JS/html 和 JS/canvas 中,我只是简单地制作了带有 alpha 通道的精灵图块,并将它们直接放置在地砖上方,使用表格和/或绝对定位.

On top of this, I would like to display character sprites, which will not take up an entire tile, but will have some alpha components. In the past, in JS/html and JS/canvas, I have simply made sprite tiles with alpha channels and positioned them directly above a floor tiles, using tables and/or absolute positioning.

然而,在 java swing 中,绝对定位(以及大部分 html)似乎效果不佳.

In java swing, however, absolute positioning (and most of html for that matter) doesn't seem to work so well.

因为精灵永远不会在两个地砖之间/重叠,所以显而易见的解决方案是在游戏外制作一堆预先计算的图像文件——每个类型的地砖上的每个角色一个,但是,这会磁盘空间(和工作)随着使用的图像数量呈指数增长,似乎没有必要.

Since a sprite will never be between/overlapping two floor tiles, the obvious solution is to make a bunch of pre-computed image files outside the game -- one for each character over each type of floor tile, however, this would scale disk space (and work) exponentially with the number of images used, and seems like it shouldn't be necessary.

据我所知,应该可以在运行时将所有这些图像创建为 Bufferedimages,方法是在启动时预先计算它们,或者根据需要在每帧制作一些图像.

From what I've seen, it looks like it should be possible to create all these images in runtime as Bufferedimages, either by pre-computing them all at startup, or making some each frame as needed.

如果我能做到以上几点,我也许还可以将我需要的所有图像每帧放入一个大的位图中,然后显示出来.如果我使系统过载,这也可以减少帧的非同步加载.

If I can do the above, I might also be able to put all the images I need into one big bitmap each frame, and display that. This could also reduce non-synchronized loading of frames if I overload the system.

在上述选项中,哪个看起来最好?还是有另一个我完全失踪的明显解决方案?如果解决方案是制作合成图像,那么就如何做到这一点而言,有人可以指出我正确的方向吗?非常感谢.

Of the above options, what seems like the best? or is there another obvious solution that I am completely missing? If the solution is to make composite images, could someone point me in the right direction as far as how to do that? Thanks a lot.

推荐答案

这个主题需要整本书,但本质上,你可以做的是......

This topic would take entire book, but essentially, what you can do is...

创建一个自定义组件(从 JPanel 或 example 扩展),用于渲染世界视图,渲染多少取决于您的需要,但您只能渲染一小部分整个世界的子集.这很重要,因为如果您没有显示它,您就不想渲染它.

Create a custom component (extending from JPanel or example), which is used to render the view of the world, how much this renders will depend on your needs, but you could render only a small subset of the overall world. This is important, because if you it's not displayed, you don't want to render it.

在该组件的 paintComponent 方法中,您将渲染当前视图的各个层.

Within the paintComponent method of this component, you would render the various layers of the current view.

如果您使用png 图像,那么这就像计算视图和虚拟世界之间的位置并使用Graphics#drawImage 一样简单.如果您的模型得到了很好的优化,您甚至可以在单个渲染循环中实现这一点,也就是说,对于每个平铺"位置,您可以确定要在其中显示的内容并分别为每个平铺构建输出,而不是使用多个循环来渲染每一层.

If you use png images, then this is as simple as calculating the position between the view and the virtual world and using Graphics#drawImage. If you're model is well optimised, you could even achieve this within a single rendering loop, that is, for each "tile" position, you could determine what is to be displayed within in and build up the output for each tile individually, rather then using multiple loops to renderer each layer.

除了在 paintComponent 方法中渲染整个视图之外,您还可以使用后台缓冲区,该缓冲区是在屏幕外准备的,例如地面层,它可以简单地作为单个步骤进行绘制paintComponent 方法,这将有助于提高整体性能.

Instead of rendering the entire view within the paintComponent method, you could also use a backing buffer, which is prepared offscreen, such as the ground layer, which would simply be painted as a single step in the paintComponent method, this would help increase the overall performance.

根据您的需要,您可以根据需要或多或少地渲染此后备缓冲区...

Depending on you needs, you could render more or less to this backing buffer as your needs required...

查看执行自定义绘画了解更多详情

最佳实践将归结为关于整体引擎设计的更多背景.从性能的角度来看,你可以绘制的越少,它就会变得越快.

Best practices would come down to much more context about the overall engine design. From a performance point of view, the less you can paint, the faster it will become.

这篇关于为 Java Swing 创建合成图像输出的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:59