Java Reference
In-Depth Information
Let's dissect what's taking place here. First, gradients do not operate on the
effect
instance variable. Rather, they are assigned to the content's
fill
instance variable. As mentioned, gradients are not part of the javafx.scene.effect
package, but instead they belong to
javafx.scene.paint
.
The
stops
variable is a sequence used for defining the overall gradient effect.
Within the sequence, a series of
Stop
instances are used to describe individual
transitions. Its
offset
variable, ranging in value from 0 to 1, describes where,
within the content, this gradient should be placed, 0.0 being the beginning of the
content, 1.0 being the end, 0.33 being approximately a third of the way in, and so
on. Alongside the
offset
variable, a
Stop
instance should also specify the
color
too. In our preceding example, we, in effect, define the simplest of transi-
tions, a beginning
Stop
(offset is set to 0.0) with a white color, and an end
Stop
(offset 1.0) set to black. With this information, the JavaFX runtime will fill in the
content interpolating all of the colors in between the stops.
Lastly, in our example, the gradient was defined with the
proportional
variable
set to true. By doing so, the start and end coordinates are defined relative to the
shape where, again, 0.0 specifies the beginning of the shape, 1.0 specifies the
end. By setting
proportional
to false instead, the start and end points would
have to be defined in absolute pixels.
LinearGradient
Sticking with our 3D theme, gradients, among many other uses, could be
employed to add perspective to content. Let's see how a
LinearGradient
can be
applied to an ordinary shape to make it look more realistic. First, we'll combine
two simple shapes together, a triangle and an ellipse, to create a cone. Figure
6.10 displays the particulars; later on we'll supply the complete code.
The resulting cone uses a class called
ShapeIntersect
, which is part of the
javafx.scene.shape
package, to construct one logical shape from a number of
previously independent ones. The
ShapeIntersect a[]
instance variable forms
a union of the shapes included in the sequence—in this case a triangle and an
ellipse—while the
b[]
sequence (not used in this example) could be used to add
additional shapes that are intersected with
a[]
to form yet a more complex shape.
The big advantage of using
ShapeIntersect
here is that you can apply an effect
to the entire object, rather than having to do so on each individual component.
Search WWH ::
Custom Search