Question

Using JavaFX create a Roll application that implements the GUI shown When the user clicks the Rol...

Using JavaFX create a Roll application that implements the GUI shown When the user clicks the Roll button, the program must roll the dice, change the figures randomly, calculate the sum of the dice and show it in a label

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

The program is written in Java 8 using Eclipse IDE and SceneBuilder for UI design

Main Program

package rollDice;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;

public class RollDiceMain extends Application {
   @Override
   public void start(Stage primaryStage) {
       try {
           // loading the FXML File
           Parent root = FXMLLoader.load(getClass().getResource("RollDiceFXML.fxml"));
           Scene scene = new Scene(root);
           primaryStage.setTitle("Roll Dice");// setting title
           primaryStage.setScene(scene);
           primaryStage.setResizable(false);// seting resizable false
           primaryStage.show();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   public static void main(String[] args) {
       launch(args);
   }
}

Controller Program

package rollDice;

import java.io.File;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class RollDiceController {
  
@FXML
private ImageView imageView1;

@FXML
private ImageView imageView2;

@FXML
private Label sum;

@FXML
void rollDiceOperation(ActionEvent event) {
   int rollDice1, rollDice2;
   //generating random number
   rollDice1 = (int)(Math.random()*(7-1)+1);
   rollDice2 = (int)(Math.random()*(7-1)+1);
  
   //change the address of file accordingly
   File file1 = new File("C://Users//Acer//Desktop//die"+rollDice1+".png");
   File file2 = new File("C://Users//Acer//Desktop//die"+rollDice2+".png");
   imageView1.setImage(new Image(file1.toURI().toString()));
   imageView2.setImage(new Image(file2.toURI().toString()));
  
   sum.setText("Points: "+(rollDice1+rollDice2));
}

}

FXML Program

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="372.0" prefWidth="522.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="rollDice.RollDiceController">
<children>
<ImageView fx:id="imageView1" fitHeight="97.0" fitWidth="94.0" layoutX="95.0" layoutY="60.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Desktop/die1.PNG" />
</image>
</ImageView>
<ImageView fx:id="imageView2" fitHeight="89.0" fitWidth="94.0" layoutX="355.0" layoutY="63.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Desktop/die4.PNG" />
</image>
</ImageView>
<Button layoutX="220.0" layoutY="306.0" mnemonicParsing="false" onAction="#rollDiceOperation" prefHeight="33.0" prefWidth="82.0" text="Roll">
<font>
<Font name="Arial Rounded MT Bold" size="24.0" />
</font>
</Button>
<Label fx:id="sum" alignment="CENTER" contentDisplay="CENTER" layoutX="191.0" layoutY="216.0" prefHeight="51.0" prefWidth="134.0" text="Press roll">
<font>
<Font name="Arial Rounded MT Bold" size="18.0" />
</font>
</Label>
</children>
</AnchorPane>

Output

Roll Dice Points: 8 Roll

Resources

Add a comment
Know the answer?
Add Answer to:
Using JavaFX create a Roll application that implements the GUI shown When the user clicks the Rol...
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
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