Double Buffering

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1182 Views - Last Post: 04 February 2010 - 05:23 PM Rate Topic: -----

#1 HunterE30633  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-October 09

Double Buffering

Post icon  Posted 22 December 2009 - 02:19 PM

I have an applet that demonstrates some double-buffering. Theres a small problem though, it double buffers but not all that well. When theres a few images on the screen moving around such as in a game-like environment, the objects tend to flicker. The basic code that I use:
import java.awt.*;
import java.awt.image.*;
import java.applet.*;

public class Buffer extends Applet
{
	Image buffer = null;
	public void init()
	{

	}//end init
	public void paint(Graphics g)
	{
		g.clearRect(0,0,getWidth(), getHeight());
		//do work
	}//end
	public void update(Graphics g)
	{
		if(buffer == null || new Dimension(buffer.getWidth(this), buffer.getHeight(this)) != getSize())
		buffer = createImage(getWidth(), getHeight());
		paint(buffer.getGraphics());
		g.drawImage(buffer, 0, 0, this);
	}//end
	public void start()
	{
		new Thread(new Runnable()
		{
			public void run()
			{
				while(true)
				{
					System.out.println(x);
					repaint();
					try
					{
						Thread.sleep(10);
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}//end while
			}//end run
		}).start();
	}//end start
}//end class



Works fine if theres just a rectangle to draw, but flickers if theres a few images to draw. I was wondering if anyone knew a better way to double buffer or if theres something wrong in the code I have

Also, I've been searching the internet for a better way. I couldn't really find one, I'm just looking for a more direct response

This post has been edited by HunterE30633: 22 December 2009 - 02:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Double Buffering

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering

Posted 22 December 2009 - 03:03 PM

Swing is NOT Thread safe
use a javax.swing.Timer() instead of a Thread that will synchronize your data update and the drawing of it
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Double Buffering

Posted 22 December 2009 - 03:14 PM

To sort of build off what pbl is saying, you need to set a Timer in the init() for the frame rate. NumberOfMillies = 100/FPS. So if you want a frame rate of ~66, you would set the timer to go off every 15 milliseconds.
public class Buffer extends Applet
{
	Image buffer = null;
	Timer time;
	public void init()
	{
		 time = new Timer(15, this);
	}//end init
	...
	public void start()
	{
		time.start();
	}
  
	public void actionPerformed(ActionEvent e) {
		update(); // Update the image.
	}
}//end class



EDIT Fixed code tag... >.< OOH And my 1100th post woohoo!

This post has been edited by Dogstopper: 22 December 2009 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering

Posted 22 December 2009 - 03:35 PM

View PostDogstopper, on 22 Dec, 2009 - 02:14 PM, said:

To sort of build off what pbl is saying, you need to set a Timer in the init() for the frame rate. NumberOfMillies = 100/FPS. So if you want a frame rate of ~66, you would set the timer to go off every 15 milliseconds.

15 is far too much (too much meaning too often)
you will redraw 4 times in memory while the hardware will actually refresh your screen only once

I have posted 2 tutorials on how refresh the screen the fastest way as possible without flickering
One with a separate thread if your computation of new data takes more than 60 ms one without if you can redraw within 60 ms


Sorry you are right DogStopper
it is 60 times/second so about 15 ms

This post has been edited by pbl: 22 December 2009 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Double Buffering

Posted 22 December 2009 - 03:41 PM

View Postpbl, on 22 Dec, 2009 - 04:35 PM, said:

15 is far too much (too much meaning too often)
you will redraw 4 times in memory while the hardware will actually refresh your screen only once

I have posted 2 tutorials on how refresh the screen the fastest way as possible without flickering
One with a separate thread if your computation of new data takes more than 60 ms one without if you can redraw within 60 ms


Alright, I'll make the proper adjustments...Though it's always worked quite well for me.
Was This Post Helpful? 1
  • +
  • -

#6 HunterE30633  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-October 09

Re: Double Buffering

Posted 22 December 2009 - 03:45 PM

can you link to the tutorials?

@dog

Thanks for the timer suggestion, hopefully that will fix some of my problems
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering

Posted 22 December 2009 - 03:46 PM

View PostDogstopper, on 22 Dec, 2009 - 02:41 PM, said:

Alright, I'll make the proper adjustments...Though it's always worked quite well for me.

Do not say it won't work.... you will just draw 3 times out of 4 for nothing
some times, especially in games, you do not want this burn these extra CPU cycles that can be used somewhere else like for playing sound or update game status


Oups my bad
Sorry DogStopper

This post has been edited by pbl: 22 December 2009 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Double Buffering

Posted 22 December 2009 - 03:52 PM

View PostHunterE30633, on 22 Dec, 2009 - 04:45 PM, said:

can you link to the tutorials?

@dog

Thanks for the timer suggestion, hopefully that will fix some of my problems


pbl's tutorials.
http://www.dreaminco...topic113451.htm
http://www.dreaminco...topic113977.htm
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering

Posted 22 December 2009 - 04:01 PM

Oups my bad
I inversed the calculation
You were right DogStopper
Would you beleive I am programming games 8h00 a day for living :)
And I play with these refresh rates at least once an hour... time for vacations

This post has been edited by pbl: 22 December 2009 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Double Buffering

Posted 22 December 2009 - 04:35 PM

Ooooh...With C? That sounds...interesting. Personally, I write all of my games in Java, just to do it. Thanks for letting me know that it works; I was getting a little bit worried there...
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering

Posted 22 December 2009 - 05:31 PM

View PostDogstopper, on 22 Dec, 2009 - 03:35 PM, said:

Ooooh...With C? That sounds...interesting. Personally, I write all of my games in Java, just to do it. Thanks for letting me know that it works; I was getting a little bit worried there...

My mistake
Yes in C so we get an interrupt when the hardware finishes to refresh the screen
Was This Post Helpful? 0
  • +
  • -

#12 HunterE30633  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-October 09

Re: Double Buffering

Posted 25 December 2009 - 01:12 PM

Well using a canvas and creating a bufferstrategy doesn't work too well for me when I use them in applets. I use them all the time in JFrames but my gallery calls for applets. Whenever I try to create a canvas and create its bufferstrategy, theres always an IllegalStateException at the second line in the code:

Canvas c = new Canvas();
c.createBufferStrategy(2);
BufferStrategy strat = c.getBufferStrategy();


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering

Posted 25 December 2009 - 09:54 PM

The Canvas has to be displayed BEFORE you can create a buffer strategy on it
I guess that it has to do with the GUI buffers already created
Was This Post Helpful? 1
  • +
  • -

#14 HunterE30633  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-October 09

Re: Double Buffering

Posted 30 December 2009 - 08:40 AM

View Postpbl, on 25 Dec, 2009 - 08:54 PM, said:

The Canvas has to be displayed BEFORE you can create a buffer strategy on it
I guess that it has to do with the GUI buffers already created


Oh I see, so I would have to create the buffer strategy in the start method instead of the init method?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering

Posted 30 December 2009 - 02:30 PM

View PostHunterE30633, on 30 Dec, 2009 - 07:40 AM, said:

View Postpbl, on 25 Dec, 2009 - 08:54 PM, said:

The Canvas has to be displayed BEFORE you can create a buffer strategy on it
I guess that it has to do with the GUI buffers already created


Oh I see, so I would have to create the buffer strategy in the start method instead of the init method?

Depends when you actually add(theCanvas)
might even be in at the end of the paint() method the first time it is called
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2