Java Language Applets Minimal Applet


A very simple applet draws a rectangle and prints a string something on the screen.

public class MyApplet extends JApplet{ 

    private String str = "StackOverflow";

    public void init() {
    public void destroy() {}
    public void start() {}
    public void stop() {}
    public void paint(Graphics g) {
        g.setFont(new Font("TimesRoman", Font.PLAIN, 48));
        g.drawString(str, 10, 80);

The main class of an applet extends from javax.swing.JApplet.

Java SE 1.2

Before Java 1.2 and the introduction of the swing API applets had extended from java.applet.Applet.

Applets don't require a main method. The entry point is controlled by the life cycle. To use them, they need to be embedded in a HTML document. This is also the point where their size is defined.

     <applet code="MyApplet.class" width="400" height="200"></applet>