Java Reference
In-Depth Information
FIGURE 13.2
Displaying cen-
tered text in a
graphical user
interface.
The getSize() method calls in lines 43 and 44 use the width and height of the panel to
determine where the text should be displayed. When the application is resized, the panel
also is resized, and paintComponent() is called automatically.
Color
The Color and ColorSpace classes of the java.awt package can be used to make a
graphical user interface more colorful. With these classes, you can set the color for use in
drawing operations, as well as the background color of an interface component and other
windows. You also can translate a color from one color-description system into another.
By default, Java uses colors according to the sRGB color-description system. In this sys-
tem, a color is described by the amounts of red, green, and blue it contains—that's what
the R, G, and B stand for. Each of the three components can be represented as an integer
between 0 and 255. Black is 0, 0, 0—the complete absence of any red, green, or blue.
White is 255, 255, 255—the maximum amount of all three. You also can represent sRGB
values using three floating-point numbers ranging from 0 to 1.0. Java can represent mil-
lions of colors between the two extremes using sRGB.
A color-description system is called a color space , and sRGB is only one such space.
There also is CMYK, a system used by printers that describes colors by the amount of
cyan, magenta, yellow, and black they contain. Java supports the use of any color space
desired as long as a ColorSpace object is used that defines the description system. You
also can convert from any color space to sRGB, and vice versa.
13
Java's internal representation of colors using sRGB is just one color space being used in
a program. An output device such as a monitor or printer also has its own color space.
When you display or print something of a designated color, the output device might not
support the designated color. In this circumstance, a different color is substituted or a
dithering pattern is used to approximate the unavailable color.
 
Search WWH ::




Custom Search