Question: *Please help with this code* What is needed is to edit the NestedLayoutPractice class, which extends BorderPane. The job is to: 1. Declare an HBox
*Please help with this code*
What is needed is to edit the "NestedLayoutPractice class", which extends BorderPane. The job is to:
1. Declare an HBox named top and three Buttons that will be added to the top position of the BorderPane.
2. Set the layout containers for the center and top positions.
3. Add the top and the gameView layout containers to the BorderPane.
4. Code an appropriate private listener class.
5. Instantiate the listener and register it on the appropriate components.
__________________________________________________________
*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
// create 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
}
___________________________________________________________
*TIlePuzzle Class*
public class TilePuzzle
{
private String [][] tiles;
private int side; // grid size
private int emptyRow;
private int emptyCol;
/** constructor
* @param newSide grid size
*/
public TilePuzzle( int newSide )
{
setUpGame( newSide );
}
/** setUpGame
* @param newSide grid size
*/
public void setUpGame( int newSide )
{
if ( newSide < 1 )
side = 3;
else
side = newSide;
emptyRow = side - 1;
emptyCol = side - 1;
tiles = new String[side][side];
// initialize tiles
for ( int i = 0; i < side; i++ )
{
for ( int j = 0; j < side; j++ )
{
tiles[i][j] = String.valueOf( ( side * side )
- ( side * i + j + 1 ) );
}
}
// set empty cell label to blank
tiles[side - 1][side - 1] = "";
}
/** getSide
* @return side
*/
public int getSide( )
{
return side;
}
/** getTiles
* @return a copy of tiles
*/
public String[][] getTiles( )
{
String[][] copyOfTiles = new String[side][side];
for ( int i = 0; i < side; i++ )
{
for ( int j = 0; j < side; j++ )
{
copyOfTiles[i][j] = tiles[i][j];
}
}
return copyOfTiles;
}
/** tryToPlay
* enable play if play is legal
* @return true if the play is legal, false otherwise
*/
public boolean tryToPlay( int row, int col )
{
if ( possibleToPlay( row, col ) )
{
// play: switch empty String and tile label at row, col
tiles[emptyRow][emptyCol] = tiles[row][col];
tiles[row][col] = "";
// update emptyRow and emptyCol
emptyRow = row;
emptyCol = col;
return true;
}
else
return false;
}
/** possibleToPlay
* @return true if the play is legal, false otherwise
*/
public boolean possibleToPlay( int row, int col )
{
if ( ( col == emptyCol && Math.abs( row - emptyRow ) == 1 )
|| ( row == emptyRow && Math.abs( col - emptyCol ) == 1 ) )
return true;
else
return false;
}
/** won
* @return true if the tiles are all at the right place, false otherwise
*/
public boolean won( )
{
for ( int i = 0; i < side ; i++ )
{
for ( int j = 0; j < side; j++ )
{
if ( !( tiles[i][j].equals(
String.valueOf( i * side + j + 1 ) ) )
&& ( i != side - 1 || j != side - 1 ) )
return false;
}
}
return true;
}
}
________________________________________________________________________
*GameView*
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javax.swing.JOptionPane;
public class GameView extends GridPane
{
private Button [][] squares;
private TilePuzzle game;
/** Constructor
* @param newSide, an int, the number of rows and columns
* in the grid of buttons
*/
public GameView( int newSide )
{
game = new TilePuzzle( newSide );
setUpGameGUI( );
}
/** setUpGame method
* @param newSide, an int, the number of rows and columns
* in the grid of buttons
*/
public void setUpGame( int newSide )
{
game.setUpGame( newSide );
setUpGameGUI( );
}
/** setUpGameGUI method
* creates the GUI
*/
public void setUpGameGUI( )
{
// remove all components and constraints
getChildren( ).clear( );
getColumnConstraints( ).clear( );
getRowConstraints( ).clear( );
// set up grid
ColumnConstraints col = new ColumnConstraints( );
col.setPercentWidth( 100.0 / game.getSide( ) );
RowConstraints row = new RowConstraints( );
row.setPercentHeight( 100.0 / game.getSide( ) );
for ( int i = 0; i < game.getSide( ); i++ )
getColumnConstraints( ).add( col );
for ( int j = 0; j < game.getSide( ); j++ )
getRowConstraints( ).addAll( row );
squares = new Button [game.getSide( )][game.getSide( )];
ButtonHandler bh = new ButtonHandler( );
for ( int i = 0; i < game.getSide( ); i++ )
{
for ( int j = 0; j < game.getSide( ); j++ )
{
squares[i][j] = new Button( game.getTiles( )[i][j] );
// add the button
add( squares[i][j], j, i );
// make button fill up available width and height
squares[i][j].setMaxWidth( Double.MAX_VALUE );
squares[i][j].setMaxHeight( Double.MAX_VALUE );
// register listener on button
squares[i][j].setOnAction( bh );
}
}
}
/** update method
* @param row, an int, the row index
* @param col, an int, the column index
* updates this GameView based on the state of game
* checks if the user won
*/
private void update( int row, int col )
{
for ( int i = 0; i < game.getSide( ); i++ )
for ( int j = 0; j < game.getSide( ); j++ )
squares[i][j].setText( game.getTiles( )[i][j] );
if ( game.won( ) )
JOptionPane.showMessageDialog( null,
"Congratulations! You won! Setting up new game" );
}
/** private class ButtonHandler
* processes button clicked by user and plays
*/
private class ButtonHandler implements EventHandler
{
public void handle( ActionEvent event )
{
for ( int i = 0; i < game.getSide( ); i++ )
{
for ( int j = 0; j < game.getSide( ); j++ )
{
if ( event.getSource( ) == squares[i][j] )
{
if ( game.tryToPlay( i, j ) )
update( i, j );
return;
}
}
}
}
}
}
__________________________________________________________________________
*NestedLayoutPracticeApplication*
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class NestedLayoutPracticeApplication extends Application
{
@Override
public void start( Stage stage )
{
NestedLayoutPractice root = new NestedLayoutPractice( );
Scene scene = new Scene( root, 375, 350 );
stage.setTitle( "Practice Nested Layouts" );
stage.setScene( scene );
stage.show( );
}
public static void main( String [] args )
{
launch( args );
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
