Using Timeline and KeyFrames in javaFx 

JavaFx  provides timeline and key frames to  process actions in a specific order in time. Within timelines there can exists multiple key frames where the actual actions to be taken at a specific time can be specified.  Once specified timeline can be executed by calling the play() or playFromStart() method for the timeline.  Repetition of timeline can be specified by setting value for the repeatCount variable. By setting the value of repeatCount to Timeline.INDEFINITE, we can run the timeline repeatedly until the stop() function is called. Timeline can also be paused by calling the pause() function.

KeyFrame is where we specify what action to take at what time within the timeline. A KeyFrame has an action (function type) which is executed at the specified time in the time variable. A timeline can have multiple KeyFrames. These key frames are executed once the timeline is started by a play() or playFromStart() function.

ex:


var timeLine = Timeline
{
   var contents = hltNodes
   var interval:Duration = 0s;
   repeatCount: 4

   keyFrames:
   [
      for (nodes in contents)
      {
         var frame:KeyFrame = KeyFrame
         {
           time: 0s + interval;

           action:function():Void
           {
              nodes.requestFocus();
           }
         }

         interval = interval + 5s;
         frame;
      },
      KeyFrame
      {
         time: interval;
      }
   ]
}

This code calls the requestFocus() for all the nodes inside the sequence hltNode in 5 second intervals. After each 5 second, the requestFocus() is moved on to the next node. This timeline is executed 4 times because the value for the repeatCount variable is specified as 4.

Advertisements