Penn foster Graded Project 5 TicTacToe Game GUI Java

Availability: In stock

Regular Price: $20.00

Special Price: $15.00

OR

Penn foster Graded Project 5 TicTacToe Game GUI Java

Double click on above image to view full picture

Zoom Out
Zoom In

More Views

Quick Overview

Penn foster Graded Project 5 TicTacToe Game GUI Java

In this project, you'll create the GUI front-end for the TicTacToe game. This application will leverage the classes you wrote in the graded project for Lesson 3. You'll copy code from Graded Project 3 for this project.
Figure 20 is a guide for the completed user interface.

INSTRUCTIONS
1. In NetBeans, create a new Java Application project named TicTacToeGUIGame.
2. Copy the games.board package from the Lesson 3 project named BoardGameTester.
- Right-click on the game.board package in the BoardGameTester project of the Projects pane panel.
- Choose the Copy option from the context menu or use the keyboard shortcut CTRL+C.
- Paste it in the TicTacToeGUIGame project using the Paste option in the context menu or the keyboard shortcut CTRL+V.
Make sure to copy and paste the package in the Source Packages folder.
3. In the Cell.java file, have the Cell class extend the JButton class. This action will ensure that each cell on the board has the look and feel of a standard Java button.
4. Override the paintComponent method in the Cell class as follows:
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));
        switch (content) {
            case NOUGHT:
                g2.setColor(Color.RED);
                //Draw O
                g2.drawOval(offset, offset, this.getWidth()
                        - offset * 2, this.getHeight() - offset * 2);
                break;
            case CROSS:
                g2.setColor(Color.BLACK);
                //Draw X
                g2.drawLine(offset, offset, this.getWidth() - offset,
                        this.getHeight() - offset);
                g2.drawLine(this.getWidth() - offset, offset, offset,
                        this.getHeight() - offset);
                break;
        }
    }
This code uses the enhanced Graphics2D class to set the stroke thickness to more than one pixel. The Oracle documentation provides more guidance on creating complex geometric shapes using the Graphics2D class at http://docs.oracle.com/javase/tutorial/2d/geometry/index.html. Remember to import the java.awt and javax.swing packages!
5. In the Board.java file, have the Board class extend the JPanel class. This action will ensure that the board can lay out each cell and process its UI events.
6. Make the following modifications to the Board constructor:
public Board(int rows, int columns, ActionListener ah) {
cells = new Cell[rows][columns];
this.setLayout(new GridLayout());
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);
}
}
}
These changes will add each cell to the UI and assign an ActionListener object to each cell.
Note: Remember to import the java.awt, java.awt.event, and javax.swing packages.
7. In the TicTacToeGUIGame.java file, have the TicTacToeGUIGame class extend JFrame. This action will ensure that the game is hosted in a Java window.
8. Import the games.board, java.awt, and javax.swing packages.
9. Add the following code to the main method:
SwingUtilities.invokeLater( new Runnable () {
public void run() { new TicTacToeGUIGame(); }
});
10. Declare the following instance variables in TicTacToeGUIGame:
private Board gb;
private int turn;
11. Add the following method to handle each turn:
private void takeTurn(Cell c) {
Mark curMark = (turn++ % 2 == 0)?Mark.NOUGHT
: Mark.CROSS;
gb.setCell(curMark,c.getRow(),c.getColumn());
}
12. Define the following constructor to create the board, provide the event listener, and display the board in the window:
private TicTacToeGUIGame() {
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);
}
13. Compile and run the project. How did you do? Test to make sure that each button displays a nought or cross when clicked.

Regular Price: $20.00

Special Price: $15.00

Details

Penn foster Graded Project 5 TicTacToe Game GUI Java

In this project, you'll create the GUI front-end for the TicTacToe game. This application will leverage the classes you wrote in the graded project for Lesson 3. You'll copy code from Graded Project 3 for this project.
Figure 20 is a guide for the completed user interface.

INSTRUCTIONS
1. In NetBeans, create a new Java Application project named TicTacToeGUIGame.
2. Copy the games.board package from the Lesson 3 project named BoardGameTester.
- Right-click on the game.board package in the BoardGameTester project of the Projects pane panel.
- Choose the Copy option from the context menu or use the keyboard shortcut CTRL+C.
- Paste it in the TicTacToeGUIGame project using the Paste option in the context menu or the keyboard shortcut CTRL+V.
Make sure to copy and paste the package in the Source Packages folder.
3. In the Cell.java file, have the Cell class extend the JButton class. This action will ensure that each cell on the board has the look and feel of a standard Java button.
4. Override the paintComponent method in the Cell class as follows:
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));
        switch (content) {
            case NOUGHT:
                g2.setColor(Color.RED);
                //Draw O
                g2.drawOval(offset, offset, this.getWidth()
                        - offset * 2, this.getHeight() - offset * 2);
                break;
            case CROSS:
                g2.setColor(Color.BLACK);
                //Draw X
                g2.drawLine(offset, offset, this.getWidth() - offset,
                        this.getHeight() - offset);
                g2.drawLine(this.getWidth() - offset, offset, offset,
                        this.getHeight() - offset);
                break;
        }
    }
This code uses the enhanced Graphics2D class to set the stroke thickness to more than one pixel. The Oracle documentation provides more guidance on creating complex geometric shapes using the Graphics2D class at http://docs.oracle.com/javase/tutorial/2d/geometry/index.html. Remember to import the java.awt and javax.swing packages!
5. In the Board.java file, have the Board class extend the JPanel class. This action will ensure that the board can lay out each cell and process its UI events.
6. Make the following modifications to the Board constructor:
public Board(int rows, int columns, ActionListener ah) {
cells = new Cell[rows][columns];
this.setLayout(new GridLayout());
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);
}
}
}
These changes will add each cell to the UI and assign an ActionListener object to each cell.
Note: Remember to import the java.awt, java.awt.event, and javax.swing packages.
7. In the TicTacToeGUIGame.java file, have the TicTacToeGUIGame class extend JFrame. This action will ensure that the game is hosted in a Java window.
8. Import the games.board, java.awt, and javax.swing packages.
9. Add the following code to the main method:
SwingUtilities.invokeLater( new Runnable () {
public void run() { new TicTacToeGUIGame(); }
});
10. Declare the following instance variables in TicTacToeGUIGame:
private Board gb;
private int turn;
11. Add the following method to handle each turn:
private void takeTurn(Cell c) {
Mark curMark = (turn++ % 2 == 0)?Mark.NOUGHT
: Mark.CROSS;
gb.setCell(curMark,c.getRow(),c.getColumn());
}
12. Define the following constructor to create the board, provide the event listener, and display the board in the window:
private TicTacToeGUIGame() {
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);
}
13. Compile and run the project. How did you do? Test to make sure that each button displays a nought or cross when clicked.

Product Tags

Use spaces to separate tags. Use single quotes (') for phrases.