Java Reference
In-Depth Information
C H A P T E R 8
■ ■ ■
Effect: Animated Gradients
Colors are obviously a key component of any graphical application. Modern monitors are able to display
millions of colors, and the designers of software have taken advantage of that. One important aspect of
colors within an application is how they blend together, and a number of graphical libraries provide a
mechanism to display the many grades of colors between any two colors. In general, this feature is called
a gradient. Gradients can be used to create remarkable subtlety, be it the shine of a button or a slight
sense of depth. While displaying solid colors or gradients is an excellent first step, some remarkable
things can happen when these components are animated.
This chapter explores the basics of colors and gradients in JavaFX and how to animate them.
The Basics
To set the color of any of the shape nodes in JavaFX, you set the property
fill
with a
Paint
object. This
can be surprising to developers new to graphics, as it is not exactly obvious why the property is called
fill
and why the value being set to is of type
Paint
. First, shapes actually have two different parts that
can have color, so it would be confusing to simply have one property called
color
. The parts are the
fill
and the
stroke
. The
fill
is the area inside the shape, and the
stroke
is the line that defines the shape.
For example, if you draw a rectangle on a piece of paper with a pencil, the
stroke
is the pencil line, while
the color of the paper is the
fill
.
The other question—why the two properties
fill
and
stroke
are of type
Paint
and not simply
Color
—can be answered by looking at the classes that extend
Paint
. These are the classes
Color
,
LinearGradient
, and
RadialGradient
. Let's take a quick look at each class.
Paint Types
Included here are the current types of
Paint
available in JavaFX. It would seem logical that in the future
there may be more, such as those that mimic physical brushes, give texture to a shape, or any number of
possibilities.