Please help I do not understand. Hints and pictures below. /////////////////////
ID: 3816249 • Letter: P
Question
Please help I do not understand. Hints and pictures below.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Implement Exercise 12.16 in the text, page 551. This makes reference to the program listed in Section 6.10 which you should incorporate in your program.
Use the following GridBagLayout approach.
Note that the program in textbook section 6.10 plays a complete game, which consists of one or more rolls. When providing a GUI interface to the game, we want to display the results of each roll when the roll button is clicked, and wait for the user to initiate the next roll with the next click of the button. So the game code has to be adapted to single step play. Each step is triggered by the Roll button, and the results of a single roll are displayed in the four text fields.
One approach would be to change the game's command line play() function so that it uses steps that are made from three actions: roll, calculate state, and display. For example,
where gameStatus, myPoint, die1, and die2 are instance variables in an instance of the class Craps, or in your instance of SnakeEyes if you merge the game with your GUI class. This provides methods that the GUI can call to step the game.
Template for GUI game step:
////////////////////////////////////////
//////////////////////////////////////
Hint:
Note that the program in textbook section 6.10 plays a complete game, which consists of one or more rolls. When providing a GUI interface to the game, we want to display the results of each roll when the roll button is clicked, and wait for the user to initiate the next roll with the next click of the button. So the game code has to be adapted to single step play. Each step is triggered by the Roll button, and the results of a single roll are displayed in the four text fields.
One approach would be to change the game's command line play() function so that it uses steps that are made from three actions: roll, calculate state, and display. For example,
// plays one game of craps
void play()
{
int sumOfDice;
sumOfDice = rollDice();
gameState();
displayRoll(sumOfDice);
if (CONTINUE == gameStatus)
{
myPoint = sumOfDice;
displayPoint();
do
{
sumOfDice = rollDice();
gameState();
displayRoll(sumOfDice);
}
while (gameStatus == CONTINUE);
}
displayWinLose();
}
where gameStatus, myPoint, die1, and die2 are instance variables in an instance of the class Craps, or in your instance of SnakeEyes if you merge the game with your GUI class. This provides methods that the GUI can call to step the game.
Explanation / Answer
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);