Getting Coordinates after Rotate Transition JavaFX

2.1k Views Asked by At

I'm writing a game like Wheel of Fortune, with the Wheel is actually an JavaFX ImageView lay outed to the right most of window. This wheel is made up of 15 piece (24 degree for each).

Wheel Portion

Basically, after RotateTransition finish I want to get the coordinate, or bounding box of something like this of the ImageView in order to calculate the angle between that coordinate, the center of the image, and the needle. From that angle I can determine the score returned.

However, I'm confused on how to do this. I've tried to play around with bounding box, but it just doesn't solve the problem.

The code for this is as follow :

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);
    }
}

Can anyone tell me a proper way to determine the coordinate or something like that to help you determine the score.

1

There are 1 best solutions below

0
On
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]);
    }
}