Template Hwk9java package hwk9 import javaawtBorderLayout im
Template
Hwk9.java
package hwk9;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings(\"serial\")
public class Hwk9 extends JFrame {
Hwk9() {
this.setTitle(\"Tic Tac Toe\");
this.setBounds(0, 0, 600, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) throws IOException {
// create a frame with border layout
JFrame frame = new Hwk9();
frame.setLayout(new BorderLayout());
// create a panel with box layer aligned with y axis
// TODO
// create a new game button that is centered on x axis
// TODO
// add the new game button to the game panel
// TODO
// create a box panel with 3 x 3 grid layout
// TODO
// create an array of 9 buttons and add them to the box panel
// TODO
// instantiate a tic-tac-toe game object
// TODO
// add an action listener to the new-game buttom so that it clears the game on click.
// TODO
// add game panel (north) and box panel (center) to the frame and set it visible
// TODO
}
}
class TicTacToe {
// this array holds flags for buttons
// 0 means not clicked
// 1 means X
// 2 means O
int[] data = new int[9];
// array of buttons representing the grid cells of the game
JButton[] buttons;
// this means next move will be X if true and O if false
boolean isX = true;
// use this fields to hold image icons loaded from files
ImageIcon icon_x, icon_o;
// save buttons array
// load icons
// clear the grid (call the clear method)
// add listener (call the addListener method)
TicTacToe(JButton[] buttons) throws IOException {
// TODO
}
// add action the same button listner to all buttons
private void addListener() {
// TODO
}
// create a button listener object directly from ActionListener interface
// by overridding its actionPerformed method
//
// the actionPerformed method will find out
// which button is clicked and
// whether the click is successful and switch the player if so
ActionListener buttonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO
}
};
// set the background color to each button to white
// remove icons by calling setIcon(null)
// clear the data array to 0
// reset isX to true
void clear() {
// TODO
}
// put X or O at the i\'th button
// set the data array to 1 for X and 2 for O
//
// returns true if i\'th button is not already clicked
private boolean play(int i, boolean isX) {
// TODO
}
}
Homework 9 November 5, 2016 1 Overview In this assignment, you will be creating a version of the Tic-Tac-Toe game from the Dean & Dean textbook (see Chapter 18.7, p. 830). This starter code is also available on D2L slides. 2 Requirements You may use the code in the book as a guide, but it will require some changes In particular, your game must add the following features/properties Marking a Cell The code in book just sets the text of a button to either X or O. Instead, we will be setting the icon of a button to be either an image of an X or an O. This w require a few change to the template (notably, checking if a cell has already been selected). Assuming there is a file called X.png in the same directory as the Java file, the following code will read an image from disk: ImageIcon icon ImageIcon(Image10. read (getClass().getResource ( \"X . png\"))); = new The above statement may throw a checked exception: IOException but you can avoid catching it by declaring it in method definition. You can set an icon image of a button with its setIcon method. You can remove an image from a button by setting the icon to null New Game Button You should add a new game button. This button will clear images and background color from all the buttons. No game is in progress when the frame is first created (before this button is pressed for the first time) If this button is pressed during a game, you should start a new game. 3 GUI You must make the frame conform to the following screenshot as closely as possible. My solution used several nested JPanel instances to get alignment toSolution
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings(\"serial\")
public class Hwk9 extends JFrame {
Hwk9() {
this.setTitle(\"Tic Tac Toe\");
this.setBounds(0, 0, 600, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) throws IOException {
// create a frame with border layout
JFrame frame = new Hwk9();
frame.setLayout(new BorderLayout());
// create a panel with box layer aligned with y axis
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));
// create a new game button that is centered on x axis
JButton newGame = new JButton(\"New Game\");
newGame.setAlignmentX(Component.CENTER_ALIGNMENT);
// add the new game button to the game panel
gamePanel.add(newGame);
// create a box panel with 3 x 3 grid layout
JPanel box = new JPanel();
box.setLayout(new GridLayout(3, 3));
// create an array of 9 buttons and add them to the box panel
JButton[] buttons = new JButton[9];
for (int i = 0; i < 9; i++) {
buttons[i] = new JButton(\"\");
box.add(buttons[i]);
}
// instantiate a tic-tac-toe game object
final TicTacToe game = new TicTacToe(buttons);
// add an action listener to the new-game buttom so that it clears the game on click.
ActionListener WhenHitTheButton = new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.clear();
}
};
newGame.addActionListener(WhenHitTheButton);
// add game panel (north) and box panel (center) to the frame and set it visible
frame.add(gamePanel, BorderLayout.NORTH);
frame.add(box, BorderLayout.CENTER);
// TODO: Uncomment the line below for testing
// Testing.test(frame, buttons, newGame, game);
// TODO: Uncomment the line below to play
// frame.setVisible(true);
}
}
class TicTacToe {
// this array holds flags for buttons
// 0 means not clicked
// 1 means X
// 2 means O
int[] data = new int[9];
// array of buttons representing the grid cells of the game
JButton[] buttons;
// this means next move will be X if true and O if false
boolean isX = true;
// use this fields to hold image icons loaded from files
ImageIcon icon_x, icon_o;
// save buttons array
// load icons
// clear the grid (call the clear method)
// add listener (call the addListener method)
TicTacToe(JButton[] buttons) throws IOException {
this.buttons = buttons;
icon_x = new ImageIcon(ImageIO.read(getClass().getResource(\"X.png\")));
icon_o = new ImageIcon(ImageIO.read(getClass().getResource(\"O.png\")));
clear();
addListener();
}
// add action the same button listner to all buttons
private void addListener() {
for (int i = 0; i < 9; i++) {
buttons[i].addActionListener(buttonListener);
}
}
// create a button listener object directly from ActionListener interface
// by overridding its actionPerformed method
//
// the actionPerformed method will find out
// which button is clicked and
// whether the click is successful and switch the player if so
ActionListener buttonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
for (int i = 0; i < buttons.length; i++) {
if (buttons[i].equals(e.getSource())) {
if (isX) {
play(i, true);
} else {
play(i, false);
}
isX = !isX;
break;
}
}
}
}
};
// set the background color to each button to white
// remove icons by calling setIcon(null)
// clear the data array to 0
// reset isX to true
void clear() {
for (int i = 0; i < 9; i++) {
buttons[i].setBackground(Color.WHITE);
buttons[i].setIcon(null);
data[i] = 0;
}
isX = true;
}
// put X or O at the i\'th button
// set the data array to 1 for X and 2 for O
//
// returns true if i\'th button is not already clicked
private boolean play(int i, boolean isX) {
boolean alreadyClicked = true;
if (data[i] > 0) {
alreadyClicked = false;
} else {
if (isX) {
buttons[i].setIcon(icon_x);
data[i] = 1;
} else {
buttons[i].setIcon(icon_o);
data[i] = 2;
}
}
return alreadyClicked;
}
}






