Applet Hello World Example
In this tutorial, you will create a simple “hello world” Applet example, and include it into a HTML page.
1) Create a Applet class
package com.mkyong.applet; import java.applet.*; import java.awt.Graphics; public class AppletExample extends Applet { public void init() { /** * init work */ } public void paint( Graphics g ) { g.drawString("Hello World - Applet",40,100); } }
The paint() method is executed whenever the applet is asked to redraw itself.
2) Create a HTML page
Create a HTML page and include the Applet file you created just now. Beware of the package you declare.
<applet width=300 height=300 code="com.mkyong.applet.AppletExample.class"> </applet>
Full HTML example
<html> <head><title>Testing</title></head> <body> <applet width=300 height=300 code="com.mkyong.applet.AppletExample.class"> </applet> </body> </html>
Put the Applet class into “applet” tag, width with and height specified.
3) Run it
Double click your HTML page created just now. You should see Applet displaying a “Hello World – Applet” in the HTML page.