Wednesday, April 06, 2005

How to Write, Compile and Run an Applet

How to Write an Applet

Note: Unless you are a programmer, you will never need to know how to write, compile or run Java applets. Java applets are compiled in the same way as Java applications, but run inside a browser.

Program

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

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

    public void start() {
    }

    public void stop() {
    }

    public void destroy() {
    }

    public void paint(Graphics g) {
        showStatus("status");

        g.setColor(Color.blue);
        g.drawString("Hello World!", 100, 80);

        // Draw a Rectangle around the applet's display area.
        g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
    }
}

HTML

<applet code="JavaApplet.class" width="300" height="200"></applet>

How to Compile an Applet

javac JavaApplet.java

How to Run an Applet

Usually applets run inside a browser. But when developing applets, it may be more efficient to use the appletviewer program.

appletviewer JavaApplet.java

Note: The JavaApplet class must be public, so that browsers can get access to the class.

1 comment:

Amu said...

How to verify: "The JavaApplet class must be public, so that browsers can get access to the class."?