How Can I Avoid Flicker in an Applet?

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. The cookbook approach is simple. 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 (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);
}


FAQ Home

 
Printer friendly version of the FAQ-JAVA-How-Can-I-Avoid-Flicker-in-an-Applet page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Virtual File System SDK
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.