Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> More Efficient Way To Double Buffer
beeseven
post Jun 17 2006, 08:35 PM
Post #1


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



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:
CODE
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:
CODE
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 constructor
private 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:
CODE
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();
          }
     }
}


This post has been edited by beeseven: Jun 28 2006, 04:14 AM
Go to the top of the page
 
+Quote Post
peterrogers
post Jan 24 2008, 04:03 PM
Post #2


Newbie
*

Group: Members
Posts: 1
Joined: 24-January 08
Member No.: 56,774



Hi, just a quick note to say thanks for your quick explanation and code for bufferStategy. I have added to my app and it is really fast without the hideous flickering that was driving me mad. Appreciate it. Pete R
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Auto Run Java Program(11)
  2. How To Double Stroke Images(22)
  3. Yet Another Signature Rotator(7)
  4. Double Leg Takedown(7)
  5. [photoshop Cs] Simple Double Layer Tech Border(0)
  6. Double Monitor Question(15)
  7. Ftp Double-click Caution(5)
  8. Assist Breast Cancer Research(3)
  9. Dialup Users Double Your Connection Speed(6)
  10. Want To Double Your Money Online(1)
  11. What Is... The Double Slit Experiment(6)
  12. [forum] Double Posting Happening By An Error(3)
  13. Buffer Overflow In Action Tutorial(0)
  14. Double Dating(6)
  15. Php Frameworks - Leaner, More Efficient Coding.(1)
  1. Double Xp, Then Dual Boot?(1)
  2. How To Double Kick Bass Drum(2)
  3. Double Dropdown(2)
  4. Stop Double Post/submit(10)
  5. Pump It Up(0)
  6. Religious Double Standards(14)
  7. Double Standard :yes For Kosovo But Not For Kurdestan(0)
  8. Most Efficient Code To Get Prime Numbers(7)
  9. Photoshop -- Double Stroke(2)
  10. Vista & Linux Double Boot(8)
  11. Conserving Battery Power(3)
  12. How To Make A Double-voice Effect(1)
  13. Manage Double Posting In Php(4)


 



- Lo-Fi Version Time is now: 27th July 2008 - 01:49 AM