Created
December 9, 2015 11:36
-
-
Save skrb/698adeb3ade7e7f2e004 to your computer and use it in GitHub Desktop.
Interpolator Demonstration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To avoid a very unhelpful exception, the
createAnimation
method can't beprivate
. Also and there is no image, so I suggest aRectangle
.