I l@ve RuBoard |
![]() ![]() |
28.4 Painting and RepaintingRepainting is a fundamental task for a graphical application, but one that is rarely explained in the detail you'd expect for something so central. This section is intended to give you a better feel for how painting and repainting via the repaint manager and JComponent work. You typically do not need to get involved with the RepaintManager class, and only the extremely brave override it. However, there are some instances in which a firm understanding of the repaint manager can help avoid confusion, and, starting with SDK 1.4, Swing provides a way for savvy code to take advantage of accelerated graphics hardware when it is available. 28.4.1 Swing ResponsibilitiesRecall that Swing uses lightweight components, which are drawn inside heavyweight top-level containers. Since the operating system has no knowledge of lightweight components (that's what makes them lightweight), it can't help coordinate their repainting. To continue the analogy first presented in the earlier lightweight and heavyweight discussion, Swing is responsible for painting and repainting everything inside its own easels. Swing delegates this duty to a RepaintManager class, which organizes and schedules repainting when told to do so. 28.4.2 The RepaintManager ClassThe RepaintManager class is responsible for keeping track of the components (and the components' parts) that have become dirty, which means that they need to be repainted. Note that the "dirty region" does not necessarily include the entire region of affected components, but often only portions of them. The RepaintManager is also charged with the second responsibility of revalidating components that have been marked invalid. Both responsibilities ultimately result in the same thing: redrawing the component. There is only one RepaintManager per thread group. Like the FocusManager class, you don't instantiate one directly. Instead, the static methods currentManager( ) and setCurrentManager( ) retrieve and set the current repaint manager, respectively. (Note that there is no RepaintManager.getCurrentManager( ) method.) Once a repaint manager is activated for a thread group, it remains active until it is replaced or that thread group is shut down. You typically access the current manager as follows: RepaintManager rm = RepaintManager.currentManager( ); At the heart of the RepaintManager are two data structures: a Hashtable of component references and their rectangular regions that need to be repainted, and a Vector of invalidated components. You can add component regions to the hashtable of dirty regions with the addDirtyRegion( ) method. Likewise, you can add a component to the invalidation vector with a call to addInvalidComponent( ) . If you wish to remove a component from the invalidation vector, you can remove it with a call to removeInvalidComponent( ). Here are some important rules for working with the repaint manager:
You can get the current dirty region for each component by using the getDirtyRegion( ) method. If you want to mark the entire component as dirty, forcing a complete redraw on its next paint, use the markCompletelyDirty( ) method. You can check to see if an entire component's region is marked as dirty with the isCompletelyDirty( ) method if this will let your paint operation run more efficiently because it doesn't need to worry about optimizing or clipping its drawing operations. To remove a component's dirty region from the list, use the markCompletelyClean( ) method. The RepaintManager class is equipped with a double-buffering mechanism, which it provides as a service to all JComponent objects. By default, it is enabled for all components that wish to take advantage of it, unless the operating system is already performing double-buffering natively (as Mac OS X does). Many built-in components use double-buffering effectively for smooth drawing. You can manually override the operation of this feature using the setDoubleBufferingEnabled( ) method, but there should be no reason to do this because its default value is optimized for the graphics environment. The maximum size of the double buffer is, by default, the size of the entire screen. If for some reason you need to, you can alter it by calling setDoubleBufferMaximumSize( ). To find out the current setting, use getDoubleBufferMaximumSize( ). SDK 1.4 introduced a new VolatileImage class that allows components to take advantage of accelerated graphics hardware for extremely efficient double-buffering. Because using such hardware presents some special conditions that don't arise with normal Image buffers, components must explicitly request them via the getVolatileOffscreenBuffer( ) method. It's worth learning how to use this method (and the VolatileImage class) because of the potential performance boost it can give your components. 28.4.2.1 Key methodsThe Swing repaint manager works closely with a few key methods in JComponent: paint( ) , repaint( ), revalidate( ), and paintImmediately( ). A call to repaint( ) at the component level results in a region being added to the dirty list queue and, consequently, scheduled for repainting. This sets up a work request that is placed in the system event queue. Once the work request is processed, the current RepaintManager calls back the paintImmediately( ) method of the component. This method finally renders the dirty portion of the component (without delay, as it's running in the event dispatch thread). The revalidate( ) method in JComponent adds the component to the RepaintManager invalidation queue. At that point, the revalidation of the component and its ancestors, if necessary, is sent as a work request to the system event queue. Once the request is dequeued, the method calls the validate( ) method of the appropriate container, recomputing its layout to account for the changes that prompted the revalidation request. Figure 28-10 shows the RepaintManager at work repainting dirty regions and revalidating invalid components. Figure 28-10. Repainting and revalidating with JComponent and the RepaintManager![]() 28.4.2.2 Methods
|
I l@ve RuBoard |
![]() ![]() |