尽快清除透明的 BufferedImage

发布于 2024-08-23 12:40:10 字数 639 浏览 21 评论 0原文

我有一个使用以下代码创建的透明 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 技术交流群。

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

发布评论

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

评论(2

紧拥背影 2024-08-30 12:40:10

明白了:)使用clearRect而不是用透明颜色填充。

            graphics = (Graphics2D) offlineBuffer.getGraphics();
            graphics.setBackground(new Color(255, 255, 255, 0));
            Rectangle screen = transformationContext.getScreen();
            graphics.clearRect(0,0, (int)screen.getWidth(), (int)screen.getHeight());

Got it :) used clearRect instead of fill with a transparent color.

            graphics = (Graphics2D) offlineBuffer.getGraphics();
            graphics.setBackground(new Color(255, 255, 255, 0));
            Rectangle screen = transformationContext.getScreen();
            graphics.clearRect(0,0, (int)screen.getWidth(), (int)screen.getHeight());
顾冷 2024-08-30 12:40:10

一种相对较快的方法,但我不知道它是否是最快的(我想看到其他答案)是拥有另一张你从未修改过的图片,并且始终“完全清除”/“完全透明”,然后你做了一个光栅副本,假设你将该副本命名为“CLEAR”:

imageYouWantToClear.setData( CLEAR.getRaster() );

请注意,在性能方面,处理图形可能非常棘手,因为有很多没有很好记录的行为。例如,您的图像(例如 CLEAR 图像)可能是硬件加速的,但是一旦您使用变异方法(例如 setRgb( ))并且事实证明很难意识到您刚刚失去了硬件加速的好处。

我认为查找有关高性能 BufferedImage 主题的信息的最佳位置是 Java 游戏程序员和 Java 游戏 API 程序员社区/论坛。

顺便说一句,请确保您的 BufferedImage 都使用“兼容”模式: TYPE_INT_ARGB 在 Windows 上可能没问题,但在 OS X 等上则不然。因此您想创建它们,执行以下操作:

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);

哎呀德米特定律很痛,谢谢 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:

imageYouWantToClear.setData( CLEAR.getRaster() );

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:

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);

Ouch the Law-of-Demeter hurts, thanks Java ;)

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