Question: 1 . Create a new Eclipse Java project named TicTacToe GUI Game. 2 . Copy the games.board package from the Lesson 3 project named BoardGameTester
Create a new Eclipse Java project named TicTacToe GUI Game.
Copy the games.board package from the Lesson project named BoardGameTester to your new project:
Rightclick or Controlclick on the games.board entry in the src folder of the Lesson project.
Choose Copy from the dropdown menu.
Rightclick or Controlclick on the src folder name in your new project.
Choose Paste from the dropdown menu.
In the Cell.java file, have the Cell class extend the JButton class. This will ensure that each cell on the board has the look and feel of a standard Java button.
Override the paintComponent method in JButton by adding this method to the Cell class as follows:
@Override
public void paintComponentGraphics g
paint the basic button first
super.paintComponentg;
int offset ;
GraphicsD gGraphicsD g;
gsetStrokenew BasicStroke;
now paint or X if required
switchcontent
case NOUGHT:
Draw O
gsetColorColorRED;
gdrawOvaloffsetoffset,
this.getWidth offset
this.getHeight offset ;
break;
case CROSS:
Draw X
gsetColorColorBLACK;
gdrawLineoffset offset,
this.getWidth offset
this.getHeight offset ;
gdrawLinethisgetWidth offset,
offset, offset,
this.getHeight offset;
break;
If your code has errors, make sure you use the necessary import statements!
This code uses the enhanced GraphicsD class, a subclass of Graphics provided with JavaD to set the stroke thickness to more than one pixel.
In the Board.java file, have the Board class extend the JPanel class. This will ensure that the board can lay out each cell and process its UI events.
Replace the Board constructor with the following code:
public Boardint rows, int columns, ActionListener ah
cells new Cellrowscolumns;
this.setLayoutnew GridLayout;
for int r ; r cells.length; r
for int c ; c cellsrlength; c
cellsrc new Cellrc;
this.addcellsrc;
cellsrcaddActionListenerah;
This will add each cell to the UI and assign an ActionListener object to each cell. Remember to use the necessary imports.
In the BoardGameTester.java file, have the BoardGameTester class extend JFrame. This will ensure that the game is hosted in a Java window. Again, check that you have the necessary import statements.
Replace the content of the main method with the following code:
SwingUtilities.invokeLater new Runnable
public void run new BoardGameTester;
;
Declare the following instance variables in the BoardGameTester class not inside main:
private Board gb;
private int turn;
Add the following method to the class:
private void takeTurnCell c
Mark curMark turn
Mark.NOUGHT: Mark.CROSS;
gbsetCellcurMarkcgetRowcgetColumn;
Define the following constructor to create the board, provide the event listener, and display the board in the window:
private BoardGameTester
gb new Board new ActionListener
public void actionPerformedActionEvent ae
Cell c Cell aegetSource;
takeTurnc;
;
this.addgb;
this.setDefaultCloseOperationEXITONCLOSE;
this.setTitleTICTACTOE";
this.setSize;
this.setVisibletrue;
Rewrite the setCell method in the Board class to use a trycatch block so the program will display a message in the console and continue if the player clicks on an occupied cell.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
