How do I create a Hello World Applet?

The code below demonstrate the very basic of Java applet. Applet is a small Java application that can be embedded on the web browser.

package org.kodejava.applet;

import java.applet.*;
import java.awt.*;

public class HelloWorldApplet extends Applet {
    public void init() {
    }

    public void start() {
    }

    public void stop() {
    }

    public void destroy() {
    }

    public void paint(Graphics g) {
        g.setColor(Color.GREEN);
        g.drawString("Hello World", 50, 100);
    }
}

To display the applet we need to create an HTML document. Here is a simple example of the document.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Hello World Applet</title>
</head>

<body>
<applet
        code="org.kodejava.applet.HelloWorldApplet"
        height="250"
        width="250">
</applet>
</body>
</html>

You can now load the applet in your browser or by using the appletviewer utility.

appletviewer.exe hello-world-applet.html
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.