Lesson One - HelloWorld Applet
This is a HelloWorld Applet based on code found in "Java in a
Nutshell." Below it is the code and the applet tag. I have heavily
commented the code for instructional purposes.
The APPLET
Tag
The HTML Applet tag is:
<APPLET code="HelloWorld.class" width=150 height=100></APPLET>
Program Listing
The java program is:
import java.awt.*;
import java.applet.*;
public class HelloWorld extends Applet
{
/** the init() mehtod is automatically called when the applet starts
*
* Normally, you would put initialization code for ANY Java class
* in the class's constructor.
*
* However, for classes derrived from Applet, the initialization
* code is placed into the method init(). This is so that the web
* browser will can call the init() method whenever the applet is
* restarted. (It would be impossible for the web browser to call
* the constructor when restarting the applet.) */
public void init()
{
// setBackground() and setForeground() are methods derrived
// indirectly from class java.awt.Component.
setBackground (Color.black);
setForeground (Color.white);
}
/** the paint() method is automatically called when the web browser
* needs to "paint" the applet. Painting happens when the applet
* is initially mapped onto the screen, and when portions need to
* be redrawn because:
*
* a) a piece of the applet that was covered by another window
* gets uncovered
* b) the window gets resized
* c) another part of the program asks the applet to redraw itself
* (used for animation)
*
* @param g an object of type Graphics; it's clipping region is set
* to the area that needs to be repainted, and it's color is set
* to the applet's foreground color */
public void paint (Graphics g)
{
// invoke the method drawString in class Graphics
g.drawString ("Hello World", 25, 50);
}
}