Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Activity 12-2 Guidance ================================== Overview -------- There are 5 parts. Make sure you complete the parts in numerical order. Notice that part 4 occurs

Programming Activity 12-2 Guidance ================================== Overview -------- There are 5 parts. Make sure you complete the parts in numerical order. Notice that part 4 occurs in the code after part 5. As stated in the part 5 comments, you should complete part 4 before part 5 because part 5 has a step that says: "// declare and instantiate an ActionListener" This ActionListener must be an object of the private inner button handler class that you write in part 4. The 5 parts of 12-2 each have helpful comments. The comments will guide you about what code to write. Button instantiation -------------------- Suppose you define a button as follows: JButton three; To instantiate it with its label set to "3 x 3", you would write: three = new JButton("3 x 3"); Part 2 ------ Note that the Game class inherits from JPanel. Therefore, the panel you are asked to add to the center of the content pane is the "game" object. Part 4 button handler --------------------- You have to create a private inner class to handle events from any of the three game setup buttons. This course is cumulative. Each new chapter builds on previous chapters. Week 4 had a video called Java GUI Button Components. The code for that video was supplied in a folder in week 4. Here is a section of the code shown in that video: private class CommandHandler implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == btnDisplayTrees) { displayTrees(); return; } if (source == btnDisplayWater) { displayWater(); return; } if (source == btnPackWindow) { packWindow(); return; } if (source == btnReset) { reset(); return; } } etc. The above code shows a private inner class named CommandHandler that handles button events. You can name your button handler whatever you want, such as ButtonHandler, but you must use the same name in part 5 after the comment: // declare and instantiate an ActionListener In the above video example, there is an if test for each button event. Each of the if blocks does something and then returns. In your button handler, you also have to do a certain thing for each button as specified in the comments. However, you cannot then simply return because validate() must be called at the end of the function--no matter which button was clicked. There are 2 simple ways to achieve this: 1) Use nested if/else's followed by the validate() call, or 2) Call validate() after each button's processing and then return from each one as in the video example. Part 4 setUpGame() ------------------ The part 4 comments include: // depending on which button was pressed, // call the setUpGame method of the Game class // with arguments 3, 4, or 5 // the API of that method is: // public void setUpGame(int nSides) To call a function we need an object of its class. Does our framework code have an object for us of type Game? Looking near the beginning of the NestedLayoutPractice class that we are in, you will see the following declaration: private Game game; So, game is an object reference of type Game that we can use. Your framework starting code already includes the following line of code in part 2: game = new Game(3); // instantiating the GamePanel object This is where the game object is actually created. It is passed a value of 3 in the constructor call. This will cause the game to be created with 3 rows by 3 columns of numbered squares. Now back to your part 4 handler comments. The comments say to change the game setup as instructed based on which button was clicked. If you look at the example user interface you will see that there are 3 buttons to change the game to 3 x 3, or 4 x 4, or 5 x 5 squares. You should have already declared these buttons in part 1 and created them in part 3 as instructed. In part 4, your event handler function must handle when each of the buttons is clicked. If, for exaample, the 4 x 4 button is clicked, then you should have the following line of code for that event: game.setUpGame(4); Don't overthink this. It is a simple function call using the game object, its setUpGame() method, and a literal parameter of 4. At the end of the function, call validate(). This is not mentioned in the book, but it is mentioned in the framework comments. 

/*Practice using layouts

Anderson, Franceschi

*/

import javafx.event.*;

import javafx.scene.control.*;

import javafx.scene.layout.*;

public class NestedLayoutPractice extends BorderPane

{

private GameView gameView;

private Label bottom;

// ***** Task 1: declare an HBox named top

// also declare three Button instance variables

// that will be added to the HBox top.

// These buttons will determine the grid size of the game:

// 3-by-3, 4-by-4, or 5-by-5

// task 1 ends here

public NestedLayoutPractice( )

{

super( );

// ***** Task 2: student code starts here

// instantiate the GameView object

// add gameView to the center of this BorderPane

// task 2 ends here

bottom = new Label( "Have fun playing this Tile Puzzle game" );

setBottom( bottom );

// ***** Task 3: Student code restarts here

// instantiate the HBox component named top

// instantiate the Buttons that determine the grid size

// add the buttons to HBox top

// make them take all the available space

// add HBox top to this BorderPane as its top component

// task 3 ends here

// ***** Task 5: Student code restarts here

// Note: search for and complete Task 4 before performing this task

// declare and instantiate an EventHandler

// register the handler on the 3 buttons

// that you declared in Task 1

// task 5 ends here

}

// ***** Task 4: Student code restarts here

// crete a private inner class that implements EventHandler

// your method should identify which of the 3 buttons

//was the source of the event

// depending on which button was pressed,

//call the setUpGame method of the GameView class

//with arguments 3, 4, or 5

// the API of that method is:

//public void setUpGame( int nSides )

// task 4 ends here

}

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions