How do I do double buffering in Java2D?

纵饮孤独 提交于 2019-12-12 10:49:19

问题


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:


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




回答2:


Some tips to use buffering effectively as a programmer view

and some testing programs are avaliable




回答3:


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][1]

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



来源:https://stackoverflow.com/questions/1587170/how-do-i-do-double-buffering-in-java2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!