A Better Hello World Applet

The second example is a simple applet that displays the a string which is passed as a parameter instead of a constant string.

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

public class DrawStringApplet extends Applet {

    private String inputstring;

    public void init() {
	inputstring = getParameter("string");
    }
	
    public void paint(Graphics g) {
	g.drawString(inputstring, 50, 25);
    }
}

When an applet is first loaded by the browser its init method is called.

Here is the Swing equivalent:

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

public class DrawSwingApplet extends JApplet {

    private String inputstring;

    public void init() {
        inputstring = getParameter("string");
        JLabel label = new JLabel(inputstring);
        label.setHorizontalAlignment(JLabel.CENTER);
        getContentPane().add(label);
    }
}