Java Code Example for Java Applet

DeeKay April 19, 2011 at 4:13 pm · java.applet

An applet is a small program that is intended not to be run on its own, but rather to be embedded inside another application.

The Applet class must be the superclass of any applet that is to be embedded in a Web page or viewed by the Java Applet Viewer. The Applet class provides a standard interface between applets and their environment.

To create an Applet below are the 4 methods which are required to load the applet :

  • init() – this method is called by the browser or applet viewer to inform this applet that it has been loaded into the system.
  • start() – this method is called by the browser or applet viewer to inform this applet that it should start its execution.
  • stop() – this method is called by the browser or applet viewer to inform this applet that it should stop its execution.
  • destroy() – this method is called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated.
  • paint() – This method is the inherited method from class java.awt.Container. Paints the container. This forwards the paint to any lightweight components that are children of this container. If this method is reimplemented, super.paint(g) should be called so that lightweight components are properly rendered. If a child component is entirely clipped by the current clipping setting in g, paint() will not be forwarded to that child.

 

Below java code example for simple hello world applet show you how to write applet and call it.


/**********************************************************************************
 * Created on Nov, 2004 Copyright(c) http://kodehelp.com All Rights Reserved. 
 **********************************************************************************/
package com.kodehelp.java.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

/**
 * @author http://kodehelp.com
 * 
 */
public class JavaApplet extends Applet {

	/*
	 * Called by the browser or applet viewer to inform this applet that it has
	 * been loaded into the system.
	 */
	public void init() {

	}

	/*
	 * Called by the browser or applet viewer to inform this applet that it
	 * should start its execution.
	 */
	@Override
	public void start() {
	}

	/*
	 * Called by the browser or applet viewer to inform this applet that it
	 * should stop its execution.
	 */
	@Override
	public void stop() {

	}

	/*
	 * Called by the browser or applet viewer to inform this applet that it is
	 * being reclaimed and that it should destroy any resources that it has
	 * allocated.
	 */
	@Override
	public void destroy() {

	}

	public void paint(Graphics g) {
		g.setColor(Color.RED);
		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.

<html>
<head>
    <title>Hello World Applet</title>
</head>
<body>
    <applet 
        code="com.kodehelp.java.applet.JavaApplet" height="250" width="250">
    </applet>
</body>
</html>