旋转转换JavaFX后获取坐标

时间:2014-05-21 16:38:34

标签: javafx coordinate-systems rotateanimation

我正在编写像Wheel of Fortune这样的游戏,其中Wheel实际上是一个JavaFX ImageView,它位于窗口的最右侧。这个轮子由15块(每个24度)组成。

Wheel Portion

基本上,在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);
    }
}

任何人都可以告诉我一个正确的方法来确定坐标或类似的东西,以帮助您确定分数。

1 个答案:

答案 0 :(得分:-1)

public static void main(String[] args)  {
    String[] prizes = new String[] {"100","200","300","400","500","600"};
    int slotSize = 360 / prizes.length;
    int angle = 359;
    for (int i = 0; i < 3600; i++) // 10 spins
    {
        angle++;
        if (angle>359) {
            angle = 0;
        }
        // based on the current angle, determine the entry:
        int entry = angle / slotSize;
        System.out.println("angle "+angle+" entry "+entry+" prize "+prizes[entry]);
    }
}