0

I am working on a slots application and I am using .jpg's of cards to display the symbols. When the slot machine is "run", I want to animate it so that the card turns over and briefly displays a new image (the back of the card), before it turns over again to display a new card. I have gotten the card to flip, but not display the back of the card or the new card. I have also gotten the card's to display new cards, but not flip.

The images are children ImageViews, which furthermore are children of HBoxes. I am trying to update the image during the animation, but I am receiving this exception:

Not on FX application thread; currentThread = Timer-0

Here is my code:

public void spin(ActionEvent actionEvent){
        int bet = Integer.parseInt(betField.getText());
        slotMachine.play(bet);
        
        for (HBox box : hboxesList){
            rotateCard(box);
        }

        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                displayBackOfCard();
            }
        };

        TimerTask timerTask2 = new TimerTask() {
            @Override
            public void run() {
                updateCardsDisplay();
                updateStats();
                updateUserState();
            }
        };

        timer.schedule(timerTask, 1000);
        timer.schedule(timerTask2, 3000);
    }

public void updateCardsDisplay(){
        for (HBox box : hboxesList) {
            box.getChildren().set(0, createImageView(slotMachine.getSymbols().get(hboxesList.indexOf(box))));
        }
    }

    public void displayBackOfCard(){
        for (HBox box : hboxesList) {
            box.getChildren().set(0, createImageView("backOfCard"));
        }
    }

private ImageView createImageView(String imageName){
        ImageView imageView = new ImageView(new Image(SlotsDisplay.class.getResourceAsStream("/images/cards/" + imageName + ".jpg")));
        imageView.setFitWidth(148);
        imageView.setFitHeight(210);
        return imageView;
    }

    private void rotateCard(Node card) {
        RotateTransition rotator = new RotateTransition(Duration.millis(3000), card);
        rotator.setAxis(new Point3D(0,5,0));
        rotator.setFromAngle(0);
        rotator.setToAngle(360);
        rotator.setInterpolator(Interpolator.LINEAR);
        rotator.setCycleCount(1);
        rotator.play();
    }

As mentioned, the animation, rotateCard(), works, but the displayBackOfCard, which works when not using an animation, throws the exception above. It seems as if it's a timer problem, since neither timerTask1 nor timerTask2 will run without throwing the same exception, am I correct?

How can I fix this? How can I update the card mid-flip?

Thanks

  • 2
    Don't use timers. If you want to create an animation in JavaFX, use the `javafx.animation` API. – James_D Oct 11 '21 at 18:07
  • 2
    (Be sure to read [this answer](https://stackoverflow.com/questions/9966136/javafx-periodic-background-task/60685975#60685975) to the linked question.) – James_D Oct 11 '21 at 18:13

0 Answers0