Simple Animation2

 

スクリプト

simpleAnimation2.fx

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.ext.swing.SwingButton;
import javafx.scene.Scene;
import javafx.scene.effect.MotionBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.ext.swing.SwingToggleButton;
 
var x: Integer = 0;
 
var timeline = Timeline {
    // INDEFINITE にすることで無限に繰り返す
    repeatCount: Timeline.INDEFINITE
 
    keyFrames: [
        KeyFrame {
            time: 0s
            values: [
                x => 0
            ]
        },
        KeyFrame {
            time: 2s
            values: [
                x => 710
            ]
        }
    ]
};
 
Stage {
    title: "Simple Animation Sample"
    scene: Scene {
        width: 600
        height: 100
        content: [
            ImageView {
                translateX: bind x
                x: -110
                y: 45
                image: Image {
                    url: "{__DIR__}car.png"
                }
            },
            SwingButton {
                var running = false;
                translateX: 270
                translateY: 10
                text: bind if (running) "Stop" else "Start"
                action: function() {
                    if (running) {
                        // アニメーション中であれば一時停止
                        timeline.pause();
                        running = false;
                    } else {
                        // アニメーションを一時停止していたら、再開
                        timeline.play();
                        running = true;
                    }
                }
            }
        ]
    }
}