在JavaFX中将形状路径过渡设置为另一个形状旋转

时间:2015-03-30 22:42:02

标签: javafx shapes transitions

我创建了一个圆圈,前面有一个三角形,随圆圈前后移动。我现在想要的是在圆圈旋转时使三角形轨道成圆形。我的第一个想法是,这将需要PathTransition和绑定,但到目前为止,我没有运气。这是我的代码:

public class FXTest2 extends Application {
@Override
public void start(Stage primaryStage) {

    Pane pane = new Pane();


    Ellipse ell = new Ellipse();
    ell.setCenterX(850);
    ell.setCenterY(500);
    ell.setRadiusX(480);
    ell.setRadiusY(480);
    ell.setFill(Color.TRANSPARENT);
    ell.setStroke(Color.BLACK);

    Circle circ1 = new Circle(500.0f, 500.0f, 25.0f);
    circ1.setFill(Color.GREEN);

    Path path = new Path();

    Polygon tri1 = new Polygon(530, 495, 530, 505, 540, 500);
    tri1.translateXProperty().bind(circ1.translateXProperty());
    tri1.translateYProperty().bind(circ1.translateYProperty());
    PathTransition pathMove = new PathTransition();
    pathMove.setNode(tri1);
    pathMove.setPath(circ1.getRotate());

    circ1.setOnKeyPressed((e) -> {
        if(e.getCode() == KeyCode.UP) {
            circ1.setTranslateX(circ1.getTranslateX() - 15);
        }
        else if(e.getCode() == KeyCode.LEFT) {
            circ1.setRotate(circ1.getRotate() - 15);
        }
        else if(e.getCode() == KeyCode.RIGHT) {
            circ1.setRotate(circ1.getRotate() + 15);
        }
        else if(e.getCode() == KeyCode.DOWN) {
            circ1.setTranslateX(circ1.getTranslateX() + 15);
        }

    });

    pane.getChildren().addAll(ell, circ1, tri1);

    Scene scene = new Scene(bpane, 1700, 1000); 
    scene.getStylesheets().add("NewFile.css");

    primaryStage.setScene(scene);
    primaryStage.setTitle("FXTest2");
    primaryStage.show();

    circ1.requestFocus();


}

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

}

1 个答案:

答案 0 :(得分:2)

您需要围绕圆心旋转三角形。要执行此操作,请创建Rotate变换,将其轴心点绑定到圆的中心,并将其角度调整为圆圈的旋转量。然后只需将旋转变换添加到三角形变换列表中:

Rotate rotate = new Rotate();
rotate.pivotXProperty().bind(circ1.centerXProperty());
rotate.pivotYProperty().bind(circ1.centerYProperty());
rotate.angleProperty().bind(circ1.rotateProperty());

tri1.getTransforms().add(rotate);