VB icon

Avoid Flicker in an applet

Email
Submitted on: 1/1/2015 4:21:00 AM
By: Anonymous (from psc cd)  
Level: Not Given
User Rating: By 3 Users
Compatibility: Java (JDK 1.1)
Views: 2584
 
     The key to fixing flicker is realizing that the screen isn't actually painted in the paint() method. The pixels get put on the screen in the update() method which most applets don't override. However by overriding the update() method you can do all your painting in an offscreen Image and then just copy the final Image onto the screen with no visible flicker. (Java FAQ:found on the web at:http://sunsite.unc.edu/javafaq/javafaq.html)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Avoid Flicker in an applet
// Description:The key to fixing flicker is realizing that the screen isn't actually painted in the paint() method. The pixels get put on the screen in the update() method which most applets don't override. However by overriding the update() method you can do all your painting in an offscreen Image and then just copy the final Image onto the screen with no visible flicker.
(Java FAQ:found on the web at:http://sunsite.unc.edu/javafaq/javafaq.html)
// By: Anonymous (from psc cd)
//**************************************

Add the following three private fields to your applet and the public update() method. Flicker will magically disappear. 
 private Image offScreenImage;
 private Dimension offScreenSize;
 private Graphics offScreenGraphics;
 public final synchronized void update (Graphics g) {
Dimension d = size();
if((offScreenImage == null) || (d.width != offScreenSize.width) || (d.height != offScreenSize.height)) {
 offScreenImage = createImage(d.width, d.height);
 offScreenSize = d;
 offScreenGraphics = offScreenImage.getGraphics();
}
offScreenGraphics.clearRect(0, 0, d.width, d.height);
paint(offScreenGraphics);
g.drawImage(offScreenImage, 0, 0, null);
 }


Other 17 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.