You are on page 1of 8

Practical 12

Write a Program using Applet


• To display message in the applet
• For configuring Applets by passing parameters
A. Java Applet:
Applet is a special type of java program that is embedded in the webpage to
generate the dynamic content. It runs inside the browser and works at client side.
• Applets are small Java applications that can be accessed on an Internet server,
transported over Internet, and can be automatically installed and run as apart of a
web document. Any applet in Java is a class that extends the java.applet.Applet
class.
• An Applet class does not have any main() method.
• It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a
separate runtime environment to run an applet application.
• JVM creates an instance of the applet class and invokes init() method to initialize
an Applet.

Advantage of Applet
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
• Plug-in is required at client browser to execute applet.

B. The Applet Class:


Every applet is an extension of the java.applet.Applet class. The base Applet
class provides methods that a derived Applet class may call to obtain information
and services from the browser context.

java.applet.Applet:

The java.applet.Applet class has 4 life cycle methods and


java.awt.Component class provides 1 life cycle methods for an applet. For
creating any applet java.applet.Applet class must be inherited.
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.
4. 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.

C. Applet Life Cycle


Applet runs in the browser and its lifecycle method are called by JVM at its birth, its
death and when it is momentarily away. Every Applet can be said to be any of the
following state

1. New Born state


2. Running state
3. Idle state (may or may not)
4. Dead state
5. Display state

1. New Born State


• The life cycle of an applet is begin on that time when the applet is first loaded into
the browser and called the init() method.
• The init() method is called only one time in the life cycle on an applet.
• The init() method is basically called to read the PARAM tag in the html file.
• The init () method retrieve the passed parameter through the PARAM tag of html
file using get Parameter() method .
• All the initialization such as initialization of variables and the objects like image,
sound file are loaded in the init () method .
• After the initialization of the init() method user can interact with the Applet

Syntax:
public void init()
{
//Statements
}
2. Running State
• After initialization, this state will automatically occur by invoking the start method
of applet class which again calls the run method and which calls the paint method.
• The running state also occurs from idle state when the applet is reloaded.
• This method may be called multiples time when the Applet needs to be started or
restarted.
• For Example if the user wants to return to the Applet, in this situation the start
Method() of an Applet will be called by the web browser and the user will be back on
the applet.
• In the start method user can interact within the applet.

Syntax:
public void start()
{
//Statements
}

3. Idle State
• The idle state will make the execution of the applet to be halted temporarily.
• Applet moves to this state when the currently executed applet is minimized or
when the user switches over to another page.
• At this point the stop method is invoked.
• From the idle state the applet can move to the running state.
• The stop() method can be called multiple times in the life cycle of applet Or called
at least one time.
• For example the stop() method is called by the web browser on that time When the
user leaves one applet to go another applet
Syntax:
public void stop()
{
//Statements
}

4. Dead State
• When the applet programs terminate, the destroy function is invoked which makes
an applet to be in dead state.
• The destroy() method is called only one time in the life cycle of Applet like init()
method.
Syntax:
public void destroy()
{
//Statements
}
5. Display State
• The applet is said to be in display state when the paint method is called.
• This method can be used when we want to display output in the screen.
• This method can be called any number of times.
• paint() method is must in all applets when we want to draw something on the
applet window.
• paint() method takes Graphics object as argument

Syntax:
public void paint(Graphics g)
{
//Statements
}

D. How to run an Applet Program


An Applet program is compiled in the same way as you have been compiling your
console programs. However there are two ways to run an applet.
• Executing the Applet within Java-compatible web browser.
• Using an Applet viewer, such as the standard tool, appletviewer. An applet viewer
executes your applet in a window
For executing an Applet in an web browser, create short HTML file in the same
directory. Inside body tag of the file, include the following code. (applet tag loads
the Applet class)
< applet code = "MyApplet.class" width=400 height=400 >< /applet >

a. There are two ways to run an applet


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

HTML <applet> Tag


<applet code="Bubbles.class" width="350" height="350">
Java applet that draws animated bubbles.
</applet>

HTML <param> Tag


<applet code="Bubbles.class" width="350" height="350">
<param name="nameofparameter" value="valueofparameter">
</applet>

E. A Simple Applet :
Now we are going to see the simple program of print hello world using applet.
F. 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: public String getParameter(String parameterName)


Exercise :

Q.1 What is applet?


->Applet is a special type of java program that is embedded in the webpage to
generate the dynamic content. It runs inside the browser and works at client side.

Q.2 Explain applet 4 life cycle methods.


->
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.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.

Q.3 Explain Running state.


->
• Afterinitialization, this state will automatically occur by invoking the start method
of applet class which again calls the run method and which calls the paint method.
• The running state also occurs from idle state when the applet is reloaded.
• This method may be called multiples time when the Applet needs to be started or
restarted.

Syntax:
public void start()
{
//Statements
}

Conclusion :

In this practical I learnt about applet , the life cycle of applet , how to run
applet and programs related to applets.

You might also like