Question: Using the Java FX code below make the following additions: 1. add a button to roll all 5 dice 2. Let the user select which
Using the Java FX code below make the following additions:
1. add a button to roll all 5 dice
2. Let the user select which dice they would like to hold before next roll
3.let user roll 3 times per round. 10 rounds
JAVA FX Code:
import java.util.Random;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class DiceGame extends Application {
Image images[];
ImageView imageViews[];
/**
* method to read dice images from files
*/
public void setupDiceImages() {
images = new Image[6];
/**
* loading each dice images into the Image object. Please replace the
* path with your dice images path. Dont remove the prefix 'file:'
*/
images[0] = new Image("file:D:\\work\\June 2018\\javafx dice\\dice\\1.png");
images[1] = new Image("file:D:\\work\\June 2018\\javafx dice\\dice\\2.png");
images[2] = new Image("file:D:\\work\\June 2018\\javafx dice\\dice\\3.png");
images[3] = new Image("file:D:\\work\\June 2018\\javafx dice\\dice\\4.png");
images[4] = new Image("file:D:\\work\\June 2018\\javafx dice\\dice\\5.png");
images[5] = new Image("file:D:\\work\\June 2018\\javafx dice\\dice\\6.png");
}
/**
* method to roll the 5 dices and display 5 random dice roll images in the
* imageviews
*/
public void roll() {
Random random = new Random();
for (int i = 0; i < imageViews.length; i++) {
/**
* generating a value between 0 and 5
*/
int index = random.nextInt(6);
/**
* setting dice image at this random index as current imageview's
* image
*/
imageViews[i].setImage(images[index]);
}
}
@Override
public void start(Stage primaryStage) {
//reading dice images
setupDiceImages();
//setting up 5 imageviews for displaying dices
imageViews = new ImageView[5];
/**
* initializing imageviews
*/
for (int i = 0; i < imageViews.length; i++) {
imageViews[i] = new ImageView();
//setting height and width
imageViews[i].setFitHeight(150);
imageViews[i].setFitWidth(150);
}
/**
* Defining an HBox to arrange all imageviews
*/
HBox hBox = new HBox(imageViews);
hBox.setSpacing(20);//space between each elements
//rolling the 5 dices
roll();
/**
* creating a pane and adding this hbox to it
*/
Pane pane = new Pane(hBox);
/**
* defining and displaying the scene
*/
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Modify this code to add the requirements above. The code only rolls the dice so far.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
