Using Color Core Java

The set Paint method of the Graphics2D class lets you select a color that is used for all subsequent drawing operations on the graphics context. For example:

You can fill the interiors of closed shapes (such as rectangles or ellipses) with a color.

Simply call fill instead of draw:

To draw in multiple colors, you select a color, draw or fill, then select another color, and draw or fill again.

You define colors with the Color class. The java .awt .Colorclass offers predefined constants for the following 13 standard colors:


NOTE: Before Java SE 1.4, color constant names were lowercase, such as This is odd because the standard coding convention is to write constants in uppercase. You can

now write the standard color names in uppercase or, for backward compatibility, in 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:

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 II for more details. If you use a Graphics object instead of a Graphics2D object, you need to use the setColormethod to set colors.

To set the background color, you use the set Background method of the Component class, an ancestor of JComponent.

There is also a set Foreground 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 SystemColorclass. The constants in this class encapsulate the colors used for various elements of the user’s system.

For example,

sets the background color of the component 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 System Color 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 below lists the system color names and their meanings.

System Colors

System ColorsSystem Colors


  • Color(int r, int g, int b)
    creates a color object.
  • Color getColor()
  • void setColor(Color c)
    gets or sets the current color. All subsequent graphics operations will use the new color.


java.awt.Graphics2D 1.2

  • Paint getPaint()
  • void setPaint(Paint p)
    gets or sets the paint property 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.
  • void fill(Shape s)
    fills the shape with the current paint.


  • Color getBackground()
  • void setBackground(Color c)
  • gets or sets the background color.<>
  • Color getForeground()
  • void setForeground(Color c)

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Core Java Topics