Java Reference
In-Depth Information
FIGURE 15.2: The Tic-Tac-Toe applet inside a web browser.
add(calcButton) ;
The applet will have a single button inside the web browser. When the button is pressed,
a new calculator window will appear. When the button is pressed again, the window will
hide. The advantage of this approach is that the program can set the size and title of the
new window.
15.4 The Tic-Tac-Toe Game
Figure 15.2 shows a snapshot of an example run of the Tic-Tac-Toe game. Note that the
program runs inside a web browser and a new window for the program is not created.
We will show a simple design that implements the applet in about 200 lines of code.
We will create two classes: TicTacToe and Square . Every object of type Square will be
responsible for one of the nine squares of the game. When asked to display the square, the
object will display a rectangle and a possible picture inside the rectangle. If the rectangle
is an X , then a picture of the letter X will be displayed. Similarly, if the square is an O ,then
a picture of the letter O will be displayed.
Here is a possible implementation of the Square class.
import java .awt . ;
Search WWH ::

Custom Search