Java Reference
In-Depth Information
placed in a cell. The methods isFull (lines 34-41) and isWon (lines 44-72) are for check-
ing the status of the game.
Since Cell is an inner class in TicTacToe , the variable ( whoseTurn ) and methods
( isFull and isWon ) defined in TicTacToe can be referenced from the Cell class. The
inner class makes programs simple and concise. If Cell were not defined as an inner class of
TicTacToe , you would have to pass an object of TicTacToe to Cell in order for the vari-
ables and methods in TicTacToe to be used in Cell . You will rewrite the program without
using an inner class in Programming Exercise 18.6.
The listener for MouseEvent is registered for the cell (line 81). If an empty cell is clicked
and the game is not over, a token is set in the cell (line 113). If the game is over, whoseTurn
is set to ' ' (lines 118, 122). Otherwise, whoseTurn is alternated to a new turn (line 126).
Tip
Use an incremental approach in developing and testing a Java project of this kind. For
example, this program can be divided into five steps:
incremental development and
testing
1.
Lay out the user interface and display a fixed token X on a cell.
2.
Enable the cell to display a fixed token X upon a mouse click.
3.
Coordinate between the two players so as to display tokens X and O alternately.
4.
Check whether a player wins, or whether all the cells are occupied without a winner.
5.
Implement displaying a message on the label upon each move by a player.
18.18
When the game starts, what value is in whoseTurn ? When the game is over, what
value is in whoseTurn ?
Check
Point
18.19
What happens when the user clicks on an empty cell if the game is not over? What
happens when the user clicks on an empty cell if the game is over?
18.20
How does the program check whether a player wins? How does the program check
whether all cells are filled?
18.21
Delete super.paintComponent(g) on line 97 in TicTacToe.java in Listing 18.10
and run the program to see what happens.
18.10 Locating Resources Using the URL Class
You can use the URL class to load a resource file for an applet, as long as the resource
file is located in the applet's class directory.
Key
Point
You have used the ImageIcon class to create an icon from an image file and used the
setIcon method or the constructor to place the icon in a GUI component, such as a button or
a label. For example, the following statements create an ImageIcon and set it on a JLabel
object jlbl :
ImageIcon imageIcon = new ImageIcon( "c:\\book\\image\\us.gif" );
jlbl.setIcon(imageIcon);
This approach presents a problem. The file location is fixed, because it uses the absolute file
path on the Windows platform. As a result, the program cannot run on other platforms and
cannot run as an applet. Assume that image/us.gif is under the class directory. You can cir-
cumvent this problem by using a relative path as follows:
ImageIcon imageIcon = new ImageIcon( "image/us.gif" );
This works fine with Java applications on all platforms but not with Java applets, because
applets cannot load local files. To enable it to work with both applications and applets, you
need to locate the file's URL (Uniform Resource Locator).
why URL class?
 
 
Search WWH ::




Custom Search