Java Reference
In-Depth Information
■
in JavafX 2, it was recommended to use the builder pattern for creating
Nodes
. as a consequence, creating a
Line
would be done as follows:
Note
line = LineBuilder.create()
.startY(50)
.endX(200)
.endY(400)
.strokeWidth(4)
.stroke(Color.BLUE)
.build();
the advantage of this approach is that it is clear what the parameter “50” in the second line means: the line has a
start-coordinate of 50 in the vertical position. the same readability can be achieved by calling setter methods,
for example
line.setStartY(50);
in practice, however, many parameters are passed via the constructor of the
Node
. in the case of a
Line
instance,
the second parameter is the
startY
parameter. this approach leads to fewer lines of code, but the developer should
be careful about the order and the meaning of the parameters in the constructor. Once again, we strongly recommend
having the JavaDoc available while writing JavafX applications.
Inserting Key Frames into the Timeline
Our timeline contains a collection of two
KeyFrame
instances. Using the
KeyValue
constructor, one of these instances
assigns 100 to the
startXVal
property at the beginning of the timeline, and the other assigns 300 to the
startXVal
property when the timeline has been running for one second. Because the
startX
property of the
Line
is bound to the
value of the
startXVal
property, the net result is that the top of the line moves 200 pixels horizontally over the course
of one second.
In the second
KeyFrame
of the timeline, the
KeyValue
constructor is passed a third argument that specifies that
the interpolation from 100 to 300 will occur in a linear fashion over the one-second duration. Other
Interpolation
constants include
EASE_IN
,
EASE_OUT
, and
EASE_BOTH
. These cause the interpolation in a
KeyFrame
to be slower in the
beginning, ending, or both, respectively.
The following are the other
Timeline
properties, inherited from the
Animation
class, used in this example:
autoReverse
, which we're initializing to true. This causes the timeline to automatically reverse
when it reaches the last
KeyFrame
. When reversed, the interpolation goes from 300 to 100 over
the course of one second.
•
cycleCount
, which we're initializing to
Animation.INDEFINITE
. This causes the timeline to
repeat indefinitely until stopped by the
stop()
method of the
Timeline
class.
Speaking of the methods of the
Timeline
class, now is a good time to show you how to control the timeline and
monitor its state.
•
Search WWH ::
Custom Search