开发者

BufferedImage imageType in swing

I'm writing a widget that does some caching to avoid unecessary calls to Shape.draw on a bunch of shapes at every rep开发者_StackOverflow社区aint.

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?


As Tedil pointed out, g.getDeviceConfiguration().createCompatibleImage( width, height, Transparency.OPAQUE) does the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜