info@tutsfinder.in



Java Applet

An Applets are used to create GUI that can be embedded in to web pages. It resides on client side and run in browser to give dynamic functionality.

An applet in embedded in HTML page using applet tag <applet>. Applets are used to provide interactive features that HTML can’t be provided.

Applet is a small java program, It can’t run alone but rather to be embedded in another application.


Applet life cycle-


The life cycle of applet starts with init() and ends with destroy() method. In its life cycle an applet is goes through many phases –

1. Init()

2. Start()

3. Paint()

4. Stop()

5. Destroy()

init()- This is the first state of applet life cycle. In this method, variable declaration and initialization operations are performed.

An applet is created by calling init() method. It is called only one time in applet life cycle, when applet is first loaded.

Start()- The start() method executes immediately after init() method. When start() method called, then applet is comes in running state.

This method is called at least once in life span of an applet.

This method is called when applet is start or restart.

Stop()- stop() method stops the execution of applet.

It is called when we move from one page to another or minimizes the applet.

It is called atleast one and can be called any no. of times.

Destroy()- It is called after stop() method. It is execute when applet window is closed.

In this state applet is completely remove from the memory.

It occurs only once in its overall life cycle.

Paint()- This method is called when applet is only in running state. It can be called any no. of times.

Applet comes in display state whenever it has to perform output operations on the screen.

Graphics class provides painting feature in applet.



First Applet Program

Applet program can be run in two ways-

By HTML

By AppletViewer Tool



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

public class JavaAppletExp extends Applet {
    public void paint(Graphics g) {
        g.drawString("TutsFinder", 50, 25);
    }
}




JavaAppletShow.html
<html>
<head>
<TITLE> Applet Program </TITLE>
</head>
<body>
<APPLET CODE="JavaAppletExp.class" WIDTH=150 HEIGHT=25>
</APPLET>
</body>
</html>


NOTE: To run program through AppletViewer follow these commands:
javac JavaAppletShow.java
appletviewer javac JavaAppletExp.java