Question
in JAVA please and please show output!!

Create a JavaFX application that simulates the rolling of a pair of dice. When the user clicks a button, the application should generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. Use ImageView component to display the dice. Six images are included in the project folder for you to use. For example, the first picture below is the initial window, after clicking the “Toss” button, the second picture comes up, which means the numbers of these two dice are 2 and 3.

Create a JavaFX application that simulates the rolling of a pair of dice. When the user clicks a button, the application shou
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You.

RollingDice.java

package gui;


import java.util.Random;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
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 RollingDice extends Application{
   public static void main(String[] args) throws Exception { launch(args); }
   @Override
   public void start(final Stage stage) throws Exception {

       Pane pane = new HBox(10);
       pane.setPadding(new Insets(5, 5, 5, 5));
       Button button = new Button("Toss");
       button.setMinWidth(100);
      
      
      
       ImageView dice1 = new ImageView();
       pane.getChildren().add(dice1);
       dice1.setFitWidth(100);
       dice1.setFitHeight(100);
       dice1.setVisible(false);


       ImageView dice2 = new ImageView();
       pane.getChildren().add(dice2);
       dice2.setFitWidth(100);
       dice2.setFitHeight(100);
       dice2.setVisible(false);
      
       pane.getChildren().add(button);
      
       button.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent e) {
               try{
                   Random rand = new Random();
                   int i = rand.nextInt(6)+1;
                   dice1.setVisible(true);
                   dice1.setImage(new Image("Die"+i+".png"));
                   i = rand.nextInt(6)+1;
                   dice2.setVisible(true);
                   dice2.setImage(new Image("Die"+i+".png"));
               }catch(Exception ex){
                   System.out.println("Error");

               }
           }
       });


      

       Scene scene = new Scene(pane,350,150);
       stage.setTitle("Roll Dice");
       stage.setScene(scene);
       stage.show();
   }
}

Roll Dice X Toss i ol ren() .add (dice2);

where images are as below

you can have image in any type

Add a comment
Know the answer?
Add Answer to:
in JAVA please and please show output!! Create a JavaFX application that simulates the rolling of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • here is the dice images you need to use Problem 1/5 (20 points) Create a JavaFX...

    here is the dice images you need to use Problem 1/5 (20 points) Create a JavaFX application that simulates the rolling of a pair of dice. When the user clicks a button, the application should generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. Use ImageView component to display the dice. Six images are included in the project folder for you to use. For example, the first picture below is...

  • Heads or Tails Create a JavaFX application that simulates a coin being tossed. When the user clic...

    Heads or Tails Create a JavaFX application that simulates a coin being tossed. When the user clicks a button, the application should generate a random number in the range of 0 to 1. If the number is 0, the coin has landed on “heads,” and if the number is 1, the coin has landed on “tails.” Use an ImageView component, and the coin images that you will find in this book’s Student Sample Programs to display the side of the...

  • JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming...

    JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming and image handling using JavaFX. Create an application that responds to the user clicking command buttons. Initially, it will display one of two graphic images and, based upon the button clicked by the user, will switch the image displayed. If the user clicks the mouse button to display the same image as is currently displayed, the image is not changed, but a message at...

  • For this question you will need to complete the methods to create a JavaFX GUI application...

    For this question you will need to complete the methods to create a JavaFX GUI application that implements two String analysis algorithms. Each algorithm is activated when its associated button is pressed. They both take their input from the text typed by the user in a TextField and they both display their output via a Text component at the bottom of the GUI, which is initialized to “Choose a string methods as indicated in the partially completed class shown after...

  • Java program that creates a photo album application.    Which will load a collection of images...

    Java program that creates a photo album application.    Which will load a collection of images and displays them in a album layout. The program will allow the user to tag images with metadata: •Title for the photo .      Limited to 100 characters. •Description for the photo . Limited to 300 characters. •Date taken •Place taken Functional Specs 1.Your album should be able to display the pictures sorted by Title, Date, and Place. 2.When the user clicks on a photo,...

  • C#: I am working on this application and keep running into this error. Please help me fix it. Err...

    C#: I am working on this application and keep running into this error. Please help me fix it. Error: Design: Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SinghAjayProgram08 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double totalInserted = 0; double totalWon = 0; private void spinButton_Click(object sender, EventArgs e) { Random rand = new Random(); int index = rand.Next(fruitsImageList.Images.Count); firstPictureBox.Image =...

  • ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button...

    ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button to a frame in the FXBookQuote2 program. When the user clicks the button, display the title of the book that contains the opening sentence or two from your favorite book in the label. FXBookQuote2.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the...

  • please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....

    please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1. Write a program for the following. NOTE that some of these steps are not dependent on each other. Using methods is mandatory. Make sure to use methods where it makes sense. a. Ask the user for a series of integers entered from the keyboard. Use a sentinel value such as 999 to end the input process. If the entered values are not integers, throw...

  • Create a windows form application in c# The form must look like a calculator and do...

    Create a windows form application in c# The form must look like a calculator and do the following. Add, subtract, multiply, and divide. Account for division by zero. Show at least two decimal places. Retain the last number on the window so that, when the user opens the calculator the next time, the number that was in the window at the last close appears. Have a memory, so that users can store values and recall them. Operate with a mouse...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT