0% found this document useful (0 votes)
33 views11 pages

Applets

A Java Applet is a client-side program embedded in web pages to create dynamic content, requiring a plugin to run in browsers. It has advantages such as reduced response time and cross-platform compatibility, but also drawbacks like the need for a browser plugin. The applet lifecycle includes methods for initialization, starting, stopping, and destruction, and it can be run via HTML files or the applet viewer tool.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

Applets

A Java Applet is a client-side program embedded in web pages to create dynamic content, requiring a plugin to run in browsers. It has advantages such as reduced response time and cross-platform compatibility, but also drawbacks like the need for a browser plugin. The applet lifecycle includes methods for initialization, starting, stopping, and destruction, and it can be run via HTML files or the applet viewer tool.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Applet

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:

o It works at client side so less response time.


o Secured
o It can be executed by browsers running under many platforms,
including Linux, Windows, Mac Os etc.

Drawback of Applet

o Plugin is required at client browser to execute applet.

Hierarchy of Applet
As displayed in the above diagram, Applet class extends Panel. Panel class
extends Container which is the subclass of Component.

Lifecycle of Java Applet

1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
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

1. For creating any applet java.applet.Applet class must be inherited. It


provides 4 life cycle methods of applet. public void init(): is used to
initialize or pass an input to an 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.
4. public void destroy(): is used to destroy or remove the Applet. It is
invoked only once.
5. public void paint (Graphics g): To draw something with an applet.
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.

Who is responsible to manage the life cycle of an applet?

Java Plug-in software.

Basic Structure of an Applet

How to run an Applet?

There are two ways to run an applet

1. By html file.
2. By appletViewer tool (for testing purpose).
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
2. import java.applet.Applet;
3. import java.awt.Graphics;
4. public class First extends Applet{
5.
6. public void paint(Graphics g){
7. g.drawString("welcome",150,150);
8. }
9.
10. }
Note: class must be public because its object is created by Java
Plugin software that resides on the browser.

myapplet.html

1. <html>
2. <body>
3. <applet code="First.class" width="300" height="300">
4. </applet>
5. </body>
6. </html>

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.

//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

Use of Init Method in an Applet

import java.applet.Applet;

import java.awt.Graphics;

public class First_init extends Applet{

public void init(){

resize(200,200);

public void paint(Graphics g){

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

}
}

/*

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

</applet>

*/
Application vs Applet

You might also like