Java Reference
In-Depth Information
WHITE
RED
PINK
(255, 255, 255)
(255, 0, 0)
(255, 175, 175)
LIGHT
_
GRAY
(192, 192, 192)
ORANGE
(255, 200, 0)
MAGENTA
(255, 0, 255)
GRAY
(128, 128, 128)
YELLOW
(255, 255, 0)
CYAN
(0, 255, 255)
DARK
_
GRAY
(64, 64, 64)
GREEN
(0, 255, 0)
BLUE
(0, 0, 255)
BLACK
(0, 0, 0,)
So if we want our window in the previous example to have a pink background, we could add the statement:
aWindow.setBackground(Color.PINK);
When you have created a
Color
object, you can brighten or darken the color it represents by calling its
brighter()
or
darker()
methods, which will increase or decrease the intensity of the color
components by a predefined factor:
thisColor.brighter(); // Brighten the color
thatColor.darker(); // Darken the color
The intensities of the component colors will always remain between 0 and 255. When you call
brighter
and a color component is already at 255, it will remain at that value. The other component
intensities will be increased if they are less than 255. In a similar way, the
darker()
method will not
change a component intensity if it is zero. The factor used for darkening a color component is 0.7. To
brighten a color component the intensity is increased by 1/0.7.
A fundamental point to remember here is that you can only obtain the colors available within the computer
and the operating system environment on which your Java program is running. If you only have a limited
range of colors, the
brighter()
and
darker()
methods may appear to have no effect. Although you can
create
Color
objects that are supposed to represent all kinds of colors, if your computer only supports
sixteen colors you will always end up with one of your sixteen. If your machine supports 24-bit color and this
is supported in your system environment, then everything should be fine and dandy.
You can obtain any of the component intensities by calling
getRed()
,
getGreen()
, or
getBlue()
for a
Color
object. A color can also be obtained as a value of type
int
that is a composite of the red,
green, and blue components of the color represented by a
Color
object using the
getRGB()
method.
You can also create a
Color
object from a single RGB value of type
int
.
To compare two
Color
objects you can use the
equals()
method. For example to compare two color
objects
colorA
and
colorB
, you could write:
if(colorA.equals(colorB))
// Do something...
The
equals()
method will return
true
if all three components of the two
Color
objects are equal.
You could also use the
getRGB()
method to do the same thing: