如何在 Java2D 中进行双缓冲?
我正在屏幕上使用 Java2D 绘制一堆基元,并且出现很多撕裂/闪烁。
如何启用/使用双缓冲,以便将其绘制出屏幕然后显示整个内容?
I'm drawing a bunch of primitives using Java2D on the screen and I'm getting a lot of tearing/flicker.
How can I enable/use double-buffering so it draws it off screen then shows the whole thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缓冲图像使用与您要渲染到的屏幕相同的模型。
检查如何使用传递给任何组件的paint方法的Graphics2D创建BufferedImage(有很多方法可以创建缓冲图像,这链接了一些...)
[http://www.exampledepot.com/egs/ java.awt.image/CreateBuf.html][1]
您获取与缓冲图像关联的 Graphics [ getGraphics() ],如果需要,将其转换为 Graphics2D,然后通过调用命令将图元渲染到缓冲图像在该图形对象上(也可以将该图形对象传递给组件以在缓冲图像上绘制自身)。
您可以通过重写组件的paint方法并在传递给组件的Graphics2D参数上调用drawImage()的变体,将缓冲的图像绘制到组件中。
The buffered image uses the same model as the screen you're rendering to.
Check this for how to create a BufferedImage using the Graphics2D passed to the paint method of any component (there are many ways to create buffered images, this links a few...)
[http://www.exampledepot.com/egs/java.awt.image/CreateBuf.html][1]
You get the Graphics [ getGraphics() ] associated with the buffered image, cast it to Graphics2D if you need, and render your primitives to the buffered image by invoking commands on that graphics object (can also pass that graphics object to components to paint themselves on your buffered image).
You draw the buffered image to your component by overriding it's paint method and calling a variant of drawImage() on the Graphics2D argument passed to the component.