尽快清除透明的 BufferedImage
我有一个使用以下代码创建的透明 BufferedImage(我认为与它的创建方式无关):
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
Rectangle screen = transformationContext.getScreen();
// Create an image that supports transparent pixels
return gc.createCompatibleImage((int) screen.getWidth(), (int) screen.getHeight(),
Transparency.BITMASK);
如何以最快的方式清除图像(与创建时状态相同的空图像)而不重新创建图像?重新创建映像会给 GC 带来负担,暂停 VM 并冻结 UI。
I have a transparent BufferedImage created with the following code(not relevant how it is created, I think):
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
Rectangle screen = transformationContext.getScreen();
// Create an image that supports transparent pixels
return gc.createCompatibleImage((int) screen.getWidth(), (int) screen.getHeight(),
Transparency.BITMASK);
How do I clear the image(empty image in the same state as it was created) in the fastest way possible without recreating the image? Recreating the image puts a burden on GC, pausing the VM and freezing the UI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
明白了:)使用clearRect而不是用透明颜色填充。
Got it :) used clearRect instead of fill with a transparent color.
一种相对较快的方法,但我不知道它是否是最快的(我想看到其他答案)是拥有另一张你从未修改过的图片,并且始终“完全清除”/“完全透明”,然后你做了一个光栅副本,假设你将该副本命名为“CLEAR”:
请注意,在性能方面,处理图形可能非常棘手,因为有很多没有很好记录的行为。例如,您的图像(例如 CLEAR 图像)可能是硬件加速的,但是一旦您使用变异方法(例如 setRgb( ))并且事实证明很难意识到您刚刚失去了硬件加速的好处。
我认为查找有关高性能 BufferedImage 主题的信息的最佳位置是 Java 游戏程序员和 Java 游戏 API 程序员社区/论坛。
顺便说一句,请确保您的 BufferedImage 都使用“兼容”模式: TYPE_INT_ARGB 在 Windows 上可能没问题,但在 OS X 等上则不然。因此您想创建它们,执行以下操作:
哎呀德米特定律很痛,谢谢 Java ;)
One relatively fast way, but I don't know if it's the fastest (and I'd like to see other answers) is to have another picture that you never modify and that is always "fully cleared" / "fully transparent" and then you do a raster copy, say you named that copy CLEAR:
Note that working with graphics can be very tricky when it comes to performances because there are a lot of not-very-well-documented behavior. For example your images (say the CLEAR one) may be hardware-accelerated but you'd then lose hardware-acceleration as soon as you'd use a mutating method (like say a setRgb()) and it would prove very difficult to realize that you just lost the benefit of hardware acceleration.
I think that the best place to find infos on the subject of performant BufferedImage would be in the Java game-programmers and Java game-API-programmers community/forums.
Btw make sure that both your BufferedImage are using the 'compatible' mode: TYPE_INT_ARGB may be fine on Windows but not on OS X, etc. so you want to create them doing something like:
Ouch the Law-of-Demeter hurts, thanks Java ;)