O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 15
Java Applets
15.1 HTML and the Java Applet Architecture .............................................. 343
15.2 Principles of Java Applets .............................................................. 344
15.3 Creating Popup Windows .............................................................. 346
15.4 The Tic-Tac-Toe Game ................................................................. 347
15.5 Summary ................................................................................ 358
15.6 Syntax .................................................................................. 358
15.7 Important Points ....................................................................... 359
15.8 Exercises ................................................................................ 360
15.9 Lab ...................................................................................... 360
15.10 Project .................................................................................. 360
The last game of this book is the popular Tic-Tac-Toe game. In order for the player to
be able to play the game from more places, we will show how to place the game inside an
applet. This means that the user can play the game from their favorite web browser without
worrying about where to find the game and how to install it.
15.1 HTML and the Java Applet Architecture
Consider Figure 15.1. The file TicTacToe.java contains the code of our program. When
we compile it, the TicTacToe.class file is created. This file contains Java binary code that
can be executed by the Java Virtual Machine (JVM).IfwewanttorunaJavaApplet,we
first need to download a .html file from the server. Here is part of an example .html file
that was automatically generated by NetBeans.
<HTML>
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<H3><HR WIDTH ="100% ">Applet HTML Page<HR WIDTH ="100% "></H3 >
<P>
<APPLET c o d e b a s e="classes " code="TicTacToe .class" width=350 height
=200></APPLET>
</P>
</BODY>
</HTML>
HTML stands for hypertext markup language.Itisoneofthesimplestkindofwebpages
that a web browser can display. An HTML file contains the text to be displayed together
with markup information (i.e., directions of how to display the text). An HTML file must
start with the <HTML> tag and then end with an </HTML> tag. The symbol / means
end of the section. An HTML file usually has two sections: head and body. The title is
343
344 Learning Java through Games
file
TicTacToe.java TicTacToe.class
Java
code
Java
binary
code
compile
image
file
y.jpg x.jpg
server
client
TicTacToe.html
HTML
text
file
image
FIGURE 15.1: Java Applet architecture.
specified inside the head section. This is the title that will be displayed as the title of the
web browser window when the page is opened. Inside the body of an HTML file, one cannot
specify the point size of the text. Instead, one can specify headers of different size (e.g., H1,
H2, and so on, where H1 is the biggest size). The <P> and </P> canbeusedtocreatea
new paragraph that is surrounded by horizontal lines.
Examine the code: <APPLET codebase="classes" code="TicTacToe.class" width =
350 height=200></APPLET>. This means display an applet at this place on the HTML web
page. The codebase parameter specifies the directory (relative to the directory where the
.html file is located) where the .class file can be found. As suggested in Figure 15.1, both
the .html and .class files need to be sent to the client.
An applet is similar to a Java application, but it has limited capabilities. It cannot
execute any program other than the applet on the client side. It cannot access any
files on the client’s computer, except for the files that come with the applet. The
applet can communicate with the server from which the applet came and with no
other computers.
15.2 Principles of Java Applets
In order to create a Java Applet from a regular Java application, follow the following
general steps.
1. Remove the main method.
2. Create a class that extends JApplet. This will be the applet’s window (i.e., substitutes
the class that inherits from JFrame).
3. Add to this class the init method. The init method will be the first method to be
executed (similar to the main method).
Java Applets 345
4. The setVisible method should not be called on the JApplet window because it is
always visible.
5. The setTitle and setSize methodsshouldnotbecalledontheJApplet window
because the title and the size of the applet are determined by the HTML file that
opens the applet.
6. Remove any code that exits the program. An applet does not have a close button.
Similarly, remove any reference to the setDefaultCloseOperation method for the
applet window.
7. An applet can create additional JFrame objects (i.e., windows), but the applet window
itself is not a JFrame object.
Following these principles, consider the following simple Java Applet code.
import java . awt . ;
import javax . swing . ;
public c la ss MyApplet extends JApplet {
MyPanel p = new MyPanel () ;
public void init () {
p. setS (" Initializing ..." );
p.repaint();
add(p) ;
}
public void start () {
p. setS (" Starting ..." );
p.repaint();
}
public void stop () {
p. setS (" Stopping ..." );
p.repaint();
}
public void destroy () {
p. setS (" Destroying ..." );
p.repaint();
}
}
class MyPanel extends JPanel {
String s ;
int i=0;
public void setS( String s ) {
this .s = s;
i ++;
}
public void paintComponent ( G raphics g ) {
super . paintComponent ( g ) ;
g . drawString ( s + ""+ i , 10 , 10) ;

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required