Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Interpolator Demonstration
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Duration;
public class InterpolatorDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
Animation linear = createAnimation(root, 100.0, Interpolator.LINEAR);
Animation easein = createAnimation(root, 200.0, Interpolator.EASE_IN);
Animation easeout = createAnimation(root, 300.0, Interpolator.EASE_OUT);
Animation easeboth = createAnimation(root, 400.0, Interpolator.EASE_BOTH);
Animation discrete = createAnimation(root, 500.0, Interpolator.DISCRETE);
Button button = new Button("Start");
root.getChildren().add(button);
button.setTranslateX(20.0);
button.setTranslateY(20.0);
button.setOnAction(e -> {
if (linear.getStatus() == Animation.Status.RUNNING) {
linear.stop();
easein.stop();
easeout.stop();
easeboth.stop();
discrete.stop();
button.setText("Start");
} else {
linear.play();
easein.play();
easeout.play();
easeboth.play();
discrete.play();
button.setText("Stop");
}
});
linear.setOnFinished(e -> button.setText("Start"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setWidth(1_000);
stage.setHeight(650);
stage.show();
}
private Animation createAnimation(Group pane, double initY, Interpolator interpolator) {
ImageView duke = new ImageView(new Image("AnimalHouseDuke.png"));
pane.getChildren().add(duke);
duke.setTranslateX(20.0);
duke.setTranslateY(initY);
Label label = new Label(interpolator.toString());
pane.getChildren().add(label);
label.setTranslateX(20.0);
label.setTranslateY(initY + 80.0);
TranslateTransition animation = new TranslateTransition(Duration.millis(2_000), duke);
animation.setFromX(20.0);
animation.setToX(890.0);
animation.setInterpolator(interpolator);
return animation;
}
public static void main(String... args) {
launch(args);
}
}
@daveyostcom

This comment has been minimized.

Copy link

daveyostcom commented Oct 28, 2019

To avoid a very unhelpful exception, the createAnimation method can't be private. Also and there is no image, so I suggest a Rectangle.

  public Animation createAnimation(Group pane, double initY, Interpolator interpolator) {
    Rectangle track = new Rectangle(930, 60, Color.ALICEBLUE);
    track.setTranslateX(20);
    track.setTranslateY(initY);
    pane.getChildren().add(track);
//    URL dukeURL = getClass().getResource("AnimalHouseDuke.png");
//    ImageView duke = new ImageView(new Image(dukeURL.toExternalForm()));
    Rectangle duke = new Rectangle(60, 60);
    pane.getChildren().add(duke);
    duke.setTranslateX(20);
    duke.setTranslateY(initY);
    Label label = new Label(interpolator.toString());
    pane.getChildren().add(label);
    label.setTranslateX(20);
    label.setTranslateY(initY + 60);
    TranslateTransition animation = new TranslateTransition(Duration.millis(2_000), duke);
    animation.setFromX(20);
    animation.setToX(890);
    animation.setInterpolator(interpolator);
    return animation;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.