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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…