Unit - 4
Unit - 4
Unit - 4
• 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)