(Java)如何在点击按钮时更新骰子图像并等于给定的数字? [英] (Java) how to update dice image when button clicked and equal to number given?

查看:222
本文介绍了(Java)如何在点击按钮时更新骰子图像并等于给定的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a simple dice game. Currently I have the dice fully working. The last thing I want to do is using an image as dice.

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

Here is my code for the dice ( at // I want it to add the image for dice one called 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。目前我可以在主要舞台上显示骰子,但因为它在开始时加载我无法改变它。

My main file is berekenen.java. Currently I can display the dice on the primary stage but because it loads in at start I can't change it.

我的申请图片:
https:// gyazo.com/14708712af9a385 8c5deed4a1bc508c6

如果您需要,这是代码:

This is the code if you need it:

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



}

}

推荐答案

以下是我用来练习不同想法的一些代码。此代码以某种方式加载图像,以便它们可以与多个骰子一起使用。它使用 JavaFX定期后台任务
(可能是我最喜欢和最常用的答案)滚动模具时翻转图像。

Here is some code that I used to practice different ideas. This code loads images in a way so that they can be used with multiple dice. It uses JavaFX periodic background task (probably my favorite and most used answer on this site) to flip through the images when rolling the die.


DieImages类:用于加载图像

DieImages Class: Used to load Images



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




模具类:

Die Class:



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




Main

Main



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

}

在你的情况下,你可以没有时间轴

In your case, you can probably do without the Timeline.

这篇关于(Java)如何在点击按钮时更新骰子图像并等于给定的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆