1
我正在编写一个小部件,它会执行一些缓存操作,以避免在每次重绘时对Shape.draw进行不必要的调用。BufferedImage imageType in swing
我一直试图做这样的事情(Scala代码):
private val buffer = new BufferedImage(width, height, /* (1) */)
...
override def paintComponent(Graphics2D g) = {
if (hasChanged) {
val bg = buffer.getGraphics.asInstanceOf[Graphics2D]
bg.draw(/* ... */)
buffer.flush
}
g.drawImage(buffer, null /* (2) */, 0, 0)
}
我不知道要放什么东西在(1)
和(2)
。 (2)的null
似乎工作(我不想改造)。但对于(1)
我不知道要选择哪种图像类型。有没有在运行时要求“正确”的方法?
取决于 - 你需要缓冲的图像是透明的吗?如果没有,BufferedImage.TYPE_INT_RGB可能没问题。 您可能还想创建一个“兼容”图像。参见'BufferedImage img = g.getDeviceConfiguration()。createCompatibleImage( width,height,Transparency.OPAQUE);' 其中g是Graphics2D的一个实例 – Tedil 2010-08-11 14:05:53
我尝试过不同的图像类型,其中TYPE_INT_RGB,但我最多只能得到黑白图像。用CompatibleImage,我得到一个黑色的图像。 [编辑]用TYPE_INT_ARGB我得到一个丰富多彩的图像,但这将在每个平台上都是一样的吗? – 2010-08-11 14:17:27
它也适用于'createCompatibleImage',但是“setBackground”没有绘制背景,所以我认为它是黑色的,谢谢! – 2010-08-11 14:23:52