Quick Contact

    Hibernate

    JavaFX Timeline

    Timeline in JavaFX is basically used to define free and flexible forms of animation. Timeline in JavaFX works on using the keyframes processing each of the keyframes individually and sequentially. It does not guarantee the timing of processing of each keyframe; instead it processes each keyframe at a specified time interval. Programmers have the facility to add the keyframes to the timeline either at the time of creation or afterwards. Timeline is a subclass of javafx.animation.Animation class. A lot of other things can be performed using timeline like setting the reverse of timeline, loop of how many times it should run, etc.

    Syntax:

    Below given is the basic syntax of using the Timeline in JavaFX:

    Final Timeline tm = new Timeline();
    tm.add(new KeyFrame(Duration.millis(10000),
    new KeyValue (button.translateXProperty(), 8)));
    tm. setCycleCount(8);
    .
    ..
    ..
    

    How does the JavaFx Timeline Function Work?

    Some of the important points defining the working of JavaFX timeline are given below:

    • Timeline in JavaFX is nothing but an animation consisting of many KeyFrameobjects , each of which needs to be run sequentially.
    • Some of the standard attributes of Timeline are auto- reverse and cycle count.
    • Cycle count is nothing but the count of the number of times, an animation should be played. We can also play the animation infinite times using Timeline.INDEFINITE. But by default animation runs only 1 time.
    • Auto- reverse is nothing but a boolean value to indicate whether the animation can play backwards or not.
    • In order to set the largest time of the Timeline, KeyFrameAnimation.cycleDurationProperty() is used.
    • We can not change the keyFrames of the running timeline. In order to use the new value, one needs to stop and start it again.
    • In the timeline of JavaFX, cycle count is 1 and the auto-reverse feature is set to TRUE.
    • Method start() contains the main logic of timeline animation.

    Constructors

    Below given are the constructors of the Timeline in JavaFX:

    S.No. Constructor Description
    1. Timeline() It is a constructor of timeline having no parameters
    2. Timeline(double targetFramerate, Keyframe… keyframe) It is the constructor of timeline having 2 parameters, i.e. targetFramerate and keyFrames.It allows to define the custom Frame rate and keyFrames of the timeline.
    3. Timeline(double targetFramerate) It is the constructor of timeline having targetFramerate as its parameter. In this constructor, one needs to define the Animation.targetFramerate.
    4. Timeline(Keyframe … keyframes) It is the constructor of timeline having keyFrames as its parameter. There can be one or multiple keyFrames.
    Methods:

    Below given are the methods along with their description used in the timeline of JavaFX:

    S.no. Method name Method description
    1. getKeyFrames() This method returns the keyframes of the respective timeline.

    Syntax:

    public final ObservableList< keyFrame>getKeyFrames();

    2. stop() As the name indicates, this method stops the animation and allows the head to start from the initial position. As using the stop() method is an asynchronous call so it might not get stopped immediately. If the animation is not working/ running, this method has no impact. It has ‘void’ as a modifier.

    Syntax:

    public void stop()

    Some of the methods inherited from the javaFX.animation.Animation class are given below:

    S.No. Method name Description
    1. getTargetframeRate() This method defines the frame rate at which the animations should run, i.e. the frames per second.
    2. getStatus() This method is used to get the status of the Animation. Animation in JavaFX has basically 3 status, i.e. STOPPED, PAUSED and RUNNING
    3. setStatus() This method is used to set the status of property, i.e. an animation. Status can be set as STOPPED, PAUSED and RUNNING.
    4. setCycleDuration() This method is used to set the value of cycle duration. It is basically used to indicate the duration of Animation’s one cycle. It is a read only variable.
    5. getCurrentTime() This method is used to get the current time of the animation. Its default value is 0 ms.
    6. setDelay() This method is used to set the delay property of the animation. Using this, programmers can delay the start of the animation. By default, its value is 0 ms.
    7. getDelay() This method is used to get the delay of the animation. By default, its value is set to 0 ms.

    Example of JavaFX Timeline

    Some of the examples implementing the usage of Timeline in JavaFX code are given below:

    Example #1

    Code:

    import javafx.animation.KeyFrame;
    import javafx.animation.KeyValue;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.Font;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    public class TimelineFX extends Application {
    public static void main(String[] args) {
    launch(args);
    }
    @Override
    public void start(Stage stage)
    {
    // Create the Text using text object
    Text text = new Text("Hello Hello!");
    VBox root = new VBox(text);
    // Setting the text font size
    text.setFont(Font.font(12));
    // Creating the Scene using its object
    Scene scene = new Scene(root);
    // Adding the Scene to the Stage
    stage.setScene(scene);
    // Now Setting the title of the above Stage
    stage.setTitle("This is stage title");
    // Displaying the Stage using the show() method
    stage.show();
    // Getting the Scene width and the Text width
    double sw = scene.getWidth();
    double tw = text.getLayoutBounds().getWidth();
    // Defining the Durations of animations
    Duration startD = Duration.ZERO;
    Duration endD = Duration.seconds(50);
    //Creating the keyFrames to use in timeline
    KeyValuestartValue = new KeyValue(text.translateXProperty(), sw);
    KeyFramestartFrame = new KeyFrame(startD, startValue);
    KeyValueendValue = new KeyValue(text.translateXProperty(), -6.0 * tw);
    KeyFrameendFrame = new KeyFrame(endD, endValue);
    // Creating a Timeline using above values
    Timeline t1 = new Timeline(startFrame, endFrame);
    // Setting the cycle count of animation
    t1.setCycleCount(t1.INDEFINITE);
    //Setting the auto reverse property of animation
    t1.setAutoReverse(false);
    // Running the animation using play() method
    t1.play();
    }
    }
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.