Applets

Applet is a special type of program that is embedded in the webpage to generate the dynamic

content. It runs inside the browser and works at client side.

Advantage of Applet

There are many advantages of applet. They are as follows:

  • It works at client side so less response time.

  • Secured

  • It can be executed by browsers running under many plateforms, including Linux,Windows, Mac Os etc.

Drawback of Applet

  • Plugin is required at client browser to execute applet.

Lifecycle of Java Applet 

  1. Applet is initialized.

  2. Applet is started.

  3. Applet is painted.

  4. Applet is stopped.

  5. Applet is destroyed.

Hierarchy of Applet

Lifecycle methods for Applet:

The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life

cycle methods for an applet.

java.applet.Applet class

For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods

of applet.

  1. public void init(): is used to initialized the Applet. It is invoked only once.

  2. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.

  3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or

browser is minimized.

  1.  public void destroy(): is used to destroy the Applet. It is invoked only once.

java.awt.Component class

The Component class provides 1 life cycle method of applet.

1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class

object that can be used for drawing oval, rectangle, arc etc.

Simple example of Applet by html file:

To execute the applet by html file, create an applet and compile it. After that create an html file

and place the applet code in html file. Now click the html file.

1. //First.java

import java.applet.Applet;

import java.awt.Graphics;

public class First extends Applet{

public void paint(Graphics g){

g.drawString("welcome",150,150);

}

}


Simple example of Applet by appletviewer tool:

To execute the applet by appletviewer tool, create an applet that contains applet tag in comment

and compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is

for testing purpose only.

1. //First.java

import java.applet.Applet;

import java.awt.Graphics;

public class First extends Applet{

public void paint(Graphics g){

g.drawString("welcome to applet",150,150);

}

}

/*

<applet code="First.class" width="300" height="300">

</applet>

*/



To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java

c:\>appletviewer First.java


Difference between Applet and Application programming

Basis

Java Applet

Java Application

  1. User graphics

Inherently graphical

Optional 

  1. Memory requirements 


Java application requirements plus web browser requirements 

Minimal Java application requirements 

  1. Distribution 

Linked via Html and transported via Https

Loaded from the file system or by a custom class loading process command-line parameters 

  1. Environmental input 

Browser client location and size; parameters embedded in the host HT IL  document.

Command Line Parameter 

  1. Method expected by the virtual Machine 


unit- initialization method start-startup method stop pause/ deactive method destroy-termination method paint-drawing method

Main - startup method 


  1. Typical applications 

public-access order-entry systems for the web, online multimedia persentations, web page animation 

Network server, multimedia kiosks, developer tools, appliance and consumer electronics control and navigation


Parameter in Applet

We can get any information from the HTML file as a parameter. For this purpose, Applet class

provides a method named getParameter(). 

Syntax:

1. public String getParameter(String parameterName)

Example of using parameter in Applet:

1. import java.applet.Applet;

2. import java.awt.Graphics;

3. public class UseParam extends Applet

4. {

5. public void paint(Graphics g)

6. {

7. String str=getParameter("msg");

8. g.drawString(str,50, 50);

9. } }

myapplet.html

1. <html>

2. <body>

3. <applet code="UseParam.class" width="300" height="300">

4. <param name="msg" value="Welcome to applet">

5. </applet>

6. </body>

7. </html>