Colors
The setPaint method of the Graphics2D class lets you select a color that is used for all subsequent drawing operations on the graphics context or component. To draw in multiple colors, you select a color, draw, then select another color, and draw again. You define colors with the Color class. The java.awt.Color class offers predefined constants for the 13 standard colors listed in Table 7-1.
Table 7-1. Standard colors
| BLACK | GREEN | RED | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BLUE | LIGHT_GRAY | WHITE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CYAN | MAGENTA | YELLOW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DARK_GRAY | ORANGE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GRAY | PINK | For example,
g2.setPaint(Color.RED);
g2.drawString("Warning!", 100, 100);
Before SDK 1.4, color constant names were lowercase such as Color.red. This is odd since the standard coding convention is to write constants in uppercase. Starting with SDK 1.4, you can write the standard color names in uppercase or, for backward compatibility, in lowercase. We use the uppercase constants in our programs. If you use an older version of the SDK, you need to change the color names to lowercase.You can specify a custom color by creating a Color object by its red, green, and blue components. Using a scale of 0-255 (that is, one byte) for the redness, blueness, and greenness, call the Color constructor like this: Color(int redness, int greenness, int blueness) Here is an example of setting a custom color:
g.setPaint(new Color(0, 128, 128)); // a dull blue-green g.drawString("Welcome!", 75, 125);
In addition to solid colors, you can select more complex "paint" settings, such as varying hues or images. See the Advanced AWT chapter in Volume 2 for more details. If you use a Graphics object instead of a Graphics2D object, you need to use the setColor method to set colors.To set the background color, you use the setBackground method of the Component class, an ancestor of JPanel. In fact, you should set the background before displaying the frame for the first time. MyPanel p = new MyPanel(); p.setBackground(Color.WHITE); contentPane.add(p); There is also a setForeground method. It specifies the default color that is used for drawing on the component. The brighter() and darker() methods of the Color class produce, as their names suggest, either brighter or darker versions of the current color. Using the brighter method is also a good way to highlight an item. Actually, brighter() is just a little bit brighter. To make a color really stand out, apply it three times: c.brighter().brighter().brighter().Java gives you predefined names for many more colors in its SystemColor class. The constants in this class encapsulate the colors used for various elements of the user's system. For example, frame.setBackground(SystemColor.window) sets the background color of the frame to the default used by all windows on the user's desktop. (The background is filled in whenever the window is repainted.) Using the colors in the SystemColor class is particularly useful when you want to draw user interface elements so that the colors match those already found on the user's desktop. Table 7-2 lists the system color names and their meanings. java.awt.Color 1.0
|
| Parameters: | c | The new color |
| Parameters: | r | The red value (0-255) |
| g | The green value (0-255) | |
| b | The blue value (0-255) |