如何在Java2D中进行双缓冲? [英] How do I do double buffering in Java2D?

查看:98
本文介绍了如何在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?

推荐答案

1)您创建了一个BufferedImage实例.为了获得最佳性能,您需要确保 缓冲的图像使用与要渲染到的屏幕相同的模型.

1) You create a BufferedImage instance. For max performance you want to make sure that The buffered image uses the same model as the screen you're rendering to.

检查此方法以了解如何使用传递到任何组件的paint方法的Graphics2D创建BufferedImage(有很多方法可以创建缓冲的图像,这链接了一些...)

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]

2)获得与缓冲图像关联的Graphics [getGraphics()],如果需要,将其强制转换为Graphics2D,并通过在该图形对象上调用命令来将基本体呈现到缓冲图像(也可以传递该图形对象组件以将其自身绘制在缓冲的图像上.

2) 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).

3)通过覆盖缓冲的图像的paint方法,并在传递给组件的Graphics2D参数上调用drawImage()的变体,可以将缓冲的图像绘制到组件上.

3) 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.

lmk,如果您需要示例代码...

lmk if you need sample code...

这篇关于如何在Java2D中进行双缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆