Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Bule Hora University

College of Informatics
Department of Computer Science

CoSc3053: Advanced Programming

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 a Java-enabled Web browser, we will be able to see the entire


Web page containing the applet.

• 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

• To execute the applet by html file, create an applet and


compile it. After that create an html file and place the
applet code in html file. Now click the html file.
• 1. //First.java
• 2. import java.applet.Applet;
• 3. import java.awt.Graphics;
• 4. public class First extends Applet{
• 5. public void paint(Graphics g){
• 6. g.drawString("welcome",150,150);
• 7. }
• 8. }
11
Cont’d
• myapplet.html
• 1. <html>
• 2. <body>
• 3. <applet code="First.class" width="300"
height="300">
• 4. </applet>
• 5. </body>
• 6. </html>

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

• Applets do not use the main() method for


initiating the execution of the code. Applets,
when loaded, automatically call certain methods
of Applet class to start and execute the applet
code.
• Unlike stand-alone applications, applets cannot
be run independently. They are run from inside
a Web page using a special feature known as
HTML tag.

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

• When we need something dynamic to be


included in the display of a Web page.
• When we require some “flash” outputs.
• When we want to create a program and make it
available on the Internet for us by others on their
computers.

16

You might also like