Jump to content
xisto Community
Sign in to follow this  
beeseven

More Efficient Way To Double Buffer

Recommended Posts

For a long time I used a BufferedImage/Graphics(2D) to double buffer my programs but someone recently pointed out something to me that is much more efficient: JFrames can automatically create buffers.

The way that I used to use was like this:

private BufferedImage image;private Graphics buffer;public NameOfJPanel() {	 image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);	 buffer = image.getGraphics();	 ...}public void paintComponent(Graphics g) {	 g.drawImage(image, 0, 0, null);}private class Time implements ActionListener {	 public void actionPerformed(ActionEvent e) {		  ...		  repaint();	 }}
It uses the methods paintComponent and repaint and two objects to double buffer the panel. The other way is just about as easy and much faster:
private static BufferStrategy bs = null;public static void main(String[] args) {	 JFrame f = new JFrame("Title");	 ...	 while(bs == null) {		  f.createBufferStrategy(2); //number of buffers, 2 is almost always enough		  bs = f.getBufferStrategy();	 }}//don't need to do anything in the JPanel constructorprivate class Time implements ActionListener {	 public void actionPerformed(ActionEvent e) {		  if(bs == null) //I put this in because you make the panel before the buffer			   return;		  Graphics g = bs.getDrawGraphics();		  ...		  bs.show();		  g.dispose();	 }}
I made a test program for each type of buffering that had a square bouncing around the screen. After one minute the BufferedImage version was at 379 fps and the BufferStrategy version was at 476 fps. The BufferStrategy also eliminated a weird thing where, when using the BufferedImage, you have to account for the title bar and edges of the frame when you set its size. I've attached both versions of the test (they're in text files because of upload restrictions). At the bottom left the numbers are (top to bottom) number of frames since the program was run, the number of seconds since the program was run, the number of frames per second.

Edit: I didn't realize that you don't actually need a JPanel to do to use this buffer method (though having one doesn't hurt). You can just have the main class extend JFrame and do everything in the constructor:
public class ClassName extends JFrame {	 public static void main(String[] args) {		  new ClassName();	 }	 public ClassName() {		  //set size, location, defaultcloseoperation, visible		  while(getBufferStrategy() == null)			   createBufferStrategy(2);		  //other stuff like add timer	 }	 private class Time implements ActionListener {		  public void actionPerformed(ActionEvent e) {			   Graphics g = getBufferStrategy().getDrawGraphics();			   //other stuff			   getBufferStrategy().show();			   g.dispose();		  }	 }}

Edited by beeseven (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.