You are on page 1of 16

HAWASSA UNIVERSITY

Institute of Technology (IOT)


Faculty of Informatics
Department of Computer Science (BSc - Evening)

Object Oriented Programming (CoSc2021)

Compiled By: Areg. T. 1


Chapter Seven

Java Applets

Compiled By: Areg. T. 2


Outlines
Overview of Java Applets

Java Applets vs Java Applications

Compiled By: Areg. T. 3


Overview of Java Applets
Applets are Java programs that are normally embedded in web
pages.

Applets are small or mini Java applications that can be accessed


on an network server, transported over network, and can be
automatically installed and run as apart of a web document.

Applets may be slow to start since they are loaded from the
network.
Compiled By: Areg. T. 4
Overview of Java Applets
Java allows you to create small or mini applications, commonly
known as applets, which can be executed inside a web browser.

Applets were created to add an interactive element to an HTML


web page.

Since applets are really just a special Java application, they can
be executed regardless of the platform the user is running.
Compiled By: Areg. T. 5
Overview of Java Applets
Applets are executed entirely on the client side. Their only
requirement is that the remote system or computer needs to have
the Java Virtual Machine (JVM) installed.

A Java applet does not have any main() method. It is viewed


using JVM.

The JVM can use either as a plug-in of the web browser or a


separate runtime environment to run an applet program.
Compiled By: Areg. T. 6
Overview of Java Applets
Example:

Compiled By: Areg. T. 7


Overview of Java Applets
How to run an Applet Program:
An applet program is compiled in the same way as we have been
compiling our console programs.

Compiled By: Areg. T. 8


Overview of Java Applets
However, there are two ways to run an applet program:
1. Executing the applet program within Java-compatible or Java
plug-in web browser.

2. Using an applet viewer, such as the standard tool, applet viewer.


An applet viewer executes our applet program in a window.

Compiled By: Areg. T. 9


Overview of Java Applets
To execute an applet program in a web browser, create a short
HTML file in the same directory.

Inside the body tag of the HTML file, include the following
code. ( applet tag loads the applet class). Note we have assumed
your Java applet program is named as "AppletApp".

Compiled By: Areg. T. 10


Overview of Java Applets
Run or open the HTML file. Note – the following is a sample
output:

Compiled By: Areg. T. 11


Overview of Java Applets
To execute an applet program with an applet viewer, write short
HTML file as discussed above. If the name of the HTML file is
MyApplet.html, then the following command will run your applet
program.

Compiled By: Areg. T. 12


Overview of Java Applets
Output:

Compiled By: Areg. T. 13


Java Applets vs Java Applications
From a programming perspective, there is very little difference
between a Java desktop application and a Java applet.

The main difference between the two is the way the code is
invoked.

Compiled By: Areg. T. 14


Java Applets vs Java Applications
In the case of a Java application, program execution begins by
executing the very first executable statement in the main()
method.

Therefore, all Java application programs mush have a main()


method.

In Java Application, the initialization code is kept in a


constructor.
Compiled By: Areg. T. 15
Java Applets vs Java Applications
Java applets have NO main method. Web browser invokes or
loads init(), start(), stop(), and destroy() methods in sequence.

Hence from a programming perspective, creating an applet


involves inheriting a Applet class and overriding methods init(),
start(), stop(), and destroy().

In Java applet initialization code is kept in init() method.


Compiled By: Areg. T. 16

You might also like