0

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.

DucCuong
  • 638
  • 1
  • 7
  • 26
  • Can't you just calculate it by using `wheel.getRotate()`? – James_D May 21 '14 at 16:43
  • Did my code help you? Was it what you were looking for? – Alexandre Santos May 21 '14 at 23:24
  • @James_D As the rotate transition does not start from 0, so I don't think I can use this simple solution to calculate. – DucCuong May 22 '14 at 02:59
  • Assuming you're not adding rotations other than from your animations, `getRotate()` will give you the total rotation from the original start position. Unless I'm missing something, `(getRotate() % 360)/24` should give you the number of segments by which the wheel is offset. – James_D May 22 '14 at 03:22
  • That was the code I wrote. Just replace the my "angle" with getRotate – Alexandre Santos May 22 '14 at 04:55

1 Answers1

-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]);
    }
}
Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64