tags:

views:

193

answers:

5

I'm working on a java game. in which after some regular millisecond i'm repainting the screen. But if the timer duration becomes very less then in that case screen starts flickering.

Is there any way to avoid it??

Can we use Multithreading to avoid this???

+1  A: 

You could use double buffering.

Here a "double buffering with Java" tutorial: Double Buffering and Page Flipping.

Multithreading alone does not solve this problem...

Johannes Weiß
I have just heard of it.. that it avoid the flickering. But i don't know how to implement it. Can u suggest me how do i implement it in java?
Mew 3.2
Use the link I posted :-)
Johannes Weiß
A: 

when you repaint, you would be clearing the entire canvas before redrawing which appears as a flicker. if you could clear only the previous drawn parts of the canvas by superimposing the same shapes with background color, you could avoid the flickering. this is just one possible solution.

Aadith
+1  A: 

Check out this tutorial which explains how to use accelerated graphics mode and double buffering to avoid screen flicker. The key concept is to create a BufferStrategy to manage the double-buffering:

// create the buffering strategy which will allow AWT
// to manage our accelerated graphics
createBufferStrategy(2);
strategy = getBufferStrategy();

Then, during rendering you obtain the Graphics object from the buffer strategy, perform the rendering using Graphics and then call strategy.show().

// Get hold of a graphics context for the accelerated 
// surface and blank it out
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,800,600);

g.dispose();
strategy.show();
Adamski
+1  A: 

A technique which can be employed to reducing screen flickering in animation is to use double buffering. Double buffering is achieved by having an off-screen destination to which the drawing operations are performed, and only when the off-screen drawing is ready to be displayed will it be drawn to the physical screen.

Multithreading is not the solution, as the GUI is managed by a single thread in both AWT and Swing.

coobird
A: 

Be sure you are drawing on the EDT. An instance of javax.swing.Timer makes this easy, as the the action event handler executes on the EDT. I see no flickering with the default buffer strategy, even at high frame rates, as seen here.

trashgod