Java Reference
In-Depth Information
LinearGradients
store this information in a
Sequence
called
stops
, which is composed of objects of
type
Stop
. A
Stop
is a very simple class; it is composed of an
offset
and a
color
and corresponds to the
vertical black lines from Figure 8-1.
RadialGradient
RadialGradients
work just like
LinearGradients
, but create a circular pattern. It is as if a line with a linear
gradient was rotated around one of its ends. So when specifying the
Stops
for a linear gradient, the
Stop
at
offset
0.0 is the center of the circle, and the stop at
offset
1.0 is the outside of the circle.
Proportional
Each type of gradient works in two different modes. Setting the property
proportional
on either a
LinearGradient
or a
RadialGradient
will toggle the two modes. When
proportional
is set to
false
, a
region of the shape is specified where the gradient will be drawn. And when
proportional
is set to
true
,
the gradient is applied to the entire shape. Let's examine the case when
proportional
is set to
false
first.
LinearGradient
has a set of four coordinate properties—
startX
,
startY
,
endX
, and
endY
—that are
used to describe the region where the gradient should be drawn. Since the region defined by the
coordinate properties might be smaller than the shape, there are three ways to color the regions outside
of the coordinates. The regions outside of the coordinates will either be the color of the first or last
stop
depending on the side, simply colored as if the gradient repeated, or colored as if a mirror image of the
gradient were applied. These options are controlled by the property
cycleMethod
.
RadialGradient
works similarly to
LinearGradient
except that a set of coordinates is not used to
describe the region drawn by the gradient. The properties
radius
,
centerX
, and
centerY
are used to
define a circular region where one cycle of the gradient is drawn. Again, specifying the
cycleMethod
will
dictate what is drawn outside of this region.
When
proportional
is set to
true
, JavaFX tries to apply the gradient to the entire shape. For example,
a rectangle with a
LinearGradient
applied to it will be drawn so that the upper left corner is the color at
offset
0.0 and the lower right color is that at
offset
1.0 as defined by the
steps
. The properties
startX
,
startY
,
endX
, and
endY
can be set, but only specify the direction of the gradient. With a
RadialGradient
,
the
radius
has no effect. However, the
centerX
and
centerY
can still be specified to move the center of
radial effect.
Figure 8-2 shows four examples of gradients.