Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone show me how to make this into showing a pair of dice instead of just rolling one? Or if I want to roll

Can anyone show me how to make this into showing a pair of dice instead of just rolling one? Or if I want to roll more than two dice. Right now it only shows as one dice.

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;

public class DiceSimulator extends JFrame {

public DiceSimulator() { setTitle("Dice Simulator"); setSize(300, 500);

JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null);

final JLabel jl = new JLabel(new ImageIcon("Die1.png")); final JLabel j2 = new JLabel(new ImageIcon("Die1.png")); jl.setBounds(50, 60, 164, 125); panel.add(jl); JButton rollButton = new JButton("Roll"); rollButton.setBounds(50, 300, 80, 30);

rollButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) {

Random rand = new Random(); int dice1 = 1 + rand.nextInt(6); int dice2 = 1 + rand.nextInt(6);

switch(dice1) { case 1:jl.setIcon(new ImageIcon("Die1.png")); break;

case 2:jl.setIcon(new ImageIcon("Die2.png")); break;

case 3:jl.setIcon(new ImageIcon("Die3.png")); break;

case 4:jl.setIcon(new ImageIcon("Die4.png")); break;

case 5:jl.setIcon(new ImageIcon("Die5.png")); break;

case 6:jl.setIcon(new ImageIcon("Die6.png")); break; }

}

});

panel.add(rollButton); JButton quitButton = new JButton("Quit"); quitButton.setBounds(150, 300, 80, 30);

quitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } });

panel.add(quitButton); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); }

public static void main(String[] args) { DiceSimulator dice = new DiceSimulator(); dice.setVisible(true); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

Additional Factors Affecting Group Communication?

Answered: 1 week ago