The basic applet the framework methods

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

Libraries
are often grouped according to their functionality. Some libraries, for
example, are used as is, off the shelf. The standard Java library
String
and
Vector
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
framework
,
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.

Method

Operation

init( )

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

start( )

Called
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( )

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

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

stop( )

Called
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( )

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

With
this information you can create a simple applet:

//: Applet1.java
// 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);
  }
} ///:~ 

Note
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( ).

To
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
[54]
to tell the page how to load and run the applet. This is the
applet
tag,
and it looks like this for Applet1:

<applet
code=Applet1
width=200
height=200>
</applet>

<param
name=identifier value = "information">

and
there can be as many as you want.

Testing
applets

You
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
appletviewer
that picks the <APPLET> tags out of the HTML file and runs the applets
without displaying the surrounding HTML text.
[55])
As the HTML file is loaded, the browser will discover the applet tag and go
hunt for the
.class
file specified by the
code
value. Of course, it looks at the CLASSPATH to find out where to hunt, and if
your
.class
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
.class
file.

A
more graphical example

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

//: Applet2.java
// 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);
  }
} ///:~ 

This
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.

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

Demonstrating

the
framework methods

//: Applet3.java
// 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);
  }
} ///:~ 

Normally
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
Applet
documentation specifically states that the
init( ),
start( ),
and
stop( )
methods in
Applet
do nothing, so it’s not necessary to call them here.

When
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( )
and
start( ).
(This
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.


[54]
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.

[55]
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>

This
way, you can run ”
appletviewer
MyApplet.java

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

More by Author

Must Read