Unit - 4

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

Unit – 4

Managing Exceptions & Applet Programming

Chapter 1– Managing errors and Exceptions

Chapter 2– Applet Programming


Chapter 2
Applet Programming
Topics
• Introduction
• How applets differ from applications
• Preparing to write applets
• Building applet code
• Applet Life Cycle
• Creating an executable applet
• Designing a Web Page
• Applet Tag
Topics
• Adding Applet to HTML File
• Running the applet
• More about applet tag
• Passing parameters to applet
• Aligning the display
• More about HTML tags
• Displaying numerical values
• Getting input from the user
• Event Handling
Introduction
What are Applets?
• Applets are small Java programs that are
primarily used in Internet computing.
• They can be transported over the Internet from
one computer to another and run using the Applet
Viewer or any Web browser that supports Java.
• An applet, like any application program can
perform arithmetic operations, display graphics,
play sounds, accept user input, create animation,
and play interactive games.
• A web page can now contain not only a simple
text or a static image but also a Java applet
which, when run, can produce graphics,
sounds and moving images.

• Java applets therefore have begun to make a


significant impact on the World Wide Web.
Local and Remote Applets
• We can embed applets into Web pages in two
ways.
1. Write our own applets and embed them
into Web pages.
2. Download an applet from a remote
computer system and then embed it to a Web
page
Local Applets
• An applet developed locally and stored in a
local system is known as a local applet.
• When a Web page is trying to find a local
applet, it does not need to use the Internet
and therefore the local system does not
require the Internet connection.
• It simply searches the directories in the local
system and locates and loads the specified
applet.
Remote Applet
• A remote applet is that which is developed by
someone else and stored on a remote
computer connected to the Internet.
• If our system is connected to the Internet, we
can download the remote applet onto our
system via at the Internet, and run it.
• In order to locate and load a remote applet,
we must know the applet's address on the
Web. This address is known as Uniform
Resource Locator (URL) and must be specified
in the applet's HTML document.
 How Applets Differ from Applications

• Although both the applets and stand-alone


applications are Java programs, there are
significant differences between them.
• Applets are not full-featured application
programs. They are usually written to
accomplish a small task or a component of a
task.
 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. Applets run
from inside a Web page using a special feature
known as HTML tag.
 Applets cannot read from or write to the files
in the local computer.
 Applets cannot communicate with other
servers on the network.
 Applets cannot run any program from the
local computer.
 Applets are restricted from using libraries
from other languages such as C or C++.

• All these restrictions and limitations are placed in the


interest of security of systems.
• These restrictions ensure that an applet cannot do
any damage to the local system.
Preparing to Write Applets
• we have been creating simple Java application
programs with a single main() method that
created objects, set instance variables and run
methods.
• Here, we will be creating applets exclusively
and therefore we will need to know:
– When to use applets,
– How an applets works,
– What sort of features an applet has, and
– Where to start when we first create our own
applets.
 When to use applets
1. When we need something dynamic to be included
in the display of a Web page.
For example, an applet that displays daily sensitivity
index would be useful on a page that lists share
prices various companies,
Or an applet that displays a bar chart would add
value to a page that contains data tables.
2. When we require some “flash" outputs.
For example, applets that produce sounds,
animations or some special effects would be
useful when displaying certain pages.
3. When we want to create a program and
make it available on the Internet for us by
• Before we try to write applets, we must make sure that
Java is installed properly and also ensure that either the
Java appletviewer or a Java-enabled browser is available.

• The steps involved in developing and testing in applet


are:
1. Building an applet code (.java file)
2. Creating an executable applet (.class file)
3. Designing a Web page using HTML tags
4. Preparing <APPLET> tag
5. Incorporating <APPLET> tag into the Web page
6. Creating HTML file
7. Testing the applet code.
Building Applet Code
• It is essential that our applet code uses the
services of two classes, namely,
-- Applet and Graphics from the Java class
library.

• The Applet class which is contained in the


java.applet package provides life and
behaviour to the applet through its methods
such as init(), start() and paint().
• Unlike the applications, where Java calls the
main() method directly to initiate the
execution of the program, when an applet is
loaded, Java automatically calls a series of
Applet class methods for starting, running and
stopping the applet code.

• The Applet class therefore maintains the


lifecycle of an applet.
• The paint() method of the Applet class, when it
is called, actually displays the result of the applet
code on the screen.
• The output may be text, graphics, or sound.
• The paint() method, which requires a Graphics
object as an argument, is defined as follows:
public void paint (Graphics g)

• This requires that the applet code imports the


java.awt package that contains the Graphics
class.
• All output operations of an applet are performed
using the methods defined in the Graphics class.
• General Format:

• The appletclassname is the main class for the applet. When the
applet is loaded.
• Java creates an instance of this class, and then a series of Applet
The HelloJava Applet Program
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Java",10, 100);
}
}

/*
<applet code = "HelloJava.class" width=500 height=500>
</applet>
*/
• save with the file name HelloJava.java, in a
java subdirectory.
• Note the public keyword for the class
HelloJava. java requires that the main applet
class be declared public.
Applet Life Cycle
• Every Java applet inherits a set of default
behaviours from the Applet class. As a result,
when an applet is loaded, it undergoes a series
of changes in its state.
• The applet states include:
1) Born or initialization state
2) Running state.
3) Idle state
4) Dead or destroyed state
1) Initialization State
• Applet enters the initialization state when it is first
loaded. This is achieved by calling the init() method
of Applet Class.
• The applet is born.
• At this stage, we can do the following, if required.
• Create objects needed by the applet
• Set up initial values
• Load images or fonts
• Set up colors
The initialization occurs only once in the
applet's life cycle. To provide any of the behaviours
to the application we must override the init().
public void init()
{……………………
……………………..(Action)
}
2) Running State
• Applet enters the running state when the system calls
the start() method of Applet Class.
• This occurs automatically after the applet is initialized.
• Starting can also occur if the applet is already in
'stopped" (idle) state.
• For example, we may leave the Web page containing
the applet temporarily to another page and return
back to the page. This again starts the applet running.
• Note that, unlike init() method, the start() method
may be called more than once.
• We may override the start() method to create a
thread to control the applet.
public void start()
{………………..
…………........(Action)

}
3) Idle or Stopped State
• An applet becomes idle when it is stopped from
running.
• Stopping occurs automatically when we leave the
page containing the currently running applet.
• We can also do so by calling the stop() method
explicitly.
• If we use a thread to run the applet, then we
must use stop() method to terminate the thread.
We can achieve this by overriding the stop()
method.
public void stop()
{………………..
……………………..(Action)
}
4) Dead State
• An applet is said to be dead when it is
removed from memory. This occurs
automatically by invoking the destroy()
method when we quit the browser.
• Like initialization, destroying stage occurs only
once in the applet's life cycle.
• If the applet has created any resources, like
threads, we may override the destroy()
method to clean up these resources.
public void destroy()
{………………..
……………………..(Action)
}
5) Display State
• Applet moves to the display state whenever it has
to perform some output operations on the screen.
• This happens immediately after the applet enters
into the running state.
• The paint() method is called to accomplish this
task. Almost every applet will have a paint()
method.
• Like other methods in the life cycle, the default
version of paint() method does absolutely
nothing. We must therefore override this method
if we want anything to be displayed on the screen.
public void paint(Graphics g)
{………………..
…………………(display statements)
}
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. Therefore, we can
use the Java compiler to compile the applet
Designing A Web Page
• Java applets are programs that reside on Web pages. In
order to run a Java applet, it is first necessary to have a
Web page that references that applet.
• A Web page is basically made up of text and HTML tags
that can be interpreted by a Web browser or an applet
viewer.
• Like Java source code, it can be prepared using any text
editor. A Web page is also known as HTML page or
HTML document. Web pages are stored using a file
extension. html such as MyApplet.html. Such files are
referred to as HTML files .
• HTML files should be stored in the same directory as the
compiled code of the applets.
• Web pages include both text that we want to
display and HTML tags (commands) to Web
browsers.
• A Web page is marked by an opening HTML
tag <HTML> and a closing HTML tag </HTML>
and is divided into the following three major
sections:
– 1) Comment section (Optional)
– 2) Head section (Optional)
– 3) Body section
Event handling
• Event handling is a mechanism that is used to
handle events generated by applets. An event
could be the occurrence of any activity such as
a mouse click or a key press. In Java, events
are regarded a as method calls with a certain
task performed against the occurrence of each
event
• Some of the key events in Java are:
ActionEvent is triggered whenever a user interface element is
activated, such as selection of a menu item)ItemEvent is triggered
at the selection or deselection of an itemized or list element, such
as check boxTextEvent is triggered when a text field is modified.)
(WindowEvent is triggered whenever a window-related operation
is performed, such as closing or activating a window.)KeyEvent is
triggered whenever a key is pressed on the keyboard) Each event
is associated with an event source and an event handler. Event
source refers to the object that responsible for generating an
event whereas event listener is the object that is referred by
event source at the time of occurrence of an event)

You might also like