BlockBreak brick flicker

Hi, this is my first Java 2D game I’m working on. My problem is that, although I’m using a BufferedImage, I can’t seem to make the flicker of my bricks go away. I don’t really know where to put the brick code. Right now the brick code is called within the update(Graphics g){} method (in an Applet). I know why it’s flickering but I don’t know how to fix it. Thanks so much for any help. Here’s the code:

	public void drawBrick(){

		for(int i = 0; i < 5; i++){
			
			for(int j = i * 10; j < brick.length; j++){
				
				g2d.setTransform(identity);
				g2d.translate(brick[j].getPos().getX(), brick[j].getPos().getY());
				
				//Drawing the bricks
				
				g2d.setColor(colorArr()[i]);
				g2d.fill(brick[j].getShape());
				g2d.setColor(Color.BLACK);
				g2d.draw(brick[j].getShape());
				
			}
		}
	
		
	}

If that’s not enough, let me know. Thanks.

If you are double buffering, then flicker could be caused by simple low FPS. I know shape drawing/filling is pretty slow, what speeds are you getting?

EDIT: Also, applets are dead. Really, 99% of people can’t run them without extensive troubleshooting, myself included.

Oh that might be it, but I was running it at 20 ms. How do I switch over? I originally tried to write it in a JFrame, but I couldn’t figure out how to update the screen. Not entirely sure how JFrame works so that’ll be my next project, I guess.

Don’t really understand what you mean, “running at 20 ms.” Is that each cycle takes 20 ms (50 FPS) or you sleep 20 ms each cycle (??? < 50 FPS)

Mind posting your game loop?

public void run() {
		
		Thread t = Thread.currentThread();
		while(t == gameloop){
			try{
				game.gameUpdate(width, height, ball, brick, board);
				
				Thread.sleep(20);
			}
			catch(InterruptedException e){
				e.printStackTrace();
			}
			repaint();
		}

	}

Yeah, ok. That loop has an unconstrained (thus irregular) period, which manifests as stuttering, and since the maximum speed at which it can run is (only) 50 Hz, it may appear as flicker.

To achieve a constant period you do something like this:


desiredPeriod = 1000 / 60; // 60 FPS in milliseconds

loop {
    timeBefore = currentTime();
    
    doStuff();
    renderStuff();
 
    timeElapsed = currentTime() - timeBefore; // final minus intial = delta

    wait(desiredPeriod - timeElapsed); // wait the remaining time, until total loop time is equal to desiredPeriod
}

In actual code:


long period = 1000000000L / 60; // in nanoseconds

while(running) {
    long time = System.nanoTime();

    gameUpdate();
    
    try {
        while (System.nanoTime() - time < period)
            Thread.sleep(1);
    } catch (InterruptedException e) {}

}

Some don’t like the wait loop, but with a sleep(1) inside of it I’ve found it’s about as smooth as one can get without burning up the CPU.

Converting from an applet to a JFrame is easy, although there are a few tips:

Instead of extending Applet, extend JComponent (or JPanel) and put everything from your init() into the constructor. Also make sure you setPreferredSize() in the constructor, so that this will behave as you want:


// in main():

JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(gamePanel); // your "applet"
frame.pack(); // resize the frame around your gamePanel, depends on setPreferredSize() in gamePanel

// optional, centers the frame on screen:
Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(ss.width / 2 - frame.getWidth() / 2, ss.height / 2 - frame.getHeight() / 2);

// finally:
frame.setVisible(true);

Thanks so much! Huge help! :slight_smile:

Why don’t you just replace
“while (System.nanoTime() - time < period)
Thread.sleep(1);”
with
“Thread.sleep( (System.nanoTime() - time)/1000000);” ?
(divide by 1000000 to convert nanoseconds to milliseconds)

Wouldn’t that have the same effect without the need for a loop and repeated calls to System.nanoTime()?

Thread.sleep doenst actually sleep the amount of time you really want to, so better sleep by the minimal amount of time and check yourself if you neet to wait another ms or not.
But i think Thread.yield() would do it even better then Thread.sleep(1);

Look at my code that handles Display.sync(…) in LWJGL.

It balances Thread.sleep(ms) and Thread.yield(), using self-monitoring, for a good balance of high accuracy and low CPU load.