Programming Language (JAVA)
Unit 6.11 – Applets
Presentation 1
Revision
1.Explain exception handling in Java.
2.List the types of exceptions in Java
language.
3.What are try, catch and throw blocks
used for?
Objectives
At the end of this presentation, you will be able to :
•Explain applet programming
•List the differences between applications and
applets
•Describe the different stages in the life cycle of
an applet
Java Programs
Java supports two kinds of programs.
•Java Applications are the normal standalone
programs.
•Java Applets are the programs that are to be
embedded in a Web page.
Applet Programming
•Applets are the Java programs that are
executed by the Web browser.
•Appletviewer is a Java utility to execute
applets.
•Applet is a special class file written to display
graphics in a Web browser.
Applets
•Applets are embedded in Web pages using
the HTML tag <APPLET>.
•When a Web page containing applet program
is run, it is downloaded to the memory
automatically and executed by the Web
browser.
•Applet gets displayed in the specific space
allocated in the Web page called Applet
Window.
Applets
Applets can contain embedded objects like :
•Animated graphics
•Video games
•Advanced text displays and images
•Audio
Applet Types
•Local Applets
Applets that are stored in the local machine
•Remote Applets
Applets that are stored in a remote computer.
These can be downloaded and embedded into Web
pages from the Internet.
Local Applets
Remote Applets
Applets Vs. Applications
Applets Applications
Applets are embedded in
Web pages and cannot be
run independently.
Applications can be
run independently.
Applet programs call certain
methods such as init(),
start(), stop(), and destroy()
of Applet class to start and
execute the applet code.
Programs use the
main() method for
initiating the
execution of the
code.
Applets Vs. Applications (Contd..)
Applets Applications
Applets cannot read from or
write to the files in the local
computer.
Applications can
read from or write
to local computer.
Applets enable the user to
interact graphically.
Applications do not
enable the user to
interact graphically.
Life Cycle of Applets
The four methods executed are :
1.init()
2.start()
3.stop()
4.destroy()
Life Cycle of Applets
init()
Initialisation State
•This method is executed when it is first
loaded into the memory.
•The applet is now said to exist in the
initialisation state.
init()
Syntax
public void init( )
{
<action statements>;
}
start()
Running State
•The start() method of the Applet Class is
executed whenever an applet is started or
restarted.
•The applet is now said to exist in the running
state.
start()
Syntax
public void start()
{
<action statements>;
}
stop()
Idle State
•The stop() method is executed when an applet is
closed.
•The applet is stopped automatically when the user
switches to another program or Web page.
•This can also be done by invoking explicitly the
stop() method of Applet class.
•When this is invoked the applet enters the idle
state.
stop()
Syntax
public void stop( )
{
<action statements>;
}
destroy()
Dead State
•The destroy() method is executed when a
Web page is closed.
•The applet is now said to be in the dead
state.
destroy()
Syntax
public void destroy( )
{
<action statements>;
}
paint()
•The paint() method of the Applet class is
executed automatically whenever the applet
is displayed in the Web page.
•This method is used to draw graphics in the
drawing area of the applet.
repaint()
•The repaint() method is used whenever you
want to redraw the applet’s drawing area.
•The repaint() method calls the update()
method.
•The update() method clears the applet area
and calls the paint() method.
paint()
Syntax
public void paint(Graphics g)
{
<display statements>;
}
Summary
In this presentation, you learnt the following
•Applets are Java programs mainly used in
Internet computing.
•The applet’s output is displayed within a subset
of the display area of the browser.
•Java applet inherits a set of default properties
from the Applet class.
•An applet enters the Initialisation state when it is
first loaded.
Summary
In this presentation, you learnt the following
•Applet enters the running state when the start()
method of Applet class is invoked.
•An applet becomes idle when it is stopped from
running.
•An applet is said to be dead when it is removed
from memory.
Assignment
1.Define an applet. List the uses of applets.
2.Sketch and explain the life cycle of an
applet.