27.4 Rendering Odds and Ends
Both the JTree and JTable
classes make use of
cell renderers to display cells and cell
editors to modify cell values. The following classes round out the
utilities available for rendering information.
27.4.1 The CellRendererPane Class
This
utility class was built to keep renderers from propagating
repaint( ) and validate( )
calls to the components using renderer components such as
JTree and JList. If you played
around with creating your own renderers for any of the Swing
components that use them, you'll recall that you did
not use this class yourself. Normally this pane is wrapped around the
renderer and the various paintComponent( ) methods
below are used to do the actual drawing. Developers do not normally
need to worry about this class.
27.4.2 The Renderer Interface
The Swing package includes a
Renderer
interface (which does not appear to
be used within Swing itself) with the following methods:
- public Component getComponent( )
-
Return a Component you can use with something like
the SwingUtilities.paintComponent( ) method to
draw the value on the screen.
- public void setValue(Object aValue, boolean isSelected)
-
This method can initialize the rendering component to reflect the
state of the object aValue.
This interface could be useful if you were to create a library of
renderers for use with your own applications; however, it is not
implemented anywhere in the Swing package as of SDK 1.4.1.
27.4.3 The GrayFilter Class
The GrayFilter
class is an extension of the
java.awt.image.RGBImageFilter class. This class
contains a static method that returns a
"disabled" version of an image
passed in. The image is converted to a grayscale version, and some
lighter parts of the image are amplified to ensure the image is
recognizable. All of the components that can display images use this
class to present a default disabled version of the image if an
explicit disabled image is not provided.
If you want the gory details on image manipulation with Java 2, check
out Java 2D
Graphics by Jonathan Knudsen (O'Reilly
& Associates).
27.4.3.1 Constructor
- public GrayFilter(boolean brighter, int percent)
-
Create an instance of the GrayFilter class that
you can use to do your own filtering. (Normally you
don't call this, but use
createDisabledImage( ) instead.) Both the
brighter and percent arguments
are used to convert color pixels to appropriately shaded gray pixels.
27.4.3.2 Image methods
- public static Image createDisabledImage(Image i)
-
Use
this method to retrieve a grayed-out version of the image
i. This method creates an instance of the
GrayFilter class with brighter
turned on and a gray percent of 50.
- public int filterRGB(int x, int y, int rgb)
-
Override the filterRGB( ) method in
RGBImageFilter, convert the rgb
pixel to a gray pixel, and return that.
|