Many of the issues connected with the creation and use of applets are found in Part II,
when the applet package is examined and also when Swing is described in Part III. However,
the fundamentals connected to the creation of an applet are presented here, because applets
are not structured in the same way as the programs that have been used thus far. As you
will see, applets differ from console-based applications in several key areas.
Let's begin with the simple applet shown here:
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
This applet begins with two import statements. The first imports the Abstract
Window Toolkit (AWT) classes. Applets interact with the user (either directly or
indirectly) through the AWT, not through the console-based I/O classes. The AWT
contains support for a w i n d o w - b a s e d , g r a p h i c a l u s e r i n t e r f a c e . A s y o u m i g h t
e x p e c t , t h e AW T i s q u i t e
l a rg e and sophisticated, and a complete discussion of it consumes several chapters in Part
II of this topic. Fortunately, this simple applet makes very limited use of the AWT. (Applets
can also use Swing to provide the graphical user interface, but this approach is described later
in this topic.) The second import statement imports the applet package, which contains the
class Applet. Every applet that you create must be a subclass of Applet.
The next line in the program declares the class SimpleApplet. This class must be declared
as public, because it will be accessed by code that is outside the program.
Inside SimpleApplet, paint( ) is declared. This method is defined by the AWT and must
be overridden by the applet. paint( ) is called each time that the applet must redisplay its
output. This situation can occur for several reasons. For example, the window in which the
applet is running can be overwritten by another window and then uncovered. Or, the applet
window can be minimized and then restored. paint( ) is also called when the applet begins
execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called.
The paint( ) method has one parameter of type Graphics. This parameter contains the graphics
context, which describes the graphics environment in which the applet is running. This context
is used whenever output to the applet is required.
Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
This method outputs a string beginning at the specified X,Y location. It has the following
general form:
void drawString(String message, int x, int y)
Here, message is the string to be output beginning at x,y. In a Java window, the upper-left
corner is location 0,0. The call to drawString( ) in the applet causes the message "A Simple
Applet" to be displayed beginning at location 20,20.
Notice that the applet does not have a main( ) method. Unlike Java programs, applets
do not begin execution at main( ). In fact, most applets don't even have a main( ) method.
Instead, an applet begins execution when the name of its class is passed to an applet viewer
or to a network browser.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home