|
|
|
|
![]() ![]() |
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; 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 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(); } } CODE private static BufferStrategy bs = null; 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.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(); } } 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 |
|
|
|
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
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 27th July 2008 - 01:49 AM |