Question: I have been given the assignment to modify the application of Section 6.10 of the Deitel & Deitel textbook to provide a GUI that enables

I have been given the assignment to modify the application of Section 6.10 of the Deitel & Deitel textbook to provide a GUI that enables the user to click a JButton to roll the dice. The application should also display four JLabels and four JTextFields, with one JLabel for each JTextField. The JTextFields should be used to display the values of each die and the sum of the dice after each roll. The point should be displayed in the fourth JTextField when the user does not win or lose on the first roll and should continue to be displayed until the game is lost.

I have created a gui.java file which contains the JLabel fields, JTextField fiels and JButton field. I am trying to do the event handlers. I started to rewrite alot of the code in Craps.java to be within methods instead of the main method, but I wasn't sure if that was the right approach. When I tried to do that I had problems getting the gameStatus to populate in the handler. The 2 files that are being used are Craps.java and Gui.java.

Question I need to know how to do the event handlers and if I need to rewrite the code in Craps.java to be in methods. If so, please give me an example of how to do that. Thanks

Craps.java

// Craps.java Section 6.10 of the Deitel & Deitel textbook: Figure 6.8:

// Craps class simulates the dice game craps import java.security.SecureRandom; import javax.swing.JFrame; public class Craps { //create secure random number generator for use in method rollDice private static final SecureRandom randomNumbers = new SecureRandom(); //enum type with constants that represent the game status private enum Status { CONTINUE, WON, LOST }; //constants that represent common rolls of the dice private static final int SNAKE_EYES = 2; private static final int TREY = 3; private static final int SEVEN = 7; private static final int YO_LEVEN = 11; private static final int BOX_CARS = 12; //plays one game of craps public static void main(String[] args) { Gui gui = new Gui(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(300,300); gui.setVisible(true); int myPoint = 0; //point if not win or loss on first roll Status gameStatus; //can contain CONTINUE, WON or LOST int sumOfDice = rollDice(); //first roll of the dice //determine game status and point based on first roll switch (sumOfDice) { case SEVEN: // win with 7 on first roll case YO_LEVEN: //win with 11 on first roll gameStatus = Status.WON; break; case SNAKE_EYES: //lose with 2 on first roll case TREY: //lose with 3 on first roll case BOX_CARS: //lose with 12 on first roll gameStatus = Status.LOST; break; default: //did not win or lose, so remember point gameStatus = Status.CONTINUE; //game is not over myPoint = sumOfDice; //remember the point System.out.printf("Point is %d%n", myPoint); break; } //while game is not complete while (gameStatus == Status.CONTINUE) //not WON or LOST { sumOfDice = rollDice(); //roll dice again //determine game status if (sumOfDice == myPoint) //win by making point gameStatus = Status.WON; else if (sumOfDice == SEVEN) //lose by rolling 7 before point gameStatus = Status.LOST; } //display won or lost message if (gameStatus == Status.WON) System.out.println("Player wins"); else System.out.println("Player loses"); }//end of main //methods //roll dice, calculate sum and display results public static int rollDice() { //pick random die values int die1 = 1 + randomNumbers.nextInt(6); //first die roll int die2 = 1 + randomNumbers.nextInt(6); //second die roll int sum = die1 + die2; // sum of die values //display results of this roll System.out.printf("Player rolled %d + %d = %d%n", die1, die2, sum); return sum; } }//end of Craps 

File Gui.java

import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Component; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JOptionPane; public class Gui extends JFrame { private JButton rollDice; private JLabel label1; private JLabel label2; private JLabel label3; private JLabel label4; private JLabel label5; private JTextField item1; private JTextField item2; private JTextField item3; private JTextField item4; private JTextField item5; private final GridBagLayout layout; //layout of this frame private final GridBagConstraints constraints; //layout's constraints public Gui(){ super("Let's play Craps"); layout = new GridBagLayout(); //set frame layout setLayout(layout); constraints = new GridBagConstraints(); // instantiate constraints //create gui components rollDice = new JButton("roll dice"); label1 = new JLabel("Die 1..."); item1 = new JTextField(3); label2 = new JLabel("Die 2..."); item2 = new JTextField(3); label3 = new JLabel("Sum of Dice..."); item3 = new JTextField(3); label4 = new JLabel("Point..."); item4 = new JTextField(3); label5 = new JLabel("Status of Game... "); item5 = new JTextField(8); //add components addComponent(rollDice,0,0,2,2); constraints.fill = GridBagConstraints.BOTH; addComponent(label1,2,0,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(label2,3,0,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(label3,4,0,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(label4,5,0,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(label5,6,0,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(item1,2,1,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(item2,3,1,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(item3,4,1,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(item4,5,1,1,1); constraints.fill = GridBagConstraints.BOTH; addComponent(item5,6,1,1,1); HandlerClass handler = new HandlerClass(); rollDice.addActionListener(handler); } //method to set constraints on private void addComponent(Component component, int row, int column, int width, int height) { constraints.gridx = column; constraints.gridy = row; constraints.gridwidth = width; constraints.gridheight = height; layout.setConstraints(component, constraints); add(component); //add component } private class HandlerClass implements ActionListener{ public void actionPerformed(ActionEvent event){ if(event.getSource()==rollDice) { JOptionPane.showMessageDialog(null,String.format("%s", event.getActionCommand())); } } } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!