# [java] Flicker in Applets. How to reduce?

## Recommended Posts

Hi Guys, I've made a racing game. I currently do all my drawing to a backbuffer then draw the backbuffer image in 1 draw during the paint method. I have the game capped at 15fps. Is there anything else I can use to stop the occasional flicker that seems to appear while drawing. Its much improved over what it was my using a backbuffer and capping the fps. But its no perfect.

##### Share on other sites
Have you tried using this: setIgnoreRepaint(true); works on most containers(JPanel, Applet, ect). should work fine in the containers initialization or just before your game loop starts.

The general problem(if you don't have that) would be that the JVM is thinking for you when to repaint all the components on the screen, out of sync from your game loop(and since you probably don't have any "normal" components to repaint it "flashes" to the background).

Depending on how you're drawing your environment though it might just give you black screen.

##### Share on other sites
mainLoop{//input//all game logic code/etc//drawEveryThingToBackBufferImage();repaint();thread.sleep(50-loopTimeTaken);}public void paint( Graphics g ){g.drawImage( backbuffer, 0, 0, this );}private void drawEveryThingToBackBufferImage(){//handle all drawrepaint();}

Thats pretty much how I handle everything.
Where should I ask the system to setIgnoreRepaint(true); ?

Am i barking up the wrong tree, by trying to make a good game in an applet? Maybe applets cant handle games like Flash(actionscript) can?

##### Share on other sites
You can use setIgn... anywhere, though calling it once would be best. It's just adjusting a flag to tell the JVM not automatically repaint.

Also only call repaint() once in your game loop instead of twice which is where some of the flickering is coming from as well.

And you can easily get Applets that run better and with more versatility than flash. Just a matter of the difficulty of Java is leagues greater to get to the same level, and you're going to work a lot harder for that versatility and power.

_______

Though using repaint at all is kind of a dead end, there are a lot of got'chas there.

I'd recomend using some form or another of active rendering: http://www.gamedev.net/reference/programming/features/javarender/ and it's resources explain quite a few things better.

##### Share on other sites
Quote:
 Original post by SetantaaAm i barking up the wrong tree, by trying to make a good game in an applet?

Kind of, read this. Calling repaint() basically says "please could you call the paint method on my applet at some point in the future". Using the active rendering method described in the link above, you are in complete control.

Also, if you have a look in the comments of that article, I found some ways to make the rendering really fast.

Regards
elFarto

##### Share on other sites
You can never eliminate display glitches if the underlying data
is changing without being completely synchronized to the display
generating thread. For example, if an object's location is being
changed and its X and Y coordinates are being changed, there
will be a time when X has changed but Y has not, resulting in
a flicker if the displayer happens to run through at that moment.

The simplest and most reliable way to do this is to have one thread
do both things. The "repaint" request shouldn't actually paint, just
set a flag that one is needed.

##### Share on other sites
Try putting setIgnoreRepaint(true) into the init method if you are extending an applet, otherwise put it in the constructor, and replace repaint() with paint(getGraphics()). I think repaint besides only scheduling the paint also calls update which fills in the background color, possibly causing a visible flickering.

[Edited by - zoto on August 17, 2009 11:26:33 PM]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978702

• 21
• 14
• 12
• 10
• 12