Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Using the Repaint Manager to Manage Double Buffering

Double buffering is a screen drawing technique that performs multiple drawing operations to an offscreen buffer and then copies this buffer to the screen. The result is that a number of separate drawing operations will appear as a single smooth screen transition. This technique is essential for applications that do animation. The tradeoff for using double buffering is a slight decrease in graphics performance and increased memory usage to support the offscreen buffer.


Note:  
The repaint manager performs double buffering on a per-component basis—it does not render multiple unrelated components to the offscreen buffer before copying the buffer to the display. The repaint manager does render all of a component’s children to the offscreen buffer before copying the buffer to the display.

All Swing components are designed to support double buffering. There are two JComponent methods, setDoubleBuffered and isDoubleBuffered, that you can use to control double buffering at the component level. By default, double buffering is disabled for most components. The only components that are double buffered by default are the JPanel, JPopupMenu, and JRootPane components.


Note:  
The fact that most Swing components do not have double buffering enabled by default is somewhat misleading. If any ancestor of a component has double buffering enabled, the repaint manager will double buffer the child component. All children of a double buffered component are, in effect, double buffered themselves. Since JRootPane is the root of most containment hierarchies, it’s safe to say that all Swing components in a JFC application are double buffered.

Enabling and Disabling Double Buffering

The repaint manager provides global control for double buffering for all components owned by a thread group (application). The setDoubleBufferingEnabled method enables and disables double buffering. The isDoubleBufferingEnabled method returns a Boolean indicating whether double buffering is currently enabled or disabled. By default, the repaint manager enables double buffering. The following line of code illustrates how to enable double buffering with the repaint manager.

RepaintManager.currentManager(this).setDoubleBufferingEnabled(true);

In addition to setDoubleBufferingEnabled, the repaint manager also includes the isDoubleBufferingEnabled method to determine if double buffering is enabled for the application. By default, the repaint manager enables double buffering.


Note:  
You must disable double buffering for the “flash graphics” option to operate properly with debug graphics. See Chapter 25, “ToolTips and Debug Graphics,” for more information on using debug graphics.

Setting the Offscreen Buffer Size

Each repaint manager instance maintains a single offscreen buffer. Since there is only one repaint manager instance per application, all components in the application share the same buffer.

The repaint manager provides the getDoubleBufferMaximumSize and setDoubleBufferMaximumSize methods to get and set the maximum size of the offscreen buffer. Note that buffer size is not specified in bytes but is specified as a Dimension. The buffer itself is an Image, not just a block of memory. The default size of the buffer is the screen size of the display on which the application is running.

Actions

In the JFC toolkit, an Action encapsulates a bit of application functionality and information to identify the functionality. Standard identification information contained in an Action can be a string to be used in a menu or a ToolTip for the Action, as well as an Icon to be used in a toolbar. However, an Action is not limited to only containing this data. The Action interface defines the putValue and getValue methods for adding and retrieving any piece of information by using a key. The data is stored in the Action in a data structure conforming to the java.util.Dictionary class.

Creating a class that implements the Action interface allows a single bit of functionality to be easily added to multiple user interface controls. The Action interface is an extension of the ActionListener interface contained in the AWT. As such, a class that implements the Action interface can be used anywhere an ActionListener can be used. However, as we will see later in this book, Actions are integrated with menus and toolbars in the JFC. This integration allows Actions to be added directly to menus and toolbars. This integration allows applications built with Actions encapsulating functionality to easily create menu and keyboard interfaces.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.