0
我已經創建了一個圓形,它前面有一個三角形,它與圓形一起向前和向後移動。我現在想要的是當圓形旋轉時,使三角形繞着圓形軌道運行。我的第一個想法是,這將需要PathTransition和綁定,但到目前爲止,我沒有運氣。這裏是我的代碼:在JavaFX中設置形狀路徑轉換爲其他形狀的旋轉
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);
}
}
這是完美的謝謝。 – Robert 2015-03-30 23:11:01