Java 2D 绘图最佳性能
我正在编写一个 Java 2D 游戏。 我正在使用内置的 Java 2D 绘图库,在我从 JFrame 中的 Canvas 的 BufferStrategy 获取的 Graphics2D 上进行绘图(有时是全屏的)。 BufferStrategy 是双缓冲的。 重新绘制是通过计时器主动完成的。 但我遇到了一些性能问题,尤其是在 Linux 上。
Java2D 有很多创建图形缓冲区和绘制图形的方法,我只是不知道我做的是否正确。 我一直在尝试使用graphics2d.getDeviceConfiguration().createCompatibleVolatileImage,这看起来很有希望,但我没有真正的证据表明,如果我将绘图代码切换到它,它会更快。
根据您的经验,在 Java 1.5+ 中将 2D 图形渲染到屏幕上最快的方法是什么? 请注意,游戏已经遥遥领先,因此我不想切换到完全不同的绘图方法,例如 OpenGL 或游戏引擎。 我基本上想知道如何获得使用 Graphics2D 对象将内容绘制到屏幕上的最快方法。
I'm in the process of writing a Java 2D game. I'm using the built-in Java 2D drawing libraries, drawing on a Graphics2D I acquire from a BufferStrategy from a Canvas in a JFrame (which is sometimes full-screened). The BufferStrategy is double-buffered. Repainting is done actively, via a timer. I'm having some performance issues though, especially on Linux.
And Java2D has so very many ways of creating graphics buffers and drawing graphics that I just don't know if I'm doing the right thing. I've been experimenting with graphics2d.getDeviceConfiguration().createCompatibleVolatileImage, which looks promising, but I have no real proof it it's going to be any faster if I switch the drawing code to that.
In your experience, what is the fastest way to render 2D graphics onto the screen in Java 1.5+? Note that the game is quite far ahead, so I don't want to switch to a completely different method of drawing, like OpenGL or a game engine. I basically want to know how to get the fastest way of using a Graphics2D object to draw stuff to the screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我已经使用 Java 完成了一些基本的绘图应用程序。 我没有做过任何图形密集型的工作,但我建议您很好地处理所有“重绘”调用。 对父容器进行额外的重绘调用可能会使您所做的渲染量增加一倍。
I've done some basic drawing applications using Java. I haven't worked on anything too graphic-intensive, but I would recommend that you have a good handle on all the 'repaint' invocations. An extra repaint call on a parent container could double the amount of rendering your doing.
我一直在关注这个问题,希望有人能给你比我更好的答案。
与此同时,我发现了以下Sun白皮书,其中是在 jdk 1.4 测试版发布后编写的。 这里有一些关于微调的有趣建议,包括运行时标志(在文章底部):
“Solaris 和 Linux 的运行时标志
从 SDK 的 Beta 3 版本(版本 1.4)开始当 DGA 不可用时,Java 2D 默认情况下将图像存储在像素图中,无论您是在本地还是远程显示环境中工作,您都可以使用 pmoffscreen 标志覆盖此行为:
-Dsun.java2d.pmoffscreen=true/false
如果设置。将此标志设置为 true,即使 DGA 可用,也会启用离屏像素图支持。禁用离屏像素图支持可以解决一些渲染问题。
I've been watching this question, hoping someone would offer you a better answer than mine.
In the meantime, I found the following Sun white paper which was written after a beta release of jdk 1.4. There are some interesting recommendations here on fine-tuning, including the runtime flags (at the bottom of the article):
"Runtime Flag For Solaris and Linux
Starting with the Beta 3 release of the SDK, version 1.4, Java 2D stores images in pixmaps by default when DGA is not available, whether you are working in a local or remote display environment. You can override this behavior with the pmoffscreen flag:
-Dsun.java2d.pmoffscreen=true/false
If you set this flag to true, offscreen pixmap support is enabled even if DGA is available. If you set this flag to false, offscreen pixmap support is disabled. Disabling offscreen pixmap support can solve some rendering problems. "
我认为,有一个重要的问题尚未提及:尽可能缓存所有图像。 如果您有一个在屏幕上移动的对象,并且其外观变化不大,请将其绘制到图像上,然后将图像渲染到屏幕上每帧的新位置。 即使对象非常简单,也要这样做 - 您可能会惊讶地发现节省了多少时间。 渲染位图比渲染图元快得多。
您可能还想考虑显式设置渲染提示,并在质量考虑允许的情况下关闭抗锯齿等功能。
There's an important one which hasn't been mentioned yet, I think: cache images of everything you can. If you have an object that is moving across the screen, and it's appearance doesn't change much, draw it to an image and then render the image to the screen in a new position each frame. Do that even if the object is a very simple one - you might be surprised at how much time you save. Rendering a bitmap is much faster than rendering primitives.
You might also want to look at setting rendering hints explicitly, and turning off things like antialiasing if quality considerations permit.
以下是我的一些想法。 如果您更具体以及您想要做什么,我可能可以提供更多帮助。 听起来像是一场游戏,但我不想假设。
只画你需要的! 不要总是盲目地调用 repaint(),尝试一些类似的方法,例如 repaint(Rect) 或 repaint(x,y,w,h)。
使用 Alpha 混合时要非常小心,因为混合图像/图元可能是一项昂贵的操作。
尝试尽可能多地预渲染/缓存。 如果您发现自己一遍又一遍地以相同的方式绘制圆形,请考虑绘制到 BufferedImage 中,然后只绘制 BufferedImage。 您正在为了速度而牺牲内存(典型的游戏/高性能图形)
考虑使用 OpenGL,使用 LWJGL 的 JOGL。 JOGL 更像 Java,而 LWJGL 在 OpenGL 访问之上提供更多游戏功能。 OpenGL 的绘制能力(使用适当的硬件和驱动程序)比 Swing 高出几个数量级。
Here are some tips off the top of my head. If you were more specific and what you were trying to do I may be able to help more. Sounds like a game, but I don't want to assume.
Only draw what you need to! Don't blindly call repaint() all of the time, try some of the siblings like repaint(Rect) or repaint(x,y,w,h).
Be very careful with alpha blending as it can be an expensive operation to blending images / primitives.
Try to prerender / cache as much as possible. If you find yourself drawing a circle the same way, over and over, consider drawing in into a BufferedImage and then just draw the BufferedImage. You're sacrificing memory for speed (typical of games / high perf graphics)
Consider using OpenGL, use JOGL of LWJGL. JOGL is more Java-like whereas LWJGL provides more gaming functionality on top of OpenGL access. OpenGL can draw orders of magnitude (with proper hardware and drivers) than Swing can.
我想我和你有同样的问题。 在这里查看我的帖子:
Java2D 性能问题
它显示了性能下降的原因以及如何修复它。 但不能保证它在所有平台上都能正常工作。 你会在帖子中看到原因。
I'm having the same issues as you are I think. Check out my post here:
Java2D Performance Issues
It shows the reason for the performance degradation and how to fix it. It's not guaranteed to work well on all platforms though. You'll see why in the post.
这篇文章的答案、Consty's 的答案以及我自己的研究的综合:
什么有效:
GraphicsConfiguration.createCompatibleImage
创建与您正在绘制的内容兼容的图像。 这是绝对必要的!-Dsun.java2d.opengl=True
来加快绘图速度。在我的测试中,使用这些方法,速度提高了 10 倍 - 15 倍,使正确的 Java 2D 图形成为可能。
A synthesis of the answers to this post, the answers to Consty's, and my own research:
What works:
GraphicsConfiguration.createCompatibleImage
to create images compatible with what you're drawing on. This is absolutely essential!Canvas.createBufferStrategy
.-Dsun.java2d.opengl=True
where available to speed up drawing.In my tests, using these methods, I got a speed increase of 10x - 15x, making proper Java 2D graphics a possibility.
有几件事您需要记住
1) 令人耳目一新 此链接展示了如何使用 Swingtimers,这可能是调用重绘的一个不错的选择。 弄清楚重新绘制(正如之前的海报所说,这很重要,因此您不必做太多工作)。
2)确保您只在一个线程中进行绘图。 从多线程更新 UI 可能会导致糟糕的事情。
3)双缓冲。 这使得渲染更加平滑。 此网站为您提供了更多有用的信息
There are couple of things you will need to keep in mind
1) is refreshing this link shows how to use swingtimers which might be a good option for calling repaint. Getting repaint figured out (as the previous posters have said is important so you're not doing too much work).
2) Make sure you're only doing drawing in one thread. Updating UI from mulitple threads can lead to nasty things.
3) Double Buffering. This makes the rendering smoother. this site has some more good info for you
确保使用双缓冲,首先绘制到内存中的一个大缓冲区,然后在所有绘制完成后刷新到屏幕。
make sure you use double buffering, draw first to one big buffer in memory that you then flush to screen when all drawing is done.
如果您不想使用纯 Java 2D,另一种选择是使用 GTGE 或 JGame 等游戏库(在 Google 上搜索它们),然后提供对图形的轻松访问,并提供双缓冲和更简单的绘图命令。
An alternative if you don't want to go pure Java 2D is to use a game library such as GTGE or JGame (search for them on Google), then offer easy access to graphics and also offer double buffering and much simpler drawing commands.