Using Color

   


The setPaint method of the Graphics2D class lets you select a color that is used for all subsequent drawing operations on the graphics context. 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); 

NOTE

Before JDK 1.4, color constant names were lower case, such as Color.red. This is odd because the standard coding convention is to write constants in upper case. Starting with JDK 1.4, you can write the standard color names in upper case or, for backward compatibility, in lower case.


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:

 g2.setPaint(new Color(0, 128, 128)); // a dull blue-green g2.drawString("Welcome!", 75, 125); 

NOTE

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.

 MyPanel p = new MyPanel(); p.setBackground(Color.PINK); 

There is also a setForeground method. It specifies the default color that is used for drawing on the component.

TIP

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,

 p.setBackground(SystemColor.window) 

sets the background color of the panel 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.

  • Color(int r, int g, int b)

    creates a color object.

Table 7-2. System Colors

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 text





 java.awt.Color 1.0 

Parameters:

r

The red value (0 255)

 

g

The green value (0 255)

 

b

The blue value (0 255)



 java.awt.Graphics 1.0 

  • void setColor(Color c)

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

    Parameters:

    c

    The new color



 java.awt.Graphics2D 1.2 

  • 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 

  • void setBackground(Color c)

    sets the background color.

    Parameters:

    c

    The new background color


  • void setForeground(Color c)

    sets the foreground color.

    Parameters:

    c

    The new 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 

The program in Example 7-5 fills a rectangle in red, then an ellipse with the same boundary in a dull green (see Figure 7-12).

Example 7-5. 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.setVisible(true); 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.       add(panel); 29.    } 30. 31.    public static final int DEFAULT_WIDTH = 400; 32.    public static final int DEFAULT_HEIGHT = 400; 33. } 34. 35. /** 36.    A panel that displays filled rectangles and ellipses 37. */ 38. class FillPanel extends JPanel 39. { 40.    public void paintComponent(Graphics g) 41.    { 42.       super.paintComponent(g); 43.       Graphics2D g2 = (Graphics2D) g; 44. 45.       // draw a rectangle 46. 47.       double leftX = 100; 48.       double topY = 100; 49.       double width = 200; 50.       double height = 150; 51. 52.       Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); 53.       g2.setPaint(Color.RED); 54.       g2.fill(rect); 55. 56.       // draw the enclosed ellipse 57. 58.       Ellipse2D ellipse = new Ellipse2D.Double(); 59.       ellipse.setFrame(rect); 60.       g2.setPaint(new Color(0,  128, 128)); // a dull blue-green 61.       g2.fill(ellipse); 62.    } 63. } 

Figure 7-12. Filled rectangles and ellipses



       
    top



    Core Java 2 Volume I - Fundamentals
    Core Java(TM) 2, Volume I--Fundamentals (7th Edition) (Core Series) (Core Series)
    ISBN: 0131482025
    EAN: 2147483647
    Year: 2003
    Pages: 132

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net