The Hello World Applet

The "Hello World" AWT-applet is a simple applet that displays the string "Hello world!".

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorldApplet extends Applet {

    public void paint(Graphics g) {
	g.drawString("Hello world!", 50, 25);
    }
}
Each time the applet needs to be redisplayed its paint method is called. It receives the applet's Graphics context as argument.

It is more "appropriate" to put the message in a label:

import java.applet.Applet;
import java.awt.Label;

public class HelloLabelApplet extends Applet {

    public void init() {
        Label label = new Label("Hello world!");
        add(label);

    }
}

In Swing this applet would become:

import javax.swing.*;
import java.awt.*;

public class HelloSwingApplet extends JApplet {

    public void init() {
        JLabel label = new JLabel("Hello world!");
        label.setHorizontalAlignment(JLabel.CENTER);
        getContentPane().add(label);
    }
}

A Swing applet has a ContentPane in which objects are drawn. In an AWT applet the distinction between the Applet and its ContentPane is not made: the Label is added directly to the Applet object.