Question

Problem 1/5 (20 points) Create a JavaFX application that simulates the rolling of a pair of dice. When the user clicks a butt
here is the dice images you need to use


image.png
image.png
image.png
image.png
image.png
image.png
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

VERY IMPORTANT NOTE: Place the required images named 1.png, 2.png… 6.png in the root folder of your IDE. (If you are using NetBeans, copy the images and go to net beans, choose your project, go to Files tab on the left, right click on the project folder and click paste. If you do it right, there will be other files like build.xml, manifest.mf in that same folder), If you do it wrong, then the images will not be displayed. If you are finding it difficult to get this right, give absolute file path (complete file name including all folders) while loading the images. Like instead of this: new Image("file:1.png");, do like this: new Image("file:C/Desktop/Images/1.png"); if the images are in C/Desktop/Images folder (example).

// RollingDice.java

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.BorderPane;

import javafx.stage.Stage;

public class RollingDice extends Application {

   

    //loading dice images into an array. please follow the instructions mentioned above.

    private Image images[] = {new Image("file:1.png"),

        new Image("file:2.png"), new Image("file:3.png"),

        new Image("file:4.png"), new Image("file:5.png"),

        new Image("file:6.png")};

    @Override

    public void start(Stage primaryStage) {

        //creating a border pane

        BorderPane root = new BorderPane();

        //two image views

        ImageView iv1=new ImageView();

        ImageView iv2=new ImageView();

        //fixing height and width of image views

        iv1.setFitHeight(200);

        iv1.setFitWidth(200);

        iv2.setFitHeight(200);

        iv2.setFitWidth(200);

       

        //toss button

        Button toss=new Button("Toss");

        //adding action listener to toss button

        toss.setOnAction(e->{

            //generating two valid indices between 0 and images.length-1

            int index1=(int) (Math.random()*images.length);

            int index2=(int) (Math.random()*images.length);

            //setting images at these indices in image views

            iv1.setImage(images[index1]);

            iv2.setImage(images[index2]);

        });

       

        //adding image views to left and right, button to the bottom

        root.setLeft(iv1);

        root.setRight(iv2);

        root.setBottom(toss);

        //aligning button to center

        BorderPane.setAlignment(toss, Pos.CENTER);

        //adjusting spacing

        BorderPane.setMargin(iv1, new Insets(20));

        BorderPane.setMargin(iv2, new Insets(20));

        root.setPadding(new Insets(20));

       

        //setting up and displaying a scene

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);

        primaryStage.setTitle("Rolling Dice");

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

/*OUTPUT*/


Rolling Dice x Toss

Add a comment
Know the answer?
Add Answer to:
here is the dice images you need to use Problem 1/5 (20 points) Create a JavaFX...
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
  • in JAVA please and please show output!! Create a JavaFX application that simulates the rolling of...

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

  • Write a JavaFX program to display 5 labels and 3 buttons. You can use any kind...

    Write a JavaFX program to display 5 labels and 3 buttons. You can use any kind of Pane you like to use. Display 3 red circles and 2 blue rectangles (pick your own size). Use the Image and ImageView classes (explained in section 14.9 of your textbook) to show 2 images of your choice. Include your first and last name somewhere on the frame, label, button, etc. We won’t discuss handing events where one would be able to click buttons...

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

  • C+ HelloVisualWorld, pages 121-125 (in the 3-6 Deciding Which Interface to Use Section) We were unable...

    C+ HelloVisualWorld, pages 121-125 (in the 3-6 Deciding Which Interface to Use Section) We were unable to transcribe this imageCHAPTER 3 Using Guy Objects and the Visual Studio IDE Using Gul Objects (continued) Forn click the Form, its Properties window appears in the lower right portion of the screen, and you can see that the Text property for the Form IS set to Forml. Take a moment to scroll through the list in the Properties Window, examining the values of...

  • 5. -16.66 points ASWSBE13 13.E.018 You may need to use the appropriate technology to answer this...

    5. -16.66 points ASWSBE13 13.E.018 You may need to use the appropriate technology to answer this question To test for any significant difference in the number of hours between breakdowns for four machines, the following data were + My Notes Ask Your Teacher obtained Machine Machine Machine Machine 2 3 4 6.7 9.1 10.8 9.9 8.1 7.6 9.8 10.3 9.4 10.2 10.1 12.8 5.6 7.8 8.8 9.4 10.1 8.9 8.9 12.2 10.9 11.4 11.2 7.4 (a) At the α =...

  • 1. + -/16.66 points ASWSBE13 13.Е.023.MI My Notes Ask Your Teacher You may need to use...

    1. + -/16.66 points ASWSBE13 13.Е.023.MI My Notes Ask Your Teacher You may need to use the appropriate technology to answer this question An experiment has been conducted for four treatments with seven blocks. Complete the following analysis of variance table (Round your values for mean squares and F to two decimal places, and your p-value to three decimal places.) Source of Variation Sum Degrees of Freedom Mean p-value of Squares Square Treatments 900 Blocks 500 Error Total 1,800 Use...

  • please I need help for my project ACC 112 Project 1B The below represents the comparative...

    please I need help for my project ACC 112 Project 1B The below represents the comparative financial statements of Kamla Corporation. Kamla Corporation Comparative Income Statement For the Years Ended December 31, 2016 and 2015 2016 2015 Net sales (all on account) $1,178,000 $1,034,000 Expenses: Cost of goods sold $808,000 $710,000 Selling and administrative 209,000 225,000 Interest expense 12,000 14,000 Income tax expense 33,400 23,000 Total expenses $1,062,400 $972,000 Net income $115,600 $62,000 2015 $37,000 28,600 149,400 138,800 353,800 782,000...

  • I want to make a really simple maze game in Python. I need to use tkinter and GUI to make this ha...

    I want to make a really simple maze game in Python. I need to use tkinter and GUI to make this happened. Under you can see the description of the task, but i need help to getting startet. If there is an other easier way I'm just happy for the help!! task Description: You have to create a maze game. The goal of the game is to get out of the maze. The game should read The maze from a...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • Please use own words. Thank you. CASE QUESTIONS AND DISCUSSION > Analyze and discuss the questions...

    Please use own words. Thank you. CASE QUESTIONS AND DISCUSSION > Analyze and discuss the questions listed below in specific detail. A minimum of 4 pages is required; ensure that you answer all questions completely Case Questions Who are the main players (name and position)? What business (es) and industry or industries is the company in? What are the issues and problems facing the company? (Sort them by importance and urgency.) What are the characteristics of the environment in which...

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