Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

javafx - (Java) how to update dice image when button clicked and equal to number given?

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.

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
                }

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.

Image of my application: https://gyazo.com/14708712af9a3858c5deed4a1bc508c6

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



}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...