绘制缓冲图像时 Java 2D 的性能问题
我正在设计一个 Canvas 对象,用于绘制大小为 228x262 像素的 BufferedImage。
该图像是使用 Graphics2D.drawImage(...) 方法绘制的。我正在给定的偏移范围内进行基于像素的颜色操作。下面的代码示例:
for( int i = frameOffset; i < colorClock; i++ ) {
rgb[i] = new Color(this.colorBK).getRGB();
}
其中 rbg 设置为我要更改的缓冲图像。
问题是代码绘制速度很慢。
我使用 GraphicsConfiguration.createCompatibleImage 创建图像,并通过缓冲区策略使用双缓冲。
请问有灯吗?
谢谢你的补充。
I'm designing a Canvas object which is been used to draw a BufferedImage of size 228x262 pixels.
That image is been drawn using Graphics2D.drawImage(...) method. I'm doing a pixel basis color manipulation within given offset ranges. A sample of the code below:
for( int i = frameOffset; i < colorClock; i++ ) {
rgb[i] = new Color(this.colorBK).getRGB();
}
Where rbg is set to that bufferedimage I'm changing in.
The problem is that code is painting slow.
I'm creating the image using GraphicsConfiguration.createCompatibleImage, and I'm using double buffering via Buffer Strategy.
Any lights please?
Thanks on adv.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果每次绘制图像时都运行循环,则循环可能是瓶颈。有一个完全不必要的对象分配,这将使垃圾收集器频繁运行。
我假设 colorBK 是 int 。如果是这种情况,您只需创建并初始化一个 Color 对象,并要求它返回分配给 rgb 数组的 rgb 值。实际发生的情况是,您在 rgb 数组中分配 colorBK 的值。因此,等效且更有效的实现是 rgb[i] = colorBK。
为了进一步优化这一点,您可以将 colorBK 的值分配给最终的局部变量。这将避免一遍又一遍地获取字段的值。因此,循环可能如下所示:
为了获得更多性能增益,您应该考虑是否有完全不同的方法来执行此操作。由于上面的示例只是将一些像素更改为特定颜色,我可以假设这可以通过图像和几个 fillRect 来完成。
因此,您可以用所需的颜色填充图像后面的矩形(在本例中为 colorBK)。如果图像在这些区域中具有透明像素,则上述循环会发生变化,它们在画布中保持不变,并获得相同的效果。这可能会更有效,因为图形方法得到了更好的优化,并且不涉及大量的数组使用。
If you run the loop every time you draw the image, the loop might be the bottleneck. There is an completely unnecessary object allocation which will make the garbage collector to run quite often.
I'm assuming that colorBK is int. If this is the case, you just create and initialize a Color object and ask it to return a rgb value that is assigned to rgb array. What actually happens is that you assign the value of colorBK in the rgb array. So, equivalent and more efficient implementation would be rgb[i] = colorBK.
To optimize this even more, you could assign the value of colorBK to a final local variable. This would avoid fetching the value of the field over and over again. So the loop could look like this:
To get even more performance gain, you should think that if there is completely different ways of doing this. As the above example just changes some pixels to certain color, I could assume that this could be done with an image and a couple of fillRects.
So you would fill a rect behind the image with the color you want (in this case colorBK). If the image has transparent pixels in those areas the above loop changes they remain unchanged in the canvas and the same effect is gained. This might be more efficient as the graphics methods are better optimized and does not involve heavy array usage.
不要只是为了为图像中的每个像素提取 RGB 整数而创建新颜色。我能为 Color 找到的唯一一个单参数构造函数是一个采用 int RGB 的构造函数 - 你能不能直接使用 colorBK 吗?
另外,如果你对每种油漆都进行这种转换,速度会很慢;您应该只需要进行一次转换。
Don't create a new Color just to extract an RGB integer for every pixel in your image. The only single parameter constructor I can find for Color is one that takes an int RGB - can you not just use colorBK directly?
Also, if you are doing this conversion on every paint that will be slow; you should only need to do the conversion once.