Professional Documents
Culture Documents
Chapter 2
Chapter 2
College of Informatics
Department of Computer Science
Chapter 2 :
Java Applet
Nigussu B.
nigussu.bitew@bhu.edu.et
1
Java Applet
• Applet is a special type of program that is
embedded in the webpage to generate the
dynamic content. It runs inside the browser and
works at client side.
• Applet is a special type of program that is
embedded in the webpage to generate the
dynamic content. It runs inside the browser and
works at client side. 2
Cont’d
• Advantage of Applet
• There are many advantages of applet. They are as
follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many
plateforms, including Linux, Windows, Mac Os etc.
• Drawback of Applet
• Plugin is required at client browser to execute
applet.
3
Lifecycle of Java Applet
4
Lifecycle methods for Applet
• init(): The init() method is the first method to run that
initializes the applet. It can be invoked only once at the
time of initialization. The web browser creates the
initialized objects, i.e., the web browser (after checking
the security settings) runs the init() method within the
applet.
• start(): The start() method contains the actual code of
the applet and starts the applet. It is invoked
immediately after the init() method is invoked. Every
time the browser is loaded or refreshed, the start()
method is invoked. It is also invoked whenever the
applet is maximized, restored, or moving from one tab
to another in the browser. 5
Cont’d
• stop(): The stop() method stops the execution of
the applet. The stop () method is invoked whenever
the applet is stopped, minimized, or moving from
one tab to another in the browser, the stop()
method is invoked. When we go back to that page,
the start() method is invoked again.
• destroy(): The destroy() method destroys the applet
after its work is done. It is invoked when the applet
window is closed or when the tab containing the
webpage is closed. It removes the applet object
from memory and is executed only once. We cannot
start the applet once it is destroyed. 6
Cont’d
• paint(): The paint() method belongs to the
Graphics class in Java. It is used to draw shapes
like circle, square, trapezium, etc., in the applet. It
is executed after the start() method and when the
browser or applet windows are resized.
• There are two ways to run an applet
• By html file.
• By appletViewer tool (for testing purpose).
7
Creating an Executable Applet
• Executable applet is nothing but the .class file of the applet,
which is obtained by compiling the source code of the applet.
• Compiling an applet is exactly the same as compiling an
application.
• Steps required for compiling an applet :
• Move to the directory containing the source code and type
the following command :
• Javac HelloJava.java
• The compiled output file called HelloJava.class is placed in
the same directory as the source.
• If any error message is received, then we must check for
errors, correct them and compile the applet again.
8
Designing a Web Page
• A Web page is basically made up of text and HTML tags that can be interpreted
by a Web browser or an applet viewer.
• Web pages should be stored in the same directory as the compiled code of the
applets.
• Web page template:
<HTML>
<! Comment
……………………
…………………… Section
>
<HEAD>
Head
</HEAD> Section
<BODY> Body
</BODY> Section
</HTML> 9
Running the Applet
• We must have the following files in our current directory :
• HelloJava.java
• HelloJava.class
• HelloJava.html
• To run an applet, we require one of the following tools:
• 1. Java-enabled Web browser (Such as HotJava or Netscape)
• 2. Java appletviewer
• If we use the appletviewer tool, we will only see the applet output.
• The appletviewer is not a full-fledged Web browser.
• We can use it to run our applet as follows :
• appletviewer HelloJava.html 10
Simple example of Applet by html file
12
How Applets differ from Applications
• An application is a Java program that is run by
using a Java interpreter program.
• An applet is a Java program that is run by a Java-
enabled web browser.
• A Java Application also known as application
program is a type of program that independently
executes on the computer.
• The Java applet works on the client side, and runs
on the browser and makes use of another
application program so that we can execute it.
13
Cont’d
14
Java Application Vs. Java Applet
Parameters Java Application Java Applet
Meaning A Java Application also known as application The Java applet works on the client side, and
program is a type of program that runs on the browser and makes use of another
independently executes on the computer. application program so that we can execute it.
Requirement of main( ) method Its execution starts with the main( ) method It does not require the use of any main()
only. The use of the main( ) is mandatory. method. Java applet initializes through init( )
method.
Execution It cannot run independently, but requires JRE to It cannot start independently but requires APIs
run. for use (Example. APIs like Web API).
Installation We need to install the Java application first and Java applet does not need to be pre-installed.
obviously on the local computer.
Connectivity with server It is possible to establish connections with other It cannot establish connection to other servers.
servers.
Operation It performs read and write tasks on a variety of It cannot run the applications on any local
files located on a local computer. computer.
File access It can easily access a file or data available on a It cannot access the file or data found on any
computer system or device. local system or computer.
Security Java applications are pretty trusted, and thus, Java applets are less reliable. So, they need to
come with no security concerns. be safe.
15
When to use applets
16