You are on page 1of 16

JAVA APPLETS

An applet is a program written in the Java that can be


included in an HTML page.

When we use a Java - enabled browser to view a page that


contains an applet,

The applet's code is transferred to our system and executed by


the browser's Java Virtual Machine (JVM).

The HTML page must tells the browser:


 Where to get the .class file (i.e. Applet’s Code).
 How the applet is positioned on the web page (i.e. size, location, etc)

The browser then received the .class files from directory on the
user’s machine and runs the applet using JVM.
In Java,
Applet is simply a Java class that extends the java.appet class.
It is not part of AWT package

Applet class hierarchy:


Object
|
Component
|
Container
_______________|_____________________
| |
Window Panel
| |
Frame Applet
| |
JFrame JApplet
The steps to execute the applet are…

 Compile our Java source files into class files.

 Create an HTML file that tells the browser which class file is to
load first and how to size the applet.

The HTML file contains <applet> tag as follows…

<applet code = “filename.class” width = “300” height = “300”>


</applet>

Before that,
We view the applet in a browser it is good to test it in appletviewer
part of JDK.

appletviewer filename.html // by command line or run option of editor


We can avoid creating an additional HTML file
By adding
Applet tag in .Java file as follows

/* <applet code = “filename.class” width = “300” height = “300”>


</applet> */
public class MyApplet extends JApplet
{
---
---
}
Life Cycle of an Applet: -
Applet class provides us 4 methods to handle life cycle of an applet.
init ()
start ()
stop ()
& destroy ()

1. init: -
This method is used for initialization of our applet.
This method also used for adding user interface components.
Syntax:
public void init()
{
// code for
// Initialization
}
2. start: -
This method get called automatically after the browser calls
the init () method.

It is also get called whenever the user returns to the page


containing the applet after having gone off to other pages.
i.e.
start () method can be get called repeatedly,
Syntax:
public void start()
{
// Code to Start
// Resume Applet Execution
}
The tasks we want to do in start method are…

 Restarting a thread for our applet.

 Resume an animation.

 And so on which we want to restart whenever applet get focus.


3. stop: -
This method get called automatically when the user moves off
the page on which the applet resides.

This can be get called repeatedly in the same applet.

It is used to stop a time - consuming activity from slowing the system


when user not paying attention to the applet.
Syntax:
public void stop()
{
// Suspend Execution
// Stop Time Consuming Activities
}
The tasks we can perform in this method are…
 Stop an animation.
 Stop an audio playback
 Stop other time consuming activities.
4. destroy: -

This method is called when the browser shuts down normally.

This method is used to close down the resources after user


leaves the page that contains an applet.

The code which we need to execute when user exits the browser, we
add it in this method.

All these methods are the members of Applet class.

So we need to override all these methods in our class to do the


requirement task.
Security with Applet: -
Applets are designed to be loaded from a remote site and
then
execute locally. So security becomes vital.

The Java enabled browser will download all the applet code on
the web page and execute it immediately.

So applets are restricted in what they can do.

Tasks that applet can perform are…


 Show images and play sounds.
 Get key strokes and mouse clicks form user and Responds.
 Send user input back to host from user where they are loaded.
Sandbox: -
The restricted execution environment for applet is often called
the sandbox.

The applet playing in sandbox can not alter the users system.

So when running in sandbox…


 Applets can never run any local executable program.
 Applet can not communicate with any host other than the server from
which they were loaded.
 Applets can not read from or write to the local computer’s file system.
 Applets can not find out any information about the local computer,
except for the Java Version used, the Name & Version of Operating
System, Characters used to Separate Files i.e. / or \, paths i.e. : or ;, and
lines i.e. \n.

All this is possible only because


 Applets are executed by JVM and not directly by the CPU on the user’s
computer.
 JVM checks all the critical instructions and program areas.
Applet Tag and its Attributes: -
Width, height: - The width and height are measured in pixels.
It is the initial size of the applet, we can resize any windows
that applet viewer creates.
But in browser,
We can not resize the applet.

Code: - This code gives the name of the applet’s class file.
This name is related to the codebase or relative to current page
if codebase is not specified.

This attributes specifies only the name of the class that


contains applet class.
e.g.
If the applet class is in the package Pack.MyPackage, then
the attribute

code = “Pack.MyPackage.MyApplet.class”
OR
code = “Pack/MyPackage/MyApplet.class”
Align: -
It specifies the alignment of an applet in the browser or window.

It has following values.


Left: - It places an applet at left margin and text on the right of an
applet.

Right: - It places the applet at right margin and text on the left of
an applet.

Top: - It places the top of an applet at the top of the current line.

Bottom: - It places the bottom of an applet at the bottom of text in the


current line.

Middle: - It places the middle of an applet with base line of the text in
current line.
Codebase: -
This attribute is used to specifying the location of class file.
Most commonly it is used to points to a subdirectory.
e.g.
If MyApp.class is located in subdirectory SubPack of current directory
then the attribute codebase has value…

<applet code = “MyApp.class” codebase = “SubPack” width=”200” height =


“200”>
</applet>

Archive: -
This attribute lists the Java archive files containing classes and
other resources for the applet.

These files are fetched from the web server before the applet is
loaded.
Application Conversion to Applets: -
To convert Graphical Application into Applets…

 Make an HTML page with the appropriate tag to load the applet tag.

 Supply a subclass of the JApplet class. Make this class public, otherwise
applet can not be loaded.

 Eliminate the main () method in the application.

 Do not construct the frame window for the application. Our application
will be displayed in the browser.

 Move the initialization code from the frame/window constructor to the


init() method of applet.

 We don’t need to create object of Applet class, the browser will


instantiate it and calls the init () method.
 Remove the call to setSize () for applet, sizing can be done with
width and height attributes of applet tag in HTML file.

 Remove the code to close the window, because applet can’t be closed, it
terminates when the browser exits.

 Remove the call to setTitle () method, because applet can’t have title bar.

 Do not call setVisible (), applet displayed automatically.

You might also like