我正在写一个像命运之轮这样的游戏,轮子实际上是一个 JavaFX ImageView,位于窗口的最右侧。这个轮子由 15 片组成(每个 24 度)。
基本上,在 RotateTransition 完成后,我想获取 ImageView 的坐标或边界框,以便计算该坐标、图像中心和针之间的角度。从那个角度我可以确定返回的分数。
但是,我对如何做到这一点感到困惑。我试过玩边界框,但它并没有解决问题。
代码如下:
public class FXMLRoundSceneController {
@FXML
private AnchorPane backgroundAnchorPane;
@FXML
private ImageView wheel;
/* Wheel background Image*/
private Image img = new Image(getClass().getResource("/resources/WOF.png").toExternalForm());
@FXML
public void initialize() {
wheel.setImage(img);
}
@FXML
private void rotateWheel(MouseEvent mv) {
rotate(1355,5); // Just an example, the rotate angle should be randomly calculated on each MouseClicked event.
}
public void rotate(double angle, double duration) {
RotateTransition rt = new RotateTransition();
rt.setNode(wheel);
rt.setByAngle(angle);
rt.setDuration(Duration.seconds(duration));
rt.setCycleCount(1);
rt.setAutoReverse(false);
rt.setInterpolator(Interpolator.EASE_OUT);
rt.play();
rt.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ae) {
wheelBoundingBox();
}
});
}
public void wheelBoundingBox() {
double MX = wheel.getBoundsInParent().getMaxX();
double MY = wheel.getBoundsInParent().getMaxY();
double mX = wheel.getBoundsInParent().getMinX();
double mY = wheel.getBoundsInParent().getMinY();
double width = MX - mX;
double height = MY - mY;
Rectangle bb = new Rectangle(mX, mY, Color.TRANSPARENT);
bb.setWidth(width);
bb.setHeight(height);
bb.setStroke(Color.WHITE);
bb.setStrokeWidth(1);
backgroundAnchorPane.getChildren().add(bb);
}
}
谁能告诉我确定坐标的正确方法或类似的方法来帮助您确定分数。