BufferStrategy
and BufferCapabilities
BufferStrategy
and BufferCapabilities
BufferStrategy
A buffer strategy gives you two all-purpose methods for drawing: getDrawGraphics and show. When you want to start drawing, get a draw graphics and use it. When you are finished drawing and want to present your information to the screen, call show. These two methods are designed to fit rather gracefully into a rendering loop:
BufferStrategy myStrategy; while (!done) { Graphics g = myStrategy.getDrawGraphics(); render(g); g.dispose(); myStrategy.show(); }
BufferCapabilities
As mentioned before, different operating systems, or even different graphics cards on the same operating system, have different techniques available at their disposal. These capabilities are exposed for you so that you can pick the best technique for your application.
The class java.awt.BufferCapabilities encapsulates these capabilities. Every buffer strategy is controlled by its buffer capabilities, so picking the right ones for your application is very crucial. To find out what capabilities are available, call the getBufferCapabilities method from the GraphicsConfiguration objects available on your graphics device.
The capabilities available in Java 2 Standard Edition version 1.4 are:
isPageFlipping
isFullScreenRequired
isMultiBufferAvailable
getFlipContents
FlipContents.COPIED
FlipContents.BACKGROUND
FlipContents.PRIOR
FlipContents.UNKNOWN
Once a particular buffer strategy has been created for a component, you can manipulate it using the getBufferStrategy method. Note that this method is also only available for canvases and windows.
Some tips about using buffer capabilities and buffer strategies:
- Getting, using, and disposing a graphics object are more robust in a try...finally clause:
BufferStrategy myStrategy; while (!done) { Graphics g; try { g = myStrategy.getDrawGraphics(); render(g); } finally { g.dispose(); } myStrategy.show(); }- Check the available capabilities before using a buffer strategy.
- For best results, create your buffer strategy on a full-screen exclusive window. Make sure you check the isFullScreenRequired and isPageFlipping capabilities before using page-flipping.
- Don't make any assumptions about performance. Tweak your code as necessary, but remember that different operating systems and graphics cards have different capabilities. Profile your application!
- You may want to subclass your component to override the createBufferStrategy method. Use an algorithm for choosing a strategy that is best suited to your application. The FlipBufferStrategy and BltBufferStrategy inner classes are protected and can be subclassed.
- Don't forget that you may lose your drawing surfaces! Be sure to check contentsLost and contentsRestored before drawing. All buffers that have been lost have to be redrawn when they are restored.
- If you use a buffer strategy for double-buffering in a Swing application, you probably want to turn off double-buffering for your Swing components, since they will already be double-buffered. Video memory is somewhat valuable and should only be used whenever absolutely necessary.
- It may be end up being wasteful to use more than one back buffer. Multi-buffering is only useful when the drawing time exceeds the time spent to do a show. Profile your application!