I'm currently working on a program that's like target practice. You literally hit...targets! However, the constant blinking that occurs when the repaint method is called has become quite annoying! :mad: I have a Timer method coupled with the ActionListener to update the screen. The need for the update is because I move the targets by pixels so when the ActionListener is called, it changes the coordinates of the targets. Because pixels are small, the repaint method is called about 100 times per second. This is supposed to make the targets move seemlessly throughout the Applet; however, the applet blinks so bad that my eyes hurt! Looking at recent projects in my AP Comp Sci class, not one person has been able to avoid this problem, not even in the old C++ programs. If anyone has a solution to offer, it would most greatly be appreciated. Happy Programming! :)

... the constant blinking that occurs when the repaint method is called has become quite annoying! ...

Yes it is, especially if you're epileptic.

Quick explanation: When you call repaint, that ends up placing a call to update, which does a fillrect to clear the drawing area and then calls paint. To get rid of the clearing on each frame, you have to override the update method of your applet (or whatever class your drawing surface lives in) so it simply calls your paint method:

public void update(Graphics g)
{
     paint(g);
}

This stops the flicker, but creates more work for you. If you don't clean up the previous frame, your targets will leave slimy trails everywhere. For moving objects, there's usually substantial overlap between frames, so you can draw the new object over most of the old one and only erase the part that's left over. If you look closely enough, you'll be able to see this happening, but it's nowhere near as noticeable as the flicker problem you're having.

If you want it really smooth, use a double buffer. Have a look at java.awt.Image; you can draw to an off-screen graphics context for each frame and zap it into the applet once you're finished. Then flicker doesn't matter because you'll never actually see the drawing operations happening--you only get the final image each frame.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.