使用PathTransition的javafx动画并不流畅

时间:2016-02-15 00:05:27

标签: animation javafx

我正在学习javafx.animation。我创建了一个椭圆和一个矩形,并沿着椭圆的边界进行矩形移动。我让这个动画无限重复。问题是动画圈之间的过渡并不顺畅。这意味着在第一轮动画结束后,下一轮动画开始需要相当长的时间。这看起来很难看。反正有没有让它更顺利?

提前谢谢。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.animation.PathTransition;
import javafx.util.Duration;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;

public class TestAnimation extends Application {
    @Override
    public void start(Stage stage){
        //create pane and nodes
        Pane pane = new Pane();
        Ellipse ellipse = new Ellipse(300, 300, 100, 200);
        ellipse.setFill(Color.BLACK);
        Rectangle rect = new Rectangle(50, 80);
        rect.setFill(Color.CRIMSON);
        pane.getChildren().addAll(ellipse, rect);

        //create animation
        PathTransition path = new PathTransition(Duration.millis(2400), ellipse, rect);
        path.setCycleCount(Timeline.INDEFINITE);
        //path.setAutoReverse(true);
        path.play();

        //define mouse event
        ellipse.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                path.pause();
            }
        });

        ellipse.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                path.play();
            }
        });

        //set up stage
        Scene scene = new Scene(pane, 600, 600);
        stage.setScene(scene);
        stage.show();
    }
}

0 个答案:

没有答案
相关问题