Introduction to Applets

Introduction to Applets

Posted in : Core Java Posted on : October 30, 2010 at 5:34 PM Comments : [ 0 ]

This section contains the detail about the Applets in java.

Introduction to Applets

An applet is a Java program which runs on the web browser. It is a fully functional java application because it has support of entire Java API. An applet class doesn't have main() method. It can directly embedded within an HTML page. When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. A JVM is required to view an applet.

Applet Life Cycle

Given below some basic methods invoked by the applet during it's life cycle :

init : This method is used to initialize the applet. After the param tags have been processed, init() method.

start : This method is called automatically after the init() method.

stop : This method is also automatically called when user moves out of the page. It can, therefore, be called repeatedly in the same applet.

destroy : This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.

paint : Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser.

A simple "Hello World" Example

The two import statement is incorporated in the below example, these two are necessary because the Java compiler would not recognize the classes Applet and Graphics, which the applet class refers to. These two import statements are :

  • java.applet.Applet.

  • java.awt.Graphics.

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

public class AppletDemo extends Applet
   public void paint (Graphics g)
      g.drawString ("Hello World", 25, 50);

Invoking an Applet

After creating and compiling the above java program, the Applet can be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java-enabled browser.

The <applet> tag is used to embed an Applet in HTML. Given below the example which invoke the "AppletDemo" applet :

<title>The Applet Demo</title>
<applet code="AppletDemo.class" width="320" height="120">
If your browser was Java-enabled, a "Hello, World"
message would appear here.

Output :

The following output is tested in a java enabled Mozilla browser :

If it would not a java enable browser , then it would show :

Applet class Services

Every applet class is the sub class java.applet.Applet. It provide methods a derived applet may need to obtain information and services from the browser context.

These methods provide the following services :

  • Get applet parameters

  • Get the network location of the HTML file that contains the applet

  • Get the network location of the applet class directory

  • Print a status message in the browser

  • Fetch an image

  • Fetch an audio clip

  • Play an audio clip

  • Resize the applet

Download Example's Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics