Java Reference
In-Depth Information
Color
letsyoucreateasolidcolor.Amongitsvariousconstructorsare
Color(int
r, int g, int b)
forcreatinganopaquesolidcolorand
Color(int r, int
g, int b, int a)
for creating a solid color with an alpha value.
Youaren'trestrictedtospecifyinginteger-basedcomponentvaluesthatrangefrom0
through255.Ifyouprefervaluesrangingfrom0.0through1.0,youcancallconstruct-
orssuchas
Color(float r, float g, float b)
and
Color(float r,
float g, float b, float a)
.
Foryourconvenience,
Color
declaresseveralprecreated
Color
constants:
BLACK
,
BLUE
,
CYAN
,
DARK_GRAY
,
GRAY
,
GREEN
,
LIGHT_GRAY
,
MAGENTA
,
ORANGE
,
PINK
,
RED
,
WHITE
,and
YELLOW
.Althoughall-lowercasevariantsareavailable,you
should avoid using them—constants should be uppercased.
GradientPaint
letsyoucreateagradient.Itdeclaresseveralconstructors,includ-
ing
GradientPaint(float x1, float y1, Color color1, float
x2, float y2, Color color2)
, which describes a gradient that transitions
fromupper-leftcorner(
x1
,
y1
)tolower-leftcorner(
x2
,
y2
)inuserspace.Thecolorat
(
x1
,
y1
) is
color1
and the color at (
x2
,
y2
) is
color2
.
Note
Java 6 introduced an abstract
java.awt.MultipleGradientPaint
class and concrete
java.awt.LinearGradientPaint
and
java.awt.RadialGradientPaint
subclassestocreatedifferentkindsofgradi-
ents that are based on multiple (typically more than two) colors. I explore these
classesandpresentdemosinmy“Java2DMultiColorGradientPaints”tutorial(
ht-
j2dmcgp
).
TexturePaint
lets you create a texture. It declares a
Tex-
turePaint(BufferedImage txtr, Rectangle2D anchor)
constructor
forcreatingthetexturefromacombinationofabufferedimage(whichspecifiestheim-
ageonwhichthetextureisbased)andarectangularanchor(whichidentifiesarectan-
gular portion of the image to be replicated).
Figure 7-18
demonstrates solid color (upper-left corner), gradient (upper-right
corner), and texture (bottom) paints.