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

1. Create a new Eclipse Java project named TicTacToe GUI Game.
2. Copy the games.board package from the Lesson 3 project named BoardGameTester to your new project:
Right-click (or Control-click) on the games.board entry in the src folder of the Lesson 3 project.
Choose Copy from the dropdown menu.
Right-click (or Control-click) on the src folder name in your new project.
Choose Paste from the dropdown menu.
3. 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.
4. Override the paintComponent() method in JButton by adding this method to the Cell class as follows:
@Override
public void paintComponent(Graphics g){
//paint the basic button first
super.paintComponent(g);
int offset =5;
Graphics2D g2=(Graphics2D) g;
g2.setStroke(new BasicStroke(5));
// now paint 0 or X if required
switch(content){
case NOUGHT:
//Draw O
g2.setColor(Color.RED);
g2.drawOval(offset,offset,
this.getWidth()- offset *2,
this.getHeight()- offset *2);
break;
case CROSS:
//Draw X
g2.setColor(Color.BLACK);
g2.drawLine(offset, offset,
this.getWidth()- offset ,
this.getHeight()- offset );
g2.drawLine(this.getWidth()- 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 Graphics2D class, a subclass of Graphics provided with Java2D, to set the stroke thickness to more than one pixel.
5. 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.
6. Replace the Board constructor with the following code:
public Board(int rows, int columns, ActionListener ah){
cells = new Cell[rows][columns];
this.setLayout(new GridLayout(3,3));
for( int r =0; r < cells.length; r++){
for (int c =0; c < cells[r].length; c++){
cells[r][c]= new Cell(r,c);
this.add(cells[r][c]);
cells[r][c].addActionListener(ah);
}
}
}
This will add each cell to the UI and assign an ActionListener object to each cell. Remember to use the necessary imports.
7. 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.
8. Replace the content of the main() method with the following code:
SwingUtilities.invokeLater( new Runnable (){
public void run(){ new BoardGameTester(); }
});
9. Declare the following instance variables in the BoardGameTester class (not inside main()!):
private Board gb;
private int turn;
10. Add the following method to the class:
private void takeTurn(Cell c){
Mark curMark =(turn++%2==0)?
Mark.NOUGHT: Mark.CROSS;
gb.setCell(curMark,c.getRow(),c.getColumn());
}
11. Define the following constructor to create the board, provide the event listener, and display the board in the window:
private BoardGameTester(){
gb = new Board(3,3, new ActionListener(){
public void actionPerformed(ActionEvent ae){
Cell c =(Cell) ae.getSource();
takeTurn(c);
}
});
this.add(gb);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("TIC-TAC-TOE");
this.setSize(300,300);
this.setVisible(true);
}
12. Rewrite the setCell method in the Board class to use a try-catch 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 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!