The basic structure of a java applet
The basic structure of a java applet is illustrated below using the simple "Hello World" example.
Show Plain TextJava code
- // import necessary packages
- import java.applet.*;
- import java.awt.*;
- // Inherit the applet class from the class Applet
- {
- // init - called the first time you enter the applets HTML page
- public void init() {}
- // start - called every time the applet gets focus
- public void start() {}
- // stop - called if you change focus from the applet
- public void stop() {}
- // destroy - called if you leave the page (e.g. closing browser)
- public void destroy() {}
- // paint - called if you move your browser window or if you
- // call repaint()
- g.drawString("Hello World - Wow!?", 0, 0);
- }
To run the applet simply add the following code to your html document, this assumes the compiled class file is located in the same directory as your html document :
Show Plain TextDemo: