路径Tansition绑定

时间:2015-05-03 19:46:38

标签: java animation binding javafx

所以我想用路径转换和绑定创建一个钟摆。我试图将线的末端绑定到圆的中心,但是当我这样做时,线不会移动。我还尝试了线路本身的另一个路径转换,但随后它将从其中心移动。我是java的新手,所以我不知道如何解决这个问题。

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.sun.javafx.geom.Arc2D;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.StrokeType;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Pendulum extends Application{   

@Override
public void start(Stage primaryStage){
    Pane root = new Pane();
    root.setPrefSize(400,400);
    Arc arc = new Arc(200, 200, 105, 75, 0, -180);
    arc.setType(ArcType.OPEN);
    arc.setStroke(Color.BLACK);
    arc.setFill(Color.WHITE);
    arc.setStrokeWidth(2);
    Circle cir = new Circle(200,275,10);
    cir.setFill(Color.BLACK);
    Line line = new Line(200,100,200,275);
    PathTransition pt = new PathTransition (Duration.millis(2000),arc,cir);
    pt.setCycleCount(Timeline.INDEFINITE);
    pt.setAutoReverse(true);
    pt.play();
    line.endXProperty().bind(cir.centerXProperty());
    line.endYProperty().bind(cir.centerYProperty());
    root.getChildren().addAll(arc,cir,line);

    Scene scene = new Scene(root, 400, 400);
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);

}

}

1 个答案:

答案 0 :(得分:1)

PathTransition的工作原理是更新正在设置动画的节点的translateXtranslateY属性。因此,centerXcenterY永远不会改变。以下修复将起作用:

    line.endXProperty().bind(cir.centerXProperty().add(cir.translateXProperty()));
    line.endYProperty().bind(cir.centerYProperty().add(cir.translateYProperty()));