亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在帶有動畫的 JavaFX 應用程序中為字符創建老虎機?

如何在帶有動畫的 JavaFX 應用程序中為字符創建老虎機?

UYOU 2022-05-12 16:37:08
我正在制作一個 JavaFX 應用程序。我在主窗口中有一個標簽。我有一個從字母表生成隨機字符的方法,但我想給它添加一些動畫。我的意思是在 2 秒內在標簽中旋轉字符,然后出現一些隨機字符。動畫就像在真正的老虎機中一樣。我沒有找到任何這樣的圖書館我怎樣才能做到這一點?@FXML private Label answerID;//generate random character and apply it to the labelprivate void generateChar() {Random r = new Random();String alphabet = "ABCDEFGHIKLMNOPQRSTUXYZ";for (int i = 0; i < 25; i++) {  String text = "" + alphabet.charAt(r.nextInt(alphabet.length()));  answerID.setText(text);    }  }
查看完整描述

1 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

一個基本的設置可以包括一個Slot代表一個槽,由一個Text被扭曲的節點組成StackPane。

ASlot可以對 應用動畫Text并為其設置一個隨機字母。

ATilePane包含所有插槽,因此所有插槽都具有相同的大?。?/p>


import java.util.Random;

import javafx.animation.Interpolator;

import javafx.animation.SequentialTransition;

import javafx.animation.TranslateTransition;

import javafx.application.Application;

import javafx.geometry.Orientation;

import javafx.scene.Node;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.StackPane;

import javafx.scene.layout.TilePane;

import javafx.scene.text.Text;

import javafx.stage.Stage;

import javafx.util.Duration;


public class FxTest extends Application {


    public  static final String ALFA_BET = "ABCDEFGHIKLMNOPQRSTUXYZ";

    private final Random rnd = new Random();

    private TilePane main;


    @Override

    public void start(Stage primaryStage)throws Exception{


        main = new TilePane(); //use tile pane: all tiles have same size

        main.setPrefColumns(ALFA_BET.length());

        main.setOrientation(Orientation.HORIZONTAL);

        main.setHgap(1);  main.setVgap(10); //vertical and horizontal space

        makeSlots();


        Button add = new Button("Spin");

        add.setOnAction(e -> spin());


        BorderPane root = new BorderPane(main);

        root.setBottom(add);

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);

        //primaryStage.sizeToScene();

        primaryStage.show();

    }


    private void spin() {

        for(Node node : main.getChildren()){

            ((Slot)node).spin();

        }

    }


    private void makeSlots() {

        for (int i=0; i< ALFA_BET.length(); i++){

            main.getChildren().add(new Slot());

        }

    }


    public static void main(String[] args) {

        launch(null);

    }


    class Slot extends StackPane{


        private final Text text;

        private static final String format = "%1S";

        private static final double WIDTH = 30, TRANS_SIZE = 30;

        private SequentialTransition animation;


        Slot(){

            text = new Text();

            setRandomText();

            getChildren().add(text);

            setPrefWidth(WIDTH);

            //better apply using css

            setStyle("-fx-padding: 5;" +

                    "-fx-border-style: solid inside;" +

                    "-fx-border-width: 2;" +

                    "-fx-border-insets: 5;" +

                    "-fx-border-radius: 5;" +

                    "-fx-border-color: blue;"+

                    //all letters have the same width

                    "-fx-font-family: 'monospaced';");

            initializeAnimation();

        }


        //define animation to be applied to text

        private void initializeAnimation() {

            TranslateTransition t1 = new TranslateTransition();

            t1.setDuration(Duration.millis(1));

            t1.setNode(text);

            t1.setFromX(0);

            t1.setFromY(0);

            t1.setToX(0);

            t1.setToY(TRANS_SIZE);

            t1.setInterpolator(Interpolator.LINEAR);


            TranslateTransition t2 = new TranslateTransition();

            t2.setDuration(Duration.millis(300));

            t2.setNode(text);

            t2.setFromX(0);

            t2.setFromY(-TRANS_SIZE);

            t2.setToX(0);

            t2.setToY(TRANS_SIZE);

            t2.setInterpolator(Interpolator.LINEAR);


            TranslateTransition t3 = new TranslateTransition();

            t3.setDuration(Duration.millis(1));

            t3.setNode(text);

            t3.setFromX(0);

            t3.setFromY(TRANS_SIZE);

            t3.setToX(0);

            t3.setToY(0);

            t3.setInterpolator(Interpolator.LINEAR);

            //to play animations one by one

            animation = new SequentialTransition(t1, t2, t3);

        }


        void spin() {

            animation.play(); //animate

            animation.setOnFinished(e-> setRandomText()); //change letter whaen animation ends

        }

        void setRandomText(){

            char letter =  ALFA_BET.charAt(rnd.nextInt(ALFA_BET.length()));

            setText(letter );

        }


        void setText(char c){

            text.setText(String.format(format, c) );

        }

    }

}


查看完整回答
反對 回復 2022-05-12
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號