Java Reference
In-Depth Information
black
(0, 0, 0)
magenta
(255, 0, 255)
blue
(0, 0, 255)
orange
(255, 200, 0)
cyan
(0, 255, 255)
pink
(255, 175, 175)
darkGray
(64, 64, 64)
red
(255, 0, 0)
gray
(128, 128, 128)
white
(255, 255, 255)
green
(0, 255, 0)
yellow
(255, 255, 0)
lightGray
(192, 192, 192)
The following statement sets the color for a
Graphics2D
object named
comp2D
by using
one of the standard class variables:
comp2D.setColor(Color.pink);
If you have created a
Color
object, it can be set in a similar fashion:
Color brush = new Color(255, 204, 102);
comp2D.setColor(brush);
After you set the current color, subsequent methods to draw strings and other things will
use that color.
You can set the background color for a component, such as a panel or frame, by calling
the component's
setBackground(
Color
)
and
setForeground(
Color
)
methods.
The
setBackground()
method sets the component's background color, as in this
example:
setBackground(Color.white);
The
setForeground()
method is called on user interface components, changing the color
of a component such as a button or a window.
You can use
setForeground()
in the
init()
method to set the color for drawing opera-
tions. This color is used until another color is chosen with either
setForeground()
or
setColor()
.
13
If you want to find out what the current color is, you can use the
getColor()
method on
a
Graphics2D
object, or the
getForeground()
or
getBackground()
methods on the com-
ponent.
The following statement sets the current color of
comp2D
—a
Graphics2D
object—to the
same color as a component's background:
comp2D.setColor(getBackground());