Contracts for all of the GUI components are available in the JDK 1.0.2. API Documentation. We will refer to this often, so please keep the window open (though not necessarily visible) during lab. Learning how to use the available documentation from Sun is an important skill for all Java programmers.
This lab is broken down into two parts:
TicTacToeBoard
is a GUI component that you can just add to your applet.
The contract for the TicTacToeBoard
is given below:
TicTacToeBoard
ContractTicTacToeBoard
is a GUI component which implements a basic
TicTacToeBoard. A TicTacToeBoard
is divided into nine areas labelled as
follows:1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
public TicTacToeBoard ()
Constructor method; creates default TicTacToeBoard with basic colors and fonts.
public void setBoardColors (Color color, Color bgColor)
Allows one to set the foreground and background colors of the TicTacToeBoard.
public void setXfont (Font f, Color c)
Sets the font for the X player.
public void setOfont (Font f, Color c)
Sets the font for the O player.
public void clearBoard ()
Clears the board.
public int getMouseArea ()
Returns the area of the board which was last clicked on.
Returns a zero(0) if an invalid part of the board was clicked on (i.e. boundaries).
public boolean placeX (int n)
Returns true if successfully placed X in the area specified by n. Otherwise, returns false
(invalid area or element already at area).
public boolean placeO (int n)
Returns true is successfully placed O in the area specified by n. Otherwise, returns false
(invalid area or element already at area).
TicTacToeBoard
is an example of how we can write our own GUI components.
Note that you are not responsible for understanding how the TicTacToeBoard
is constructed. The code is provided for those who are interested. You can probably understand
most of it. Feel free to ask questions.
The following instance variables are defined in our TicTacToe
applet:
Component Type | Variable Name(s) | Description |
---|---|---|
TicTacToeBoard | board | The game board. |
Checkbox | xCheckbox, oCheckbox | Pick which player the preferences are for. |
TextField | nameField | Input name of player. |
Choice | colorChoice | Select color for player's mark on board. |
Choice | fontChoice | Select font for player's mark on board. |
Choice | fontStyleChoice | Select font style for player's mark on board. |
Choice | fontSizeChoice | Select font size for player's mark on board. |
Button | prefButton | Button to press to set the player's preferences. |
Label | gameStatus | Label which indicates game status: whose turn, who wins, tied games. |
Label | xNameLabel, oNameLabel | Label showing player's name. |
TextField | xWinField, oWinField | Field showing number of games player has won. |
List | historyList | List showing order of games won/tied and by whom. |
Your task: Draw the layout decomposition for this TicTacToe applet using
two forms of representation: tree representation and individual layout representation.
Use the same standards as used in lecture. Some guidelines to follow for the
tree representation are:
Panel
and Applet
are the only Containers we will be using. Containers should be identified with their
layout (including any default layouts).
North | ||
West |
Center |
East |
South |
public boolean action (Event evt, Object arg)
This method dispatches most of the GUI events.
See handleEvent
for responding to mouse clicks.
public void setPlayerPreferences ()
This method sets the player preferences in response to the prefButton
being pressed. It also clears the nameField
and switches the player
selector (so it is less likely one will accidentally overwrite the other player's
preferences). Finally, it updates the appropriate name label (xNameLabel or oNameLabel)
in the user interface.
public String getFontChoice ()
This method returns the name of the font selected.
public int getFontStyle ()
This method returns the font style selected. Font styles are indicated by
the following constants (which are actually integer values): Font.BOLD, Font.ITALIC,
and Font.PLAIN. In order to specify a font which is both bold and italic, one
must send the value equal to Font.BOLD+Font.ITALIC.
public int getFontSize ()
This method returns the font size selected.
public Color getColorChoice ()
This method returns the color selected.
public void startNewGame ()
This method starts a new Tic-Tac-Toe game. Starting a new games involves the
following events:
isXturn
appropriately and set the gameStatus
to let players know who goes first.
isGameActive
to true (to enable mouse events)
public void resetScores ()
This method sets the number of games won by each player and the total number of games
to zero. It also sets the textfields appropriately and clears the history list. Note
that the history list will not be redrawn until the applet redraws it.
public boolean handleEvent (Event evt)
This method responds to mouse clicks and a host of other events.
The applet calls handleEvent
before calling action
for events of type ACTION_EVENT
. We use this method to
call makeNextMove
if the game is active and the mouse was
pressed on the game board.
public void makeNextMove (int area)
This method attempts to move for the currently active player in the area specified.
If the move is successful, the game is checked to see if it is over yet.
If not, the area is added to the list of areas the player has
marked. The next player becomes the active player and the game status changes to
indicate the next player's turn.
public boolean isGameOver ()
This method checks to see if the game is over or not.
If the game is not over, this method returns false.
If the game is over (one player wins or there is a tie), the total number of games
increases by one, the game status message gets updated to reflect the end of the
game and the game goes into the history list. If a particular player won, their
winning game count goes up by one and this is reflected in their TextField
(xWinField or oWinField). The game is no longer active and the "Set Preferences"
button is enabled.
Data Type | Variable Name(s) | Description |
---|---|---|
String | xName, oName | Stores names of players. |
int | xWins, oWins | Stores number of games each player has won. |
int | games | Stores total number of games played. |
IntList | xList, oList | Stores which areas each player has marked. |
boolean | isGameActive | True if game is active; false otherwise. |
boolean | isXturn | True if it is X's turn; false otherwise. |
Some hints:
Integer.toString(n)
Integer.parseInt(sInt)
action
method)evt.target.equals(button1)
arg.equals("Hit Me!")
getState()
method to see which state they are in. Checkboxes can have
their state set via the setState(boolean newState)
method.
getText
method. TextFields
can have their values set via the setText(String newText)
method. TextFields
which can not be edited by the user are created by setting their editable property to false
via the setEditable(boolean isEditable)
method.
getSelectedItem()
method.
enable()
and disable()
methods.
addItem(String itemDescription)
method.
Lists can have all their items deleted via the clear()
method.