Flickering occurs when you use the canvas component to refresh the display of pictures or drawings. This is because the mechanism is to first use the background color to overwrite the original content, and then draw new content to the above caused by. The flicker we see is the alternating display between the background color and the new content.
The following is the code for the update () method in the canvas
public void Update (Graphics g) {g.clearrect (0, 0, width, height); Paint (g); }
As can be seen from the above, each time the update, the original content is filled with the background color, and then to draw.
The following uses double buffering technology to eliminate flicker. Principle: The content to be refreshed before the refresh is ready in memory, the refresh does not fill the background color, the contents of the memory directly drawn out. This will overwrite the update () method.
The following is the rewritten update () method code:
Class Graph extends Canvas{private image buffer;//declaration picture buffer public void paint (Graphics g) {//Paint code}public void Update (graph ICS g) {Buffer=createimage (GetWidth (), getheight ());//Create Picture Buffer graphics gbuffer=bufferimage.getgraphics ();// Gets the picture buffer of the brush if (gbuffer!=null) paint (Gbuffer); Elsepaint (g); Gbuffer.dispose (); G.drawimage (bufferimage, 0, 0,null);}}
This article is from the "Flying Fish Technology" blog, please be sure to keep this source http://flyingfish.blog.51cto.com/9580339/1621430
Java double buffering technology to eliminate flickering images