Iguana fills its shapes with a
number of colors, using the
setPaint() method of
Graphics2D. This method sets the
current color in the graphics context, so we set it to a different color
before each drawing operation.
setPaint() accepts any object that implements
Paint interface. The 2D API
includes three implementations of this interface, representing solid
colors, color gradients, and textures.
represents color in Java. A
object describes a single color and implements the
Paint interface for filling an area with it.
You can create an arbitrary
specifying the red, green, and blue values, either as integers between 0
and 255 or as floating-point values between 0.0 and 1.0. The (somewhat
getColor() method can
be used to look up a named color in the system properties table, as
described in Chapter 11.
Color class also defines a
static final color values;
we used these in the
These constants, such as
Color.red, provide a convenient
set of basic color objects for your drawings.
A color gradient is a smooth blend
between two or more colors. The
encapsulates this idea in a handy implementation of the
Paint interface. All you need to do is specify
two points and the color at each point.
GradientPaint takes care of the details so
that the color fades smoothly from one point to the other. In the
previous example, the ellipse is filled with a gradient this way:
The last parameter in
GradientPaint’s constructor determines whether
the gradient is cyclic. In a cyclic gradient, the
colors keep fluctuating beyond the two points that you’ve specified.
Otherwise, the gradient just draws a single blend from one point to the
other. Beyond each endpoint, the color is solid.
Java 6 added multistop gradient capabilities to
RadialGradientPaint. A multistop gradient can,
for example, smoothly fade from green to blue to red.
A texture is simply an image repeated
over and over like a floor tile. This concept is represented in the 2D
API with the
TexturePaint class. To
create a texture, just specify the image to be used and the rectangle
that will be used to reproduce it. To do this, you also need to know how
to create and use images, which we’ll get to a little later.
Color class makes
it easy to construct a particular color; however, that’s not always what
you want to do. Sometimes you want to match a preexisting color scheme.
This is particularly important when you are designing a user interface;
you might want your components to have the same colors as other
components on that platform and to change automatically if the user
redefines his or her color scheme.
That’s where the
SystemColor class comes
in. A system color represents the color used by the local windowing
system in a certain context. The
SystemColor class holds lots of predefined
system colors, just like the
class holds some predefined basic colors. For example, the field
represents the color used for the background of the titlebar of an
represents the color used for the title itself.
menu represents the background color of menu
menuText represents the color of a menu item’s text
when it is not selected;
the color used when the menu item is selected; and so on. You could use
window value to set the
color of a
Window to match the other
windows on the user’s screen—whether or not they’re generated by Java
is a subclass of
Color, you can use
it wherever you would use a
are tricky. They are constant, immutable objects as far as you, the
programmer, are concerned (your code is not allowed to modify them), but
they can be modified at runtime by the system. If the user changes his
color scheme, the system colors are automatically updated to follow
suit; as a result, anything displayed with system colors will
automatically change color the next time it is redrawn. For example, the
myWindow would automatically
change its background color to the new background color.
SystemColor class has one
noticeable shortcoming. You can’t compare a system color to a
Color directly; the
Color.equals() method doesn’t return reliable
results. For example, if you want to find out whether the window
background color is red, you can’t call: