beeseven 0 Report post Posted June 17, 2006 (edited) 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 June 28, 2006 by beeseven (see edit history) Share this post Link to post Share on other sites
peterrogers 0 Report post Posted January 24, 2008 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 Share this post Link to post Share on other sites