Java double buffering technology to eliminate flickering images

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.