swing 中的 BufferedImage imageType
我正在编写一个小部件,它会进行一些缓存,以避免每次重绘时对一堆形状进行不必要的 Shape.draw 调用。
我尝试做这样的事情(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)
我不知道该选择哪种图像类型。有没有一种方法可以在运行时询问“正确的”?
I'm writing a widget that does some caching to avoid unecessary calls to Shape.draw on a bunch of shapes at every repaint.
I've tried to do something like this (scala code):
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)
}
I'm not sure what to put in (1)
and (2)
. null
for (2) seems to work (I want no transformation). But for (1)
I have no idea which image type to chose. Is there a way of asking for the "right" one at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Tedil 指出的,
g.getDeviceConfiguration().createCompatibleImage( width, height, Transparency.OPAQUE)
就可以解决这个问题。As Tedil pointed out,
g.getDeviceConfiguration().createCompatibleImage( width, height, Transparency.OPAQUE)
does the trick.