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 Text
Java code
  1. // import necessary packages
  2. import java.applet.*;
  3. import java.awt.*;
  4. // Inherit the applet class from the class Applet
  5. public class BasicApplet extends Applet
  6. {
  7.       // init - called the first time you enter the applets HTML page
  8.       public void init() {}
  9.       // start - called every time the applet gets focus
  10.       public void start() {}
  11.       // stop - called if you change focus from the applet
  12.       public void stop() {}
  13.       // destroy - called if you leave the page (e.g. closing browser)
  14.       public void destroy() {}
  15.       // paint - called if you move your browser window or if you
  16.       // call repaint()
  17.       public void paint (Graphics g) {
  18.           g.drawString("Hello World - Wow!?", 0, 0);
  19.       }

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 Text
HTML code
  1. <applet code ="BasicApplet.class" width=200 height=50></applet>

Demo:
Filed under: Java  Tags: Applet

0 Responses to “The basic structure of a java applet”

Sorry, comments have been closed for this post.