You are on page 1of 16

Chapter Three

HTML and Applet


Lecture –four
HTML
• HTML is an acronym for Hypertext Mark-up Language
• HTML is a language used to write documents that will be
viewed on a Web browser, either over the Internet or entirely
on your own computer.
Syntax :
< tag >
Some text
</ tag >
Example:
<h1>
World’s Greatest Home Page
</h1>

interpreted by Asaminew G. 2
tag that mark a single location in the text do not need
to be closed with a command of the form
</ tag >
Example <hr>, <br>
<hr> which inserts a horizontal line, a good way to
separate sections.
Some important tags of Html
<html>, <head>,<title> ,<body>,
<center>,<p>, <a>, <img> ….

interpreted by Asaminew G. 3
Outline of an HTML Document
<html>
<head>
<title>
OOP HTML and Applet
</title>
</head>
<body>

….
</body>
</html>

interpreted by Asaminew G. 4
Programming Applets
• Applets
• used for small programs run over the
Internet
• defines no constructors
• do not contain a main method
• normally embedded in an HTML
• displayed automatically by a Web browser or an
applet viewer
• Derived from Applet class, Which is in the
package javax.swing
interpreted by Asaminew G. 5
The Core Applet Methods
A Java Applet is not a stand alone program
All applets expect to find code for five methods
defended in their class file.
These methods are
init() for initialisation applet components,
start() used to start an applet,
stop() is for stopping an applet,
destroy() for cancel all processes and
paint() used for painting .

interpreted by Asaminew G. 6
Initialisation and starting
Initialisation occurs when the applet is first loaded from
the server.
To provide initialisation behaviour for your applet,
override the “init()” method of Applet class
The init() method will always be the very first method
to be called after he e applet is loaded
Starting is different from initialisation because an
applet may be started many times during its life,
whereas initialisation can only happen once,
immediately after loading.
To provide starting behaviour, override the start()
method of the Applet class

interpreted by Asaminew G. 7
Stopping
An applet residing on a particular HTML page will be stopped by
the browser if the user follows a link to another page.
It will be started again if the user revisits the original page.
To provide stopping behaviour, override the stop() method of
the Applet class
The default implementation of start() and stop() provided by
Applet do nothing.

interpreted by Asaminew G. 8
Destroying
• The browser’s applet control program will call
method destroy() from an applet class just before the
code for that class is removed from the browser catch.

• Painting
• Painting is the way an applet actually draws
something on the screen, be it text, a line, a coloured
background, or an image.
• To provide paining behaviour, override the paint()
method of the JApplet class
• paint method accepts a Graphics object as argument

interpreted by Asaminew G. 9
Example
• …

interpreted by Asaminew G. 10
Example
• …

interpreted by Asaminew G. 11
Exception and its handling
• Exception :
• a program that disrupts the normal flow of the
program during execution
• Caused by user or programmer
• Example :
• Dividing an integer by zero
• A user has entered invalid data
• A file that needs to be opened cannot be found
• Accessing an element that is out of bounds in
an array
interpreted by Asaminew G. 12
Exception cont. …
• Exception : Divided into two:
• Checked exceptions:
• is an exception that is typically a user error or a
problem that cannot be foreseen by the programmer
• have been declared or handled
• E.g. IOException
• Runtime exceptions (unchecked):
• is an exception that occurs and probably could have
been avoided by the programmer
• do not have to be declared or handled
• E.g. ArithmeticException

interpreted by Asaminew G. 13
Exception handling
• Exception handling
• enables programmers to remove error-handling code
• works by transferring the execution of a program to an
appropriate exception handler when an exception occurs
• use the try and catch keywords
• If an exception occur in try block it jump to catch block
• There is more than one catch block

interpreted by Asaminew G. 14
Finally
• Finally :
• used after a try block or after a try/catch block
• code in the finally block is used to clean up after the
code in the try block e.g. close any open files,
release system resources, display some output to
the user
• there is only one finally block
• execute before program execution ends
• executed if any part of the try block is executed
with exception or no exception
• can be added after a try block, if there are no catch
blocks. Or it can be added after the catch block
interpreted by Asaminew G. 15
Cont. …
• Syntax of finally keyword and try catch block;

or

interpreted by Asaminew G. 16

You might also like