0% found this document useful (0 votes)
72 views12 pages

Applet Programming

An applet is a Java program that runs in a web browser. It differs from a standalone application in that it does not have a main method and must extend the Applet class. An applet's lifecycle involves initialization, start, stop, and destroy methods. Local applets are stored locally while remote applets are downloaded from the internet. Applets can be embedded in HTML using the <applet> tag and parameters can be passed using the <param> tag.

Uploaded by

Madhu Sudhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
72 views12 pages

Applet Programming

An applet is a Java program that runs in a web browser. It differs from a standalone application in that it does not have a main method and must extend the Applet class. An applet's lifecycle involves initialization, start, stop, and destroy methods. Local applets are stored locally while remote applets are downloaded from the internet. Applets can be embedded in HTML using the <applet> tag and parameters can be passed using the <param> tag.

Uploaded by

Madhu Sudhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Java Applet

An applet is a special kind of Java program that embedded inside a web


page and runs in the browser.

There are some important differences between an applet and a standalone


Java application, including the following –

Java Application Java Applet


Java Applications are the stand- Java Applets are small Java programs
alone programs which can be which are designed to exist within
executed independently HTML web document
Java Applications must have main() Java Applets do not need main() for
method for them to execute execution
Java Applets cannot run
Java Applications just needs the JRE
independently and require API’s
Java Applications do not need to Java Applets must extend
extend any class unless required java.applet.Applet class
Java Applications can execute codes Java Applets Applications cannot do
from the local system so

Java Applications has access to all


Java Applets has access only to the
the resources available in your
browser-specific services
system

Types of Applet:

We can be classified the java applets into two ways –

Local Applet
Remote Applet

 Local Applet:
An applet developed locally and stored in a local system that is known as a Local
Applet. When a web page is trying to find a local applet, it doesn’t need to use the
Internet and the local system doesn’t require the Internet Connection.
 Remote Applet:
A remote applet is that which is developed by someone else and stored on a
remote computer connected to the Internet. If our system is connected to the
Internet, we can download the remote applet onto our system via the Internet.

Local Applet vs Remote Applet:


Local Applet Remote Applet

1. Local Applet is stored on our computer. 1. Remote Applet is not stored on our
computer.

2. It is not required Internet Connection to 2. It is required Internet Connection to


access the applet. access the applet.

3. In a local applet, it don't need the 3. In a remote applet, it needs the


applet's URL. applet's URL.

Life Cycle of an Applet:


Every Java Applet needs to go through a series of phases from initialization
to destruction in order to complete its execution. For that, the very first step
is to inherit the java.applet.Applet class. This class aids with various
methods which help in holding up a basic framework for the Java Applets.

The various methods involved in the life cycle of Java Applet has been
depicted by the below diagram.
there are 4 main methods which are mandatory for any Java Applet to
override.

1. public void init(): This is the very first method to be invoked during
the life cycle of an applet. In this method, the variable that will be
used further in the applet is initialized. One thing you must note here
is that this method can be invoked only once per applet life cycle.
2. public void start(): This is the second method that is invoked just
after the init() method is called by the browser. Each time a user
revisits the web page containing the applet, start() method is invoked
and the applet is started.
3. public void stop(): This method is invoked whenever a user leaves the
web page containing applet. In other words, the stop() method is used
to suspend the threads which are not required when the applet is in
the background or is not visible on the screen. These can be easily
resumed using the start() method.
4. public void destroy(): Finally, we have the destroy() method which is
invoked in order to completely remove an applet from the memory.
This method is invoked only once per applet life cycle and all the
engaged resources must be freed up before this method is called.

One more method that is mostly used along with the above four is paint().

 public void paint(Graphics g): This method is invoked whenever an


applet needs to be redrawn or repainted in the browser, irrespective of
the cause. The paint() method takes one Graphic object as a
parameter that contains the graphics context in which the applet is
being executed. Also, this method is invoked each time output is
expected from the applet.

How to create and run applet:


applet that we create must be a subclass of Applet class which is in the
applet package so we have to import applet applet package.

import java.applet.Applet;

Inside paint( ) is a call to drawString( ), which is a member of


the Graphics class. This method outputs a string beginning at the specified
X,Y location. Graphics class is available in AWT package so we have to
import it.

import java.awt.Graphics;

drawString( ) has the following general form:

void drawString(String message, int x, int y)


Here, message is the string to be output beginning at x,y. In a Java window,
the upper-left corner is location 0,0. The call to drawString( ) in the applet
causes the message to be displayed beginning at location x,y.
Creating Hello World applet :

Let’s begin with the HelloWorld applet :

import java.applet.Applet;

import java.awt.Graphics;

// HelloWorld class extends Applet

public class HelloWorld extends Applet

public void paint(Graphics g)

g.drawString("Hello World", 20, 20);

After you enter the source code for HelloWorld.java, compile in the same way
that you have been compiling java programs(using javac command).

There are two standard ways in which you can run an applet :

1. By html file.
2. By appletViewer tool (for testing purpose).

1. Using html file: To execute an applet in a web browser we have to


write a short HTML text file that contains a tag that loads the applet.
We can use APPLET or OBJECT tag for this purpose. Using APPLET,
here is the HTML file that executes HelloWorld :

HelloWorld.html
<html>
<body>
<applet code=" HelloWorld.class" width="300" height="300">
</applet>
</body>
</html>

The width and height statements specify the dimensions of the display area
used by the applet. The APPLET tag contains several other options. After you
create this html file, you can use it to execute the applet.
2. Using appletviewer : This is the easiest way to run an applet. To
execute HelloWorld with an applet viewer, you may also execute the
HTML file shown earlier. For example, if the preceding HTML file is
saved with HelloWorld.html,then the following command line will run
HelloWorld :

appletviewer HelloWorld.html
Output:

appletviewer with java source file : If you include a comment at the head
of your Java source code file that contains the APPLET tag then your code is
documented with a prototype of the necessary HTML statements, and you
can run your compiled applet merely by starting the applet viewer with your
Java source code file. If you use this method, the HelloWorld source file
looks like this :

// A Hello World Applet

// Save file as HelloWorld.java

import java.applet.Applet;

import java.awt.Graphics;

/*

<applet code="HelloWorld" width=200 height=200>


</applet>

*/

// HelloWorld class extends Applet

public class HelloWorld extends Applet

public void paint(Graphics g)

g.drawString("Hello World", 20, 20);

With this approach, first compile HelloWorld.java file and then simply run
below command to run applet :

appletviewer HelloWorld.java

Output:
Adding Applet to HTML File: The <applet> tag in HTML was used to embed
Java applets into any HTML document.

Syntax:
<applet attribute1 attribute2....>
<param parameter1>
<param parameter2>
....
</applet>

Attributes: The <applet> tag takes a number of attributes, with one of the
most important being the code attribute. This code attribute is used to link
a Java applet to the concerned HTML document. It specifies the file name of
the Java applet.

<html>

<applet code="HelloWorld" width=200 height=60>

</applet>

</html>

Here, HelloWorld is the class file, which contains the applet.


The width and height attributes determine the width and height of the
applet in pixels when it is opened in the browser.

Attributes available to be used in conjunction with the <applet> tag are as


follows:

Attribute Value Description

align URL Deprecated − Defines the text alignment around


the applet

alt URL Alternate text to be displayed in case browser


does not support applet

code URL A URL that points to the class of the applet

codebase URL Indicates the base URL of the applet if the code
attribute is relative

height pixels Height to display the applet


hspace pixels Deprecated − Defines the left and right spacing
around the applet

title test Additional information to be displayed in tool


tip of the mouse

vspace pixels Deprecated − Amount of white space to be


inserted above and below the object.

width pixels Width to display the applet.

Passing parameters to an applet:

 To pass the parameters to the Applet we need to use the param tag.
 To retrieve a parameter's value, we need to use
the getParameter() method of Applet class.

Param Tag
The <param> tag is a sub tag of the <applet> tag. The <param> tag contains
two attributes: name and value which are used to specify the name of the
parameter and the value of the parameter respectively. For example, the
param tags for passing name and age parameters looks as shown below:

<param name=”name” value=”Ramesh” />


<param name=”age” value=”25″ />

Now, these two parameters can be accessed in the applet program using
the getParameter() method of the Applet class.

getParameter() Method
The getParameter() method of the Applet class can be used to retrieve the
parameters passed from the HTML page. The syntax
of getParameter() method is as follows:

String getParameter(String param-name)

Let’s look at a sample program which demonstrates the <param> HTML tag
and the getParameter() method:

import java.awt.*;

import java.applet.*;
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Rama" />
<param name="age" value="25" />
</applet>
*/
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 40);
}
}
Output of the above program is as follows:

Displaying Numerical values in Applets:


In Applet, we can display numerical values by first converting them into
strings and then using the drawString() method of Graphics class. We can
do this easily by calling the valueOf() method of String class.

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

/*<applet code = "temp.class" width = "600" height = "400"></applet>*/

public class temp extends Applet

public void paint (Graphics g)

int x=10;

int y=25;

int add=x+y;

String s="add:"+String.valueOf(add);

g.drawString(s, 100, 100);

Output:

Getting Input from the User in Java Applet:


Applets work in graphical environment. Therefore applets treat input as text
strings. We must first create an area of the screen in which user can type
and edit input items. We can do this by using the TextField class of the awt
package. The values of the fields can be given even editer after the creation
of input fields. Next step is to retrieve the items from the fields for display of
calculations.

For any kinds of computation on the input field, we must convert it to the
right form and the results again converted back to strings for display.

Let us consider an example:

import java.awt.*;
import java.applet.*;
/*<applet Code="UserInput.class" Width=400 Height=300>
</applet>*/

public class UserInput extends Applet


{
TextField text1, text2;
public void init()
{
text1 = new TextField(8);
text2 = new TextField(8);
add(text1);
add(text2);
text1.setText("0");
text2.setText("0");
}

public void paint(Graphics g)


{
int x=0,y=0,z=0;
String s1,s2,s;
g.drawString("Input a number in each box ",10,50);
try
{
s1 = text1.getText();
x = Integer.parseInt(s1);
s2 = text2.getText();
y = Integer.parseInt(s2);
}
catch(Exception e) { }
z = x + y;
s = String.valueOf(z);
g.drawString("The Sum is : ",10,75);
g.drawString(s,100,75);
}
public boolean action(Event event, Object obj)
{
repaint();
return true;
}
}
Output:

You might also like