(Java)如何在点击按钮时更新骰子图像并等于给定的数字?

时间:2018-04-25 11:29:10

标签: java javafx

我正在尝试制作一个简单的骰子游戏。目前我的骰子完全正常工作。我想做的最后一件事是使用图像作为骰子。

这是我的骰子代码(在//我希望它为骰子添加一个名为Alea_1.png的图像

public class dice {

public static int rollDice(int number, int nSides) {



    int num = 0;
    int roll = 0;
    Random r = new Random();
    if (nSides >= 3) {
        for (int i = 0; i < number; i++) {

            roll = r.nextInt(nSides) + 1;
            System.out.println("Roll is:  " + roll);
            num = num + roll;
            if (roll == 1) {
                //insert new image Alea_1.png
                }

我的主要文件是berekenen.java。目前我可以在初级阶段显示骰子,但因为它在开始时加载我无法改变它。

我的申请图片: https://gyazo.com/14708712af9a3858c5deed4a1bc508c6

如果您需要,这是代码:

public class berekenen extends Application implements EventHandler <ActionEvent> {


public static int ballance = 5000;

HBox hb = new HBox();
Button bopnieuw = new Button("opnieuw");
TextField tf1 = new TextField();
TextField tf2 = new TextField(String.valueOf("Ballance:" + ballance));
TextField tf3 = new TextField();

public static void main(String[] args) {
    launch(args);
}


public dice dice123 = new dice();


public void handle(ActionEvent event) {
    int gekozen = Integer.parseInt(tf3.getText());
    int result = dice123.rollDice(1,6);
    if (event.getSource() == bopnieuw) {
        tf1.setText(result + "");
        if (dice.check(gekozen,result))  {
        ballance = ballance + 100;
            System.out.println(ballance);
            tf2.setText(toString().valueOf("Ballance:" + ballance));
        }else{
            ballance = ballance - 20;
            System.out.println(ballance);
            tf2.setText(toString().valueOf("Ballance:" + ballance));
        }
    }
}

public void start(Stage primaryStage) throws Exception {

    primaryStage.setTitle("Mijn eerste javaFX application");
    primaryStage.setMaxHeight(8000);
    primaryStage.setMaxWidth(8000);
    primaryStage.getIcons().add(new Image(""));
    primaryStage.show();
    StackPane pane = new StackPane();
    Image image = new Image("Alea_1.png");
    ImageView iv1 = new ImageView();
    iv1.setImage(image);
    bopnieuw.setOnAction(this);
    hb.getChildren().addAll(tf1, tf2, tf3);
    hb.getChildren().addAll(bopnieuw);
    pane.getChildren().addAll(hb, iv1);
    Scene scene = new Scene(pane, 1000, 400);
    primaryStage.setScene(scene);
    primaryStage.show();



}

}

1 个答案:

答案 0 :(得分:0)

以下是我用来练习不同想法的一些代码。此代码以某种方式加载图像,以便它们可以与多个骰子一起使用。它使用JavaFX periodic background task (可能是我最喜欢和最常用的答案在这个网站上)在掷骰子时翻转图像。

  

DieImages类:用于加载图像

import javafx.scene.image.Image;

/**
 *
 * @author blj0011
 */
public class DieImages
{
    final Image die1 = new Image(getClass().getResourceAsStream("dieRed1.png"));
    final Image die2 = new Image(getClass().getResourceAsStream("dieRed2.png"));
    final Image die3 = new Image(getClass().getResourceAsStream("dieRed3.png"));
    final Image die4 = new Image(getClass().getResourceAsStream("dieRed4.png"));
    final Image die5 = new Image(getClass().getResourceAsStream("dieRed5.png"));
    final Image die6 = new Image(getClass().getResourceAsStream("dieRed6.png"));

    final Image[] images = new Image[6];

    public DieImages()
    {
        images[0] = die1;
        images[1] = die2;
        images[2] = die3;
        images[3] = die4;
        images[4] = die5;
        images[5] = die6;
    }

    public Image[] getImages()
    {
        return images;
    }
}
  

模具类:

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 *
 * @author blj0011
 */
public class Die
{
    ImageView dieFace;
    Image[] images;

    public Die(Image[] images)
    {
        this.images = images;
        dieFace = new ImageView(this.images[0]);//set default to image 0
    }

    public Die(Image[] images, int dieFaceValue)
    {
        //Need to catch for values less than 1 and greater than 6!
        this.images = images;
        dieFace = new ImageView(this.images[dieFaceValue - 1]);
    }

    public ImageView getdieFace()
    {
        return dieFace;
    }

    public void setDieFace(int dieFaceValue)
    {
        //Need to catch for values less than 1 and greater than 6!
        dieFace.setImage(this.images[dieFaceValue - 1]);
    }
}
  

主要

import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication173 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        DieImages images = new DieImages();
        Die die = new Die(images.getImages());
        StackPane stackPane = new StackPane(die.getdieFace());//Add ImageView(Die) to stackPane
        VBox.setVgrow(stackPane, Priority.ALWAYS);

        Button btn = new Button();
        btn.setText("Roll Die");
        btn.setOnAction((ActionEvent event) -> {
            btn.setDisable(true);//Disable Button
            Random random = new Random();
            Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.3), (actionEvent) -> {
                int tempRandom = random.nextInt(6) + 1;
                System.out.println(tempRandom);
                die.setDieFace(tempRandom);
            }));

            timeline.setCycleCount(random.nextInt(20) + 1);
            timeline.play();
            timeline.setOnFinished(actionEvent -> {
                btn.setDisable(false);//Enable Button
            });
        });

        VBox root = new VBox(stackPane, new StackPane(btn));
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
    * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

在您的情况下,您可能没有Timeline