0% found this document useful (0 votes)
9 views10 pages

Applet

An applet is a client-side Java program embedded in HTML that runs in a web browser, capable of interactive features like sound and graphics. Applets differ from standard Java programs as they lack a main() method, cannot run independently, and are restricted in file access and network communication. The applet life cycle consists of five key methods: init(), start(), paint(), stop(), and destroy(), and applets can be created by extending either the Applet or JApplet class.

Uploaded by

prernasingh9804
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)
9 views10 pages

Applet

An applet is a client-side Java program embedded in HTML that runs in a web browser, capable of interactive features like sound and graphics. Applets differ from standard Java programs as they lack a main() method, cannot run independently, and are restricted in file access and network communication. The applet life cycle consists of five key methods: init(), start(), paint(), stop(), and destroy(), and applets can be created by extending either the Applet or JApplet class.

Uploaded by

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

Applet:

-->Applet is a client side java program that is embedded in a HTML


document (web page) and executed by a web browser.

-->An applet can play sounds, display graphics, take user input, play
interactive games and so on.

-->An applet which is stored on our computer system and when the
browser tries to access the applet, it is not necessary for our computer
to be connected to the Internet is called local applet.

--> A remote applet is the one which is not stored in our computer
system and we are required to connect to the Internet to access it.

Difference between an applet and a normal java program:

-->Applets do not have a main() method.

-->Applets cannot run independently. They are run from within a


browser or applet viewer tool is used for testing purpose.

-->Applets cannot read from or write to files on the local computer.

-->Applets are restricted from using libraries of other languages.

-->Applets cannot communicate with other computers on the network.


Applet Life Cycle:

-->The life cycle of an applet is composed of five methods:

init(), start(), paint(), stop(), destroy()

a. init()-->It is the first method to be called. This is where all variables


are initialized. This method is called only once during the runtime of
applet.

b. start()--> It is called after init(). It is also called to restart an applet


after it has been stopped. start() is called each time an applets HTML
document is displayed on screen. So, if a user leaves a web page and
comes back, the applet resumes execution at start().

c. paint()--> it is called after start(). the paint() method is called each


time the applet's output must be redrawn. This method takes one
parameter of type Graphics.

d. stop()-->It is called when a user navigates away from the page


containing the applet. It is used to suspend the threads that don't need
to run when the applet is not visible. We can resume them when start()
is called if the user returns to the page.

e. destroy()-->it is called when the HTML document containing the


applet is closed by the user. The stop() method is always called before
destroy().
Fig: Life Cycle of Applet

Applet Skeleton:

-->General structure of applet.

import java.applet.*; //import javax.swing.*; if we extend JApplet

import java.awt.*;

public class MyApplet extends Applet {

public void init(){}

public void start(){}

public void paint(Graphics g){}

public void stop(){}

public void destroy(){}

}
Creating applet:

We can create applet in two ways:

1. By extending Applet class of java.applet package

2. By extending JApplet class of javax.swing package

//An applet to display Hello User!!!

import java.applet.*;

import java.awt.*;

public class HelloApplet extends Applet{

public void paint(Graphics g){

g.drawString("Hello User",50,80); //string,xco,yco

Embedding applet into HTML

<html>

<body>

<applet code= “HelloApplet.class” width= “100” height= “100”>

</applet>

</body>

</html>
Q. Create an applet that displays Tribhuvan University inside a
rectangle.

g.drawRect(10,10,300,200);

g.drawString(“Tribhuvan University”,50,50);

Drawing Different Shapes

import java.applet.*;

import java.awt.*;

public class DrawShapes extends Applet {

public void paint(Graphics g){

//to draw a line

g.setColor(Color.blue);

//g.drawLine(10, 10, 200, 10); //x1,y1, x2,y2

//to draw a rectangle

//g.drawRect(10,30, 200,100);

//g.fillRect(10,30,200,100);

//to draw oval and circle drawOval() fillOval()

//g.drawOval(10,30,200,100); //oval

//g.drawOval(10,30,200,200); //circle

//g.fillOval(10,30,200,200);
//to draw arc

//g.drawArc(10,10,200,100,0,-180);

//g.fillArc(10,10,200,100,0,-180);

//to draw polygon

Polygon p=new Polygon();

p.addPoint(100,20);

p.addPoint(200,200);

p.addPoint(20,200);

//g.drawPolygon(p);

g.fillPolygon(p);

Creating GUI in Applet

//create an applet with text fields and button to add two numbers

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class AppletGUI extends Applet implements ActionListener{

TextField t1,t2,t3;
Button b1;

public void init(){

t1=new TextField(20);

t2=new TextField(20);

t3=new TextField(20);

b1=new Button("Add");

setLayout(new FlowLayout());

add(t1);add(t2);add(t3);add(b1);

b1.addActionListener(this);

public void actionPerformed(ActionEvent e){

int m=Integer.parseInt(t1.getText());

int n=Integer.parseInt(t2.getText());

int p=m+n;

t3.setText(p+"");

}
Passing parameter to applet

--> <param> tag is used to pass parameter to an applet. Its attributes


are "name" and "value".

--> String getParameter(String paramName) method is used to get the


value of parameter passed. It returns a string value.

<html>

<body>

<applet code="ParamApplet.class" width="100" height="100">

<param name="message" value="This is a parameter applet"/>

</applet>

</body>

</html>

Applet code
import java.applet.*;
import java.awt.*;
public class ParamApplet extends Applet {
String s="";
public void start(){
s=getParameter("message");
}
public void paint(Graphics g){
g.drawString(s,10,30);
}
}

The <APPLET> Tag


< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels]
>
[< PARAM NAME = appletParameter1 VALUE = value >]
[< PARAM NAME = appletParameter2 VALUE = value >]
...
</APPLET>
CODEBASE
specifies the base URL of the applet -- the directory or folder that contains
the applet's code.
CODE
gives the name of the file that contains the compiled Applet class.
ALT
specifies any text that should be displayed if the browser understands the APPLET tag but can't
run Java applets.
NAME
specifies a name for the applet instance, which makes it possible for applets on the same page
to find (and communicate with) each other.
WIDTH = pixels HEIGHT = pixels
give the initial width and height (in pixels) of the applet display area
ALIGN
specifies the alignment of the applet. left, right, top, middle, bottom.
VSPACE = pixels HSPACE = pixels
specify the number of pixels above and below the applet (VSPACE) and on each side of the
applet (HSPACE).
< PARAM NAME = appletParameter1 VALUE = value >
<PARAM> tags are the only way to specify applet-specific parameters. Applets read user-
specified values for parameters with the getParameter() method.

You might also like