named
TicTacToeGUIGame.
2. Copy the
games.board
package from the Lesson 3 proj-
ect 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
Graded Project
197
Note:
This code uses the enhanced
Graphics2D
class
to set the stroke thickness to more than one pixel. The
Oracle documentation provides more guidance on creat-
ing 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:
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.
g2.drawLine(offset, offset, this.getWidth() – offset , this.getHeight() – offset );
g2.drawLine(this.getWidth() – offset, offset , offset, this.getHeight()- offset);
break;
}
}
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);
}
}
}
. 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. Test to make sure each
button displays a nought or cross when clicked
* You can also use 2CO option if you want to purchase through Credit Cards/Paypal but make sure you put the correct billing information otherwise you wont be able to receive any download link.
* Your paypal has to be pre-loaded in order to complete the purchase or otherwise please discuss it with us at [email protected].
* As soon as the payment is received, download link of the solution will automatically be sent to the address used in selected payment method.
* Please check your junk mails as the download link email might go there and please be patient for the download link email. Sometimes, due to server congestion, you may receive download link with a delay.
* All the contents are compressed in one zip folder.
* In case if you get stuck at any point during the payment process, please immediately contact us at [email protected] and we will fix it with you.
* We try our best to reach back to you on immediate basis. However, please wait for atleast 8 hours for a response from our side. Afterall, we are humans.
* Comments/Feedbacks are truely welcomed and there might be some incentives for you for the next lab/quiz/assignment.
* In case of any query, please donot hesitate to contact us at [email protected].
* MOST IMPORTANT Please use the tutorials as a guide and they need NOT to be used for any submission. Just take help from the material.
******************************************** Good Luck ***************************************************
Any personal information received will only be used to fill your order. We will not sell or redistribute your information to anyone.
We will try our best to resolve the issue and if still persists we can discuss for a refund in case its required.