943,813 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7905
  • Java RSS
May 17th, 2004
0

Java Applet viewer blinks everytime the init method is used.

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Radahl is offline Offline
2 posts
since May 2004
May 18th, 2004
0

Re: Java Applet viewer blinks everytime the init method is used.

Quote originally posted by Radahl ...
... 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:

Java Syntax (Toggle Plain Text)
  1. public void update(Graphics g)
  2. {
  3. paint(g);
  4. }

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.
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help in thread handling pplease read
Next Thread in Java Forum Timeline: Memory buttons problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC