This discussion is archived
1 Reply Latest reply: Mar 28, 2013 7:09 AM by James_D RSS

Question about using KeyFrames without calling a Keyvalue in the constructo

KonradZuse Explorer
Currently Being Moderated
I'm curious why in examples like this
          Timeline countdown = new Timeline(
               new KeyFrame(javafx.util.Duration.ZERO, 
                         (event) -> secondsRemaining.set((int) Duration.between(Instant.now(), finishTime).getSeconds())
                ),
               new KeyFrame(javafx.util.Duration.seconds(1))
          );
the first is KeyFrame(duration, event) then KeyFrame(duration)

according to the 2.2, and this being pasted from the fx 8 docs is the constructors for KeyFrame

KeyFrame(Duration time, EventHandler<ActionEvent> onFinished, KeyValue... values)
Constructor of KeyFrame
KeyFrame(Duration time, KeyValue... values)
Constructor of KeyFrame
KeyFrame(Duration time, java.lang.String name, EventHandler<ActionEvent> onFinished, java.util.Collection<KeyValue> values)
Constructor of KeyFrame
KeyFrame(Duration time, java.lang.String name, EventHandler<ActionEvent> onFinished, KeyValue... values)
Constructor of KeyFrame
KeyFrame(Duration time, java.lang.String name, KeyValue... values)
Constructor of KeyFrame
So no keyvalues....?
Example 1-5 Timeline Animation

final Rectangle rectBasicTimeline = new Rectangle(100, 50, 100, 50);
rectBasicTimeline.setFill(Color.RED);
...
final Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
final KeyValue kv = new KeyValue(rectBasicTimeline.xProperty(), 300);
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
timeline.play();
I've been a little confused with hod KeyValues and KeyFrames play a role, but it seems like the value specifies on what we are doing, and what we are changing it to
 (rectBasicTimeline.xProperty(), 300); 
and the "Frame" is the duration and which "KeyValue?" So the KeyValue is the important part, or an event.

It seems like the event takes place of the "KeyValue" so we don't need one? I see it says (KeyValue... values) and I never really thought about it, but that means as many values as needed?

It doesn't, however, show that Events take the place of the KeyValues soo.... thoughts anyone?

I've seen this posted by 2 members on here(who will most likely answer my question anyways :p) so I was curious how it all works, and if Event does replace a KeyValue, then I want to know when am I supposed to know in which classes this works on...?
  • 1. Re: Question about using KeyFrames without calling a Keyvalue in the constructo
    James_D Guru
    Currently Being Moderated
    The syntax "..." in the type of the last parameter of a method (or constructor) indicates 0 or more parameters of that type. It's called "varargs", or more formally a "variable arity parameter". See [url http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html]Arbitrary numbers of arguments in the Java tutorial, or the [url http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html]description of varargs from release 1.5.0 of the Java language (when varargs were introduced).

    A timeline animation is composed of a collection of KeyFrames. Each KeyFrame consists of a Duration (the time on the timeline for that "frame"), zero or more KeyValues, and optionally an EventHandler.

    The KeyValues consist of a writable property and a "target value" for that property.

    When the timeline animation runs, it "smoothly" changes the values of all the properties that appear in KeyValues. The aim is that for each instant defined by a KeyFrame, the value of any property specified by a KeyValue in that KeyFrame will be equal to the corresponding target value. Values at times in between KeyFrames are interpolated between the times specified by the KeyFrames.

    Additionally, when the time on the timeline passes the time specified in that key frame, and event will be fired to the event handler, if there is one.

    So your constructor call with a Duration and EventHandler matches the constructor with signature
    KeyFrame(Duration, EventHandler, KeyValues...)
    with zero KeyValues. This key frame doesn't specify any values to be "targeted" at that moment, but will call the EventHandler's handle(...) method when the corresponding moment on the timeline is passed.

    The code
    final KeyValue kv = new KeyValue(rectBasicTimeline.xProperty(), 300);
    final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
    creates a key frame at 500 milliseconds with a target value of 300 for rectBasicTimeline's x property. So the timeline will smoothly change rectBasicTimeline.xProperty from it's initial value at time 0 to 300 over a time period of 500 milliseconds (assuming no other KeyFrames in there).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points