The basic applet the framework methods

Bruce Eckel’s Thinking in Java Contents | Prev | Next

are often grouped according to their functionality. Some libraries, for
example, are used as is, off the shelf. The standard Java library
classes are examples of these. Other libraries are designed specifically as
building blocks to build other classes. A certain class of library is the application
whose goal is to help you build applications by providing a class or set of
classes that produces the basic behavior that you need in every application of
a particular type. Then, to customize the behavior to your own needs you
inherit from the application class and override the methods of interest. The
application framework’s default control mechanism will call your
overridden methods at the appropriate time. An application framework is a good
example of “separating the things that change from the things that stay
the same,” since it attempts to localize all the unique parts of a
program in the overridden methods.



init( )

when the applet is first created to perform first-time initialization of the

start( )

every time the applet moves into sight on the Web browser to allow the applet
to start up its normal operations (especially those that are shut off by
stop( )).
Also called after
init( ).

paint( )

of the base class
(three levels of inheritance up).

as part of an
update( )
to perform special painting on the canvas of an applet.

stop( )

every time the applet moves out of sight on the Web browser to allow the applet
to shut off expensive operations. Also called right before
destroy( ).

destroy( )

when the applet is being unloaded from the page to perform final release of
resources when the applet is no longer used

this information you can create a simple applet:

// Very simple applet
package c13;
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet {
  public void paint(Graphics g) {
    g.drawString("First applet", 10, 10);
} ///:~ 

that applets are not required to have a
main( ).
That’s all wired in to the application framework; you put any startup
code in
init( ).

run this program you must place it inside a Web page and view that page inside
your Java-enabled Web browser. To place an applet
inside a Web page you put a special tag inside the HTML source for that Web page
to tell the page how to load and run the applet. This is the
and it looks like this for Applet1:


name=identifier value = "information">

there can be as many as you want.


can perform a simple test without any network connection by starting up your
Web browser and opening the HTML file containing the applet tag. (Sun’s
JDK also contains a tool called the
that picks the <APPLET> tags out of the HTML file and runs the applets
without displaying the surrounding HTML text.
As the HTML file is loaded, the browser will discover the applet tag and go
hunt for the
file specified by the
value. Of course, it looks at the CLASSPATH to find out where to hunt, and if
file isn’t in the CLASSPATH then it will give an error message on the
status line of the browser to the effect that it couldn’t find that

more graphical example

example above isn’t too thrilling, so let’s try adding a slightly
more interesting graphic component:

// Easy graphics
import java.awt.*;
import java.applet.*;
public class Applet2 extends Applet {
  public void paint(Graphics g) {
    g.drawString("Second applet", 10, 15);
    g.draw3DRect(0, 0, 100, 20, true);
} ///:~ 

puts a box around the string. Of course, all the numbers are hard-coded and are
based on pixels, so on some machines the box will fit nicely around the string
and on others it will probably be off, because fonts will be different on
different machines.

are other interesting things you can find in the documentation for the
Any sort of graphics activity is usually entertaining, so further experiments
of this sort are left to the reader.


framework methods

// Shows init(), start() and stop() activities
import java.awt.*;
import java.applet.*;
public class Applet3 extends Applet {
  String s;
  int inits = 0;
  int starts = 0;
  int stops = 0;
  public void init() { inits++; }
  public void start() { starts++; }
  public void stop() { stops++; }
  public void paint(Graphics g) {
    s = "inits: " + inits +
      ", starts: " + starts +
      ", stops: " + stops;
    g.drawString(s, 10, 10);
} ///:~ 

when you override a method you’ll want to look to see whether you need to
call the base-class version of that method, in case it does something
important. For example, with
init( )
you might need to call
super.init( ).
However, the
documentation specifically states that the
init( ),
start( ),
stop( )
methods in
do nothing, so it’s not necessary to call them here.

you experiment with this applet you’ll discover that if you minimize the
Web browser or cover it up with another window you might not get calls to
stop( )
start( ).
behavior seems to vary among implementations; you might wish to contrast the
behavior of Web browsers with that of applet viewers.) The only time the calls
will occur is when you move to a different Web page and then come back to the
one containing the applet.

It is assumed that the reader is familiar with the basics of HTML. It’s
not too hard to figure out, and there are lots of books and resources.

Because the appletviewer ignores everything but APPLET tags, you can put those
tags in the Java source file as comments:

<applet code=MyApplet.class width=200 height=100></applet>

way, you can run ”

and you don’t need to create tiny HTML files to run tests.

More by Author

Must Read