Tuesday, June 14, 2016

33. TranslateTransition

A red circle of 20 radius is drawn, and animated from 20, 20 to 380, 380, continuously moved in a diagonal direction.


We set the duration (dur) as 1 second (1000 milliseconds). We selected the Node of circ to go from 20, 20 to 380, 380. Since we have used the setAutoReverse method as true, it will do both down-diagonal and up-diagonal animations and will last forever since setCycleCount method is set to Timeline.INDEFINITE.


package ex33;
 
import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
 
public class Ex33 extends Application {
 
    @Override
    public void start(Stage primaryStage) {
 
        primaryStage.setTitle("Example 33. TranslateTransition");
        
        Circle circ = new Circle(20,Color.RED);
        
        Group root = new Group();
        root.getChildren().add(circ);
        Scene scene = new Scene(root, 400, 400);
        
        Duration dur = new Duration(1000);
        
        TranslateTransition tT = new TranslateTransition();
        tT.setDuration(dur);
        tT.setNode(circ);
        tT.setFromX(20);
        tT.setFromY(20);
        tT.setToX(380);
        tT.setToY(380);
        tT.setInterpolator(Interpolator.LINEAR);
        tT.setCycleCount(Timeline.INDEFINITE);
        tT.setAutoReverse(true);
        tT.play();
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

This is the output:


No comments:

Post a Comment