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. 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);

Java graphics notes_icon

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);

Java graphics notes_icon

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.Java graphics exclamatory_icon

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. lists the system color names and their meanings.

java.awt.Color 1.0

Java graphics api_icon
  • Color(int r, int g, int b)

    creates a color object.

    java.awt.Graphics 1.0

    Java graphics api_icon
    • void setColor(Color c)

      changes the current color. All subsequent graphics operations will use the new color.

      java.awt.Graphics2D 1.2

      Java graphics api_icon
      • void setPaint(Paint p)

        Sets the paint attribute of this graphics context. The Color class implements the Paint interface. Therefore, you can use this method to set the paint attribute to a solid color.

      java.awt.Component 1.0

      Java graphics api_icon
      • void setBackground(Color c)

        sets the background color.void setForeground(Color c)

        sets the foreground color.

        Filling Shapes

        You can fill the interiors of closed shapes (such as rectangles or ellipses) with a color (or, more generally, the current paint setting). Simply call fill instead of draw:

        Rectangle2D rect = . . .;
        g2.setPaint(Color.RED);
        g2.fill(rect); // fills rect with red color
        

        Table 7-2. System colors

        Parameters: c The new background color
        Parameters: c The new foreground color
        desktop Background color of desktop
        activeCaption Background color for captions
        activeCaptionText Text color for captions
        activeCaptionBorder Border color for caption text
        inactiveCaption Background color for inactive captions
        inactiveCaptionText Text color for inactive captions
        inactiveCaptionBorder Border color for inactive captions
        window Background for windows
        windowBorder Color of window border frame
        windowText Text color inside windows
        menu Background for menus
        menuText Text color for menus
        text Background color for text
        textText Text color for text
        textInactiveText Text color for inactive controls
        textHighlight Background color for highlighted text
        textHighlightText Text color for highlighted text
        control Background color for controls
        controlText Text color for controls
        controlLtHighlight Light highlight color for controls
        controlHighlight Highlight color for controls
        controlShadow Shadow color for controls
        controlDkShadow Dark shadow color for controls
        scrollbar Background color for scrollbars
        info Background color for spot-help text
        infoText Text color for spot-help textThe program in fills a rectangle in red, then an ellipse with the same boundary in a dull green (see ).

        Example FillTest.java

         1. import java.awt.*;
         2. import java.awt.geom.*;
         3. import javax.swing.*;
         4.
         5. public class FillTest
         6. {
         7. public static void main(String[] args)
         8. {
         9. FillFrame frame = new FillFrame();
        10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        11. frame.show();
        12. }
        13. }
        14.
        15. /**
        16. A frame that contains a panel with drawings
        17. */
        18. class FillFrame extends JFrame
        19. {
        20. public FillFrame()
        21. {
        22. setTitle("FillTest");
        23. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        24.
        25. // add panel to frame
        26.
        27. FillPanel panel = new FillPanel();
        28. Container contentPane = getContentPane();
        29. contentPane.add(panel);
        30. }
        31.
        32. public static final int DEFAULT_WIDTH = 400;
        33. public static final int DEFAULT_HEIGHT = 400;
        34. }
        35.
        36. /**
        37. A panel that displays filled rectangles and ellipses
        38. */
        39. class FillPanel extends JPanel
        40. {
        41. public void paintComponent(Graphics g)
        42. {
        43. super.paintComponent(g);
        44. Graphics2D g2 = (Graphics2D)g;
        45.
        46. // draw a rectangle
        47.
        48. double leftX = 100;
        49. double topY = 100;
        50. double width = 200;
        51. double height = 150;
        52.
        53. Rectangle2D rect = new Rectangle2D.Double(leftX, topY,
        54. width, height);
        55. g2.setPaint(Color.RED);
        56. g2.fill(rect);
        57.
        58. // draw the enclosed ellipse
        59.
        60. Ellipse2D ellipse = new Ellipse2D.Double();
        61. ellipse.setFrame(rect);
        62. g2.setPaint(new Color(0, 128, 128)); // a dull blue-green
        63. g2.fill(ellipse);
        64. }
        65. }
        

        Filled rectangles and ellipses

        Java graphics 07fig12


Parameters: c The new color
Parameters: r The red value (0-255)
g The green value (0-255)
b The blue value (0-255)