Java Programming from the Grounds Up
Creating Java Applets
Java applets are similar to standalone Java applications, but are embedded within Web pages. We will examine two simple applets in order to see how HTML, Java, and its classes can be combined to deliver executable content. Figure 3 shows the Java code for a Variable Hello applet. A variation of the standard programmer's "Hello, World" program, the Variable Hello applet shows the basic structure of an applet and its interaction with HTML and the browser environment.
FIGURE 3
The Variable Hello Java Applet.
1: /**
2: A simple Java applet to issue an
3: HTML-specified greeting
4: @author Mark C. Reynolds
5: @version 1.0
6: */
7:
8: import java.awt.*;
9: import java.net.*;
10:
11: public class VH extends java.applet.Applet
12: implements Runnable {
13:
14: String msg1;
15: String msg2;
16:
17: public void init() {
18: String who;
19: String where;
20: URL uwhere;
21:
22: who = getParameter("who");
23: if ( who == null ) who = "world";
24: uwhere = getDocumentBase();
25: where = uwhere.toString();
26: msg1 = "Hello, " + who + ",";
27: msg2 = "from " + where;
28: }
29:
30: public void paint(Graphics g) {
31: g.setColor(Color.blue);
32: g.drawString(msg1, 10, 15);
33: g.drawString(msg2, 10, 30);
34: }
35:
36: public void run() {
37: resize(preferredSize());
38: repaint();
39: }
40: }
The code begins with two import statements. These alert the Java compiler that this applet will use classes from the java.awt and java.net packages. The asterisk is a wildcard character indicating that everything within those packages should be imported. For simplicity's sake, we've used the wildcard instead of being more selective and designating only those classes that will be required.
In line 11, we declare a public class VH as a subclass of the Applet class with the extends clause. For this applet to function, we need to add executability functionality to the VH class. Since this is not something inherited by the Applet class--and remember, Java only has single inheritance--we will use the implements clause to fill in missing elements from a Java interface named Runnable. Interfaces such as Runnable are a useful workarounds for situations where single inheritance is a drawback.
Applet programmers must provide an init() method and a run() method within their Applet subclass. A class declaration that extends Applet and implements Runnable is mandatory in all applets; it is the equivalent of the main() entry point for applications.
The VH class has two class variables, msg1 and msg2, and three methods. The init() method is called once when an instance of VH is created. The run() method is called whenever the applet is activated. The paint() method is what displays the graphics. A Web browser will call init() when it first loads the applet's page, and it will call run() whenever the page is visited or revisited. The paint() method is called whenever the applet needs to draw something. There is a default paint() method, unlike init() and run(), but many applets will want to provide their own version of paint in order to perform their own specific graphical operations.
The VH init() method starts out by calling getParameter() to obtain the value of a parameter named who. getParameter() is a Java method for obtaining information from the HTML which defines a Web page.
NEXT
Mark Reynolds is a network protocol designer, Java programmer, computer animator, and fanatic mountaineer. He currently consults to Adaptive Optics Associates, a United Technologies company.
Reprinted from Web Developer® magazine, Vol. 2 No.1 Spring 1996 (c) 1996 internet.com Corporation. All rights reserved.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|