Question

Create a Roll application that implements the GUI shown in figure 1 (default images must be...

Create a Roll application that implements the GUI shown in figure 1 (default images
must be set as shown in figure 1). 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 (as shown in figure 2).

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

DiceGUI.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DiceGUI extends JFrame implements ActionListener
{
JLabel l1;
JButton b1;
int x=1,y=1;
DiceGUI(String s) // constructor
{
super(); // calls super class constructor
setSize(500,500); // setting size of frame
setLayout(null);
this.setBackground(Color.WHITE); // setting backgroung color to white
l1=new JLabel(""); // creates JLabel oject
b1=new JButton("Roll Dice"); // creates JButton object
l1.setBounds(100,50,200,30); // setting bounds to JLabel
add(l1); // adds label in frame
b1.setBounds(150,400,200,30); // setting bounds to button
add(b1); // adds button in frame

b1.addActionListener(this); // add action listener on button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // default close operation
setVisible(true); // visible frame
}
public void paint(Graphics g) // paint method definition
{
super.paint(g);
if(x==0 && x==1)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice1.jpg"); // create imageicon
i.paintIcon(this,g,30,200); //it draws image
}
else
if(x==2)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice2.jpg"); // create imageicon
i.paintIcon(this,g,30,200); //it draws image
}
else
if(x==3)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice3.jpg"); // create imageicon
i.paintIcon(this,g,30,200); //it draws image
}
else
if(x==4)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice4.jpg"); // create imageicon
i.paintIcon(this,g,30,200); //it draws image
}
else
if(x==5)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice5.jpg"); // create imageicon
i.paintIcon(this,g,30,200); //it draws image
}
else
if(x==6)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice6.jpg"); // create imageicon
i.paintIcon(this,g,30,200); //it draws image
}

if(y==0 && y==1)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice1.jpg"); // create imageicon
i.paintIcon(this,g,280,200); //it draws image
}
else
if(y==2)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice2.jpg"); // create imageicon
i.paintIcon(this,g,280,200); //it draws image
}
else
if(y==3)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice3.jpg"); // create imageicon
i.paintIcon(this,g,280,200); //it draws image
}
else
if(y==4)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice4.jpg"); // create imageicon
i.paintIcon(this,g,280,200); //it draws image
}
else
if(y==5)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice5.jpg"); // create imageicon
i.paintIcon(this,g,280,200); //it draws image
}
else
if(x==6)
{
ImageIcon i=new ImageIcon("C:\\Users\\AKSHAY\\Desktop\\dice6.jpg"); // create imageicon
i.paintIcon(this,g,280,200); //it draws image
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
Random rn = new Random(); // creates Random class object
x = rn.nextInt(6) + 1;
y = rn.nextInt(6) + 1;
if(x==0)
x=1;
if(y==0)
y=1;
int sum=x+y;
l1.setFont(new Font("Arial",Font.BOLD,18)); // set font to the label
l1.setText("Sum Of Dice= "+sum); // set text on label
repaint(); // calls paint() method
}
}
public static void main(String args[])
{
new DiceGUI("Roll Dice GUI"); // anonymous object of DiceGUI class
}
}

Output

C:\Users\AKSHAY\Desktop>javac DiceGUI.java

C:\Users\AKSHAY\Desktop>java DiceGUI

Add a comment
Know the answer?
Add Answer to:
Create a Roll application that implements the GUI shown in figure 1 (default images must be...
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
  • 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

  • Write a GUI-based Java application that implements the "Math Game" shown below. The program asks the...

    Write a GUI-based Java application that implements the "Math Game" shown below. The program asks the user to enter the answer of multiplying two one-digit random integers.  If the user enters a correct answer, a message will be displayed at the bottom of the frame, and the text field will be cleared.  If the user enters a wrong answer, a message will be displayed at the bottom of the frame asking for another answer.  If the user...

  • Please, build calculator to exact image as below using Python. Write a GUI that implements the...

    Please, build calculator to exact image as below using Python. Write a GUI that implements the calculator shown in the following image: Calculator х Plus Equals: Add Clear Quit - User enters two integers into the text fields. - When Add button is pressed, the sum of the values in the text fields are shown after the Equals: as a label. - The Clear button clears the values in the text fields. The cleared values can be blank or zero....

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

  • JAVASCRIPT Write a GUI application that has three labels, two text fields, and two buttons (with...

    JAVASCRIPT Write a GUI application that has three labels, two text fields, and two buttons (with the titles Name and Age). When the program starts the user will enter his/her first name in the first text field and his age in the second text field. When the user clicks the Name button, the first name entered by the user will be displayed as a separate label with the words "CSC210 student at the back of it. When the user clicks...

  • This is python3. Please help me out. Develop the following GUI interface to allow the user...

    This is python3. Please help me out. Develop the following GUI interface to allow the user to specify the number of rows and number of columns of cards to be shown to the user. Show default values 2 and 2 for number of rows and number of columns. The buttons "New Game" and "Turn Over" span two column-cells each. Fig 1. The GUI when the program first starts. When the user clicks the "New Game" button, the program will read...

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

  • ##JAVA## about a GUI program (see screenshot below) that permits the user to create, save, and load polygons that are displayed (both hollow and filled) in a GUI. The features and operations required...

    ##JAVA## about a GUI program (see screenshot below) that permits the user to create, save, and load polygons that are displayed (both hollow and filled) in a GUI. The features and operations required by the GUI program include: 1. The program starts with an empty (no vertices) polygon set with a randomly generated color. The drawing area is set to be initially square and should have a black background. ------------------------------------------------------------------------------------------------ 2. The left mouse button lets you begin building a...

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

  • 1. Create a Java application that will change the appearance of a string as shown in...

    1. Create a Java application that will change the appearance of a string as shown in the picture. 2. The program will include a panel, label, checkboxes, radio buttons, and list. 3. To change color, set up 4 radio buttons in a button group. The colors are Black, Red, Green, and Blue to be selected. The default color is Black. 4. To change the font size, set up a list. The sizes are 18, 26, 38, and 56 to be...

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