views:

280

answers:

3

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?

A: 

Check out this tutorial about double buffering: http://gpwiki.org/index.php/Java:Tutorials:Double_Buffering

flash
+1  A: 

Some tips to use buffering effectively as a programmer view

and some testing programs are avaliable

Paniyar
A: 

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.

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%5D%5B1%5D

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) 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 if you need sample code...

Thibaud de Souza