/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ package interpolators; import java.util.Random; import javafx.animation.Interpolator; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.beans.value.WritableDoubleValue; import javafx.geometry.Point3D; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.shape.Polyline; import javafx.scene.transform.Rotate; import javafx.scene.transform.Shear; import javafx.stage.Stage; import javafx.util.Duration; /** * * @author Aleksandr Sakharuk */ public class Interpolators extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Interpolator"); Group root = new Group(); VBox vBox = new VBox(60); HBox hBox1 = new HBox(); hBox1.getChildren().addAll(line1, new Label("(5.67, 1), (1, 1), (1, 0)")); HBox hBox2 = new HBox(); hBox2.getChildren().addAll(line2, new Label("(5.67, 0.58), (0.58, 1), (1, 0)")); HBox hBox3 = new HBox(); hBox3.getChildren().addAll(line3, new Label("(5.67, 0.58), (0.58, 0), (0, 0)")); HBox hBox4 = new HBox(); hBox4.getChildren().addAll(line4, new Label("(1, 1.73), (1.73, 0.51), (0.51, 0)")); vBox.getChildren().addAll(hBox1, hBox2, hBox3, hBox4); root.getChildren().addAll(vBox); root.translateYProperty().set(HEIGHT / 2); line1.getTransforms().add(new Rotate(180, new Point3D(1, 0, 0))); line2.getTransforms().add(new Rotate(180, new Point3D(1, 0, 0))); line3.getTransforms().add(new Rotate(180, new Point3D(1, 0, 0))); line4.getTransforms().add(new Rotate(180, new Point3D(1, 0, 0))); DoubleProperty dp1 = new SimpleDoubleProperty(); dp1.addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Number arg1, Number arg2) { //System.out.printf("%1$f %2$f\n", t, arg2); line1.getPoints().addAll(t, arg2.doubleValue()); t = tl.getCurrentTime().toSeconds() * (WIDTH - INDENT) / tl.getTotalDuration().toSeconds() + INDENT / 2; } private double t = INDENT / 2; }); DoubleProperty dp2 = new SimpleDoubleProperty(); dp2.addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Number arg1, Number arg2) { //System.out.printf("%1$f %2$f\n", t, arg2); line2.getPoints().addAll(t, arg2.doubleValue()); t = tl.getCurrentTime().toSeconds() * (WIDTH - INDENT) / tl.getTotalDuration().toSeconds() + INDENT / 2; } private double t = INDENT / 2; }); DoubleProperty dp3 = new SimpleDoubleProperty(); dp3.addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Number arg1, Number arg2) { //System.out.printf("%1$f %2$f\n", t, arg2); line3.getPoints().addAll(t, arg2.doubleValue()); t = tl.getCurrentTime().toSeconds() * (WIDTH - INDENT) / tl.getTotalDuration().toSeconds() + INDENT / 2; } private double t = INDENT / 2; }); DoubleProperty dp4 = new SimpleDoubleProperty(); dp4.addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Number arg1, Number arg2) { //System.out.printf("%1$f %2$f\n", t, arg2); line4.getPoints().addAll(t, arg2.doubleValue()); t = tl.getCurrentTime().toSeconds() * (WIDTH - INDENT) / tl.getTotalDuration().toSeconds() + INDENT / 2; } private double t = INDENT / 2; }); tl.getKeyFrames().addAll(new KeyFrame(Duration.seconds(1), new KeyValue(dp1, 50, Interpolator.TANGENT(Duration.millis(250), 5.67, Duration.millis(250), 1)), new KeyValue(dp2, 50, Interpolator.TANGENT(Duration.millis(250), 5.67, Duration.millis(250), 0.58)), new KeyValue(dp3, 50, Interpolator.TANGENT(Duration.millis(250), 5.67, Duration.millis(250), 0.58)), new KeyValue(dp4, 50, Interpolator.TANGENT(Duration.millis(250), 1, Duration.millis(250), 1.73))), new KeyFrame(Duration.seconds(1), new KeyValue(dp1, -50, Interpolator.TANGENT(Duration.millis(250), 1, Duration.millis(250), 1)), new KeyValue(dp2, -50, Interpolator.TANGENT(Duration.millis(250), 0.58, Duration.millis(250), 1)), new KeyValue(dp3, -50, Interpolator.TANGENT(Duration.millis(250), 0.58, Duration.millis(250), 0)), new KeyValue(dp4, -50, Interpolator.TANGENT(Duration.millis(250), 1.73, Duration.millis(250), 0.51))), new KeyFrame(Duration.seconds(1), new KeyValue(dp1, 50, Interpolator.TANGENT(Duration.millis(250), 1, Duration.millis(250), 0)), new KeyValue(dp2, 50, Interpolator.TANGENT(Duration.millis(250), 1, Duration.millis(250), 0)), new KeyValue(dp3, 50, Interpolator.TANGENT(Duration.millis(250), 0, Duration.millis(250), 0)), new KeyValue(dp4, 50, Interpolator.TANGENT(Duration.millis(250), 0.51, Duration.millis(250), 0)))); /*tl.getKeyFrames().add(new KeyFrame(Duration.seconds(2), new KeyValue(dp2, 100, Interpolator.TANGENT(Duration.millis(2000), 5.67, Duration.millis(500), 1))));*/ primaryStage.setScene(new Scene(root, WIDTH, HEIGHT)); tl.play(); primaryStage.show(); } private Polyline line1 = new Polyline(); private Polyline line2 = new Polyline(); private Polyline line3 = new Polyline(); private Polyline line4 = new Polyline(); private Timeline tl = new Timeline(); private static final int WIDTH = 500; private static final int HEIGHT = 500; private static final int INDENT = 100; }