Filling Shapes
Iguana fills its
shapes with
a
number of colors, using the setPaint( ) method of
Graphics2D. setPaint( ) sets
the current color in the graphics context, so we set it to a
different color before each drawing operation. This method accepts
any object that implements the Paint interface.
The 2D API includes three implementations of this interface,
representing solid colors, color gradients, and textures.
Solid Colors
The java.awt.Color
class handles color in Java. A Color object
describes a single color. You can create an arbitrary
Color by 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. You can also
use getColor( ) to look up a named color in the
system properties table, as described in Chapter 9. getColor( ) takes a
String color property name, retrieves the integer
value from the Properties list, and returns the
Color object that corresponds to that color.
The Color class also
defines a number of static final color values;
these are what we used in the Iguana example.
These constants, such as Color.black and
Color.red, provide a convenient set of basic color
objects for your drawings.
Color Gradients
A color gradient
is a
smooth blend from one color to another. The
GradientPaint
class 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. The
GradientPaint will take care of the details so ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access