Colors
Home ] Up ] Introduction ] The Swing Package ] Frames ] Panes, Text & Fonts ] [ Colors ] Shapes ] Paint Modes ] Images ]

 

Foreground Color
Background Color
System Colors

 

What would GUI programming be without the ability to control colors?

Naturally, Java provides support for this, via a class called Color.  (There are other color-related classes, but they are not as frequently used.)

As with any Java class, you can learn a lot from going to the Color class's javadoc page.

The primary color model that you will tend to use is the RGB (Red, Green, Blue) model, although there are others.  With the RGB model, every color is represented in terms of its red, green and blue components, each within a range of 0 through 255.   0 means no contribution from a given color component, while 255 means 100% contribution from a given color component.

The class Color predefines (as class variables) a set of commonly used colors. Here is an image of a Java application that shows the colors, their names, and their (R,G,B) components:

The above names can be used in a Java program to specify a particular color.  For example:

Color.MAGENTA

Prior to JDK 1.4, the predefined colors were given only lowercase names:

Color.magenta

and you can still use the lowercase names if you wish. However, all new code should use the uppercase versions.

Note that those (newer) names with an underscore have (older) lowercase equivalents in "bumpy case":

Color.DARK_GRAY <-> Color.darkGray
Color.LIGHT_GRAY <-> Color.lightGray
 

This page was last modified on 02 October, 2007