Java 1) The player enters a bet in a text field and then clicks a button to ente
ID: 3845167 • Letter: J
Question
Java
1) The player enters a bet in a text field and then clicks a button to enter the bet in text field and roll the dice
2) if the roll is 7 or 11 the player wins three times the bet.// The following is the dice game which allows a player to roll the dice please help implement problems number 1 and 2 into the following code //
/ * The java code is modified into
* three classes called DiceDriver
* ,RollDicePanel and Dice.
* */
//DiceDriver.java
import javax.swing.*;
public class DiceDriver
{
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setTitle("Dice Demo");
//set size of the window
window.setSize(350, 350);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//call add to add instance of RollDicePanel
window.add(new RollDicePanel());
//set visible true
window.setVisible(true);
}//end main
}
------------------------------------------------------------------------------------------------------
//RollDicePanel.java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class RollDicePanel extends JPanel
{
private Dice _left; // component for one Dice
private Dice _right;
private JButton rollButton;
private JPanel dicePanel;
public RollDicePanel()
{
_left=new Dice();
_right=new Dice();
rollButton = new JButton("Roll");
rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24));
rollButton.addActionListener(new RollListener());
dicePanel = new JPanel();
dicePanel.setLayout(new GridLayout(1, 2, 4, 0));
dicePanel.add(_left);
dicePanel.add(_right);
this.setLayout(new BorderLayout());
this.add(rollButton, BorderLayout.NORTH);
this.add(dicePanel , BorderLayout.CENTER);
}
private class RollListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
_left.roll();
_right.roll();
}
}
}
------------------------------------------------------------------------------------------------------
//Dice.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class Dice extends JPanel
{
private int _value; // value that shows on face of Dice
private int _diam = 9; // Diameter of spots
private static Random random = new Random(); // random generator
public Dice() {
setBackground(Color.white);
//-- Preferred size is set, but layout may change it.
setPreferredSize(new Dimension(60,60));
roll(); // Set to random initial value
}
public int roll() {
int val = random.nextInt(6) + 1; // Range 1-6
setValue(val);
return val;
}
public int getValue() {
return _value;
}
public void setValue(int spots) {
_value = spots;
repaint(); // Value has changed, must repaint
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); // required
int w = getWidth();
int h = getHeight(); // should use to resize spots too.
switch (_value)
{
case 1: drawSpot(g, w/2, h/2);
break;
case 3: drawSpot(g, w/2, h/2);
// Fall thru to next case
case 2: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
break;
case 5: drawSpot(g, w/2, h/2);
// Fall thru to next case
case 4: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
break;
case 6:
drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
drawSpot(g, w/4, h/2);
drawSpot(g, 3*w/4, h/2);
break;
}
}
private void drawSpot(Graphics g, int x, int y)
{
g.fillOval(x-_diam/2, y-_diam/2, _diam, _diam);
}//end drawSpot
}
Explanation / Answer
I did what I understand from question. If my understanding is wrong, please comment my answer with correctly explaining the to-do. Always happy to help.
Thankyou.
//DiceDriver.java
import javax.swing.*;
public class DiceDriver
{
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setTitle("Dice Demo");
//set size of the window
window.setSize(350, 350);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//call add to add instance of RollDicePanel
window.add(new RollDicePanel());
//set visible true
window.setVisible(true);
}//end main
}
//RollDicePanel.java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class RollDicePanel extends JPanel
{
private Dice _left; // component for one Dice
private Dice _right;
private JTextField inputBet;
private JButton rollButton;
private JPanel dicePanel,textfieldAndRollbutton;
public RollDicePanel()
{
_left=new Dice();
_right=new Dice();
rollButton = new JButton("Roll");
rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24));
rollButton.addActionListener(new RollListener());
// I have taken new JtextField and new panel that contains JtextField and rollButton(you defined already)
inputBet = new JTextField(10);
textfieldAndRollbutton = new JPanel();
textfieldAndRollbutton.add(inputBet);//added textfield to this new panel
textfieldAndRollbutton.add(rollButton);//added rollButton to this new panel
dicePanel = new JPanel();
dicePanel.setLayout(new GridLayout(1, 2, 4, 0));
dicePanel.add(_left);
dicePanel.add(_right);
this.setLayout(new BorderLayout());
this.add(textfieldAndRollbutton, BorderLayout.NORTH);//added this new panel at north. Before there was only rollButton.
this.add(dicePanel , BorderLayout.CENTER);
}
private class RollListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int sum = _left.roll() + _right.roll();//summing randomly given dice values
if(inputBet.getText().isEmpty())
{//ensuring for user have to enter some bet value in text field
JOptionPane.showMessageDialog(null, "Enter some bet value in text field", "Enter Bet Value", JOptionPane.INFORMATION_MESSAGE);
}
else
{
int inputbet = Integer.parseInt(inputBet.getText());//getting bet value from text field
if(sum==7 || sum==11)
{//you won
inputbet*=3;//myltiplying 3 times the bet value
JOptionPane.showMessageDialog(null, "You Won 3 times the bet you input.Now your bet value is "+inputbet, "You WON", JOptionPane.INFORMATION_MESSAGE);
}
else
{//you loose
JOptionPane.showMessageDialog(null, "You are loser.try again!", "LOSER", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
//Dice.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class Dice extends JPanel
{
private int _value; // value that shows on face of Dice
private int _diam = 9; // Diameter of spots
private static Random random = new Random(); // random generator
public Dice() {
setBackground(Color.white);
//-- Preferred size is set, but layout may change it.
setPreferredSize(new Dimension(60,60));
roll(); // Set to random initial value
}
public int roll() {
int val = random.nextInt(6) + 1; // Range 1-6
setValue(val);
return val;
}
public int getValue() {
return _value;
}
public void setValue(int spots) {
_value = spots;
repaint(); // Value has changed, must repaint
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); // required
int w = getWidth();
int h = getHeight(); // should use to resize spots too.
switch (_value)
{
case 1: drawSpot(g, w/2, h/2);
break;
case 3: drawSpot(g, w/2, h/2);
// Fall thru to next case
case 2: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
break;
case 5: drawSpot(g, w/2, h/2);
// Fall thru to next case
case 4: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
break;
case 6:
drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
drawSpot(g, w/4, h/2);
drawSpot(g, 3*w/4, h/2);
break;
}
}
private void drawSpot(Graphics g, int x, int y)
{
g.fillOval(x-_diam/2, y-_diam/2, _diam, _diam);
}//end drawSpot
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.