Applet, Event Handling
Applet, Event Handling
❖ INTRODUCTION
● 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.
● Applets are used to make the web site more dynamic and interactive.
● An applet is embedded in an HTML page using the APPLET or OBJECT tag and
hosted on a web server.
● All applets are sub-classes (either directly or indirectly) of java.applet.Applet
class.
● Applets are not stand-alone programs. Instead, they run within either a web
browser or an applet viewer. JDK provides a standard applet viewer tool called
applet viewer.
Types of Applets
There are 2 Types of Applets
Advantages of Applet
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the following sequence of method calls takes place:
1. stop( )
2. destroy( )
1. init( ) : The init( ) method is the first method to be called. This is where you
should initialize variables. This method is called only once during the run
time of your applet.
2. start( ) : The start( ) method is called after init( ). It is also called to restart
an applet after it has been stopped. Note that init( ) is called once i.e. when
the first time an applet is loaded whereas start( ) is called each time an
applet’s HTML document is displayed onscreen. So, if a user leaves a web
page and comes back, the applet resumes execution at start( ).
3. paint( ) : The paint( ) method is called each time an AWT-based applet’s
output must be redrawn. This situation can occur for several reasons. For
example, the window in which the applet is running may be overwritten by
another window and then uncovered. Or the applet window may be
minimized and then restored.
paint( ) is also called when the applet begins execution. Whatever the applet
must redraw its output, paint( ) is called.The paint( ) method has one
parameter of type Graphics. This parameter will contain the graphics
context, which describes the graphics environment in which the applet is
running. This context is used whenever output to the applet is required.
4. stop( ) : The stop( ) method is called when a web browser leaves the HTML
document containing the applet—when it goes to another page, for example.
When stop( ) is called, the applet is probably running. You should use stop( ) to
suspend threads that don’t need to run when the applet is not visible. You can
restart them when start( ) is called if the user returns to the page.
5. destroy( ) : The destroy( ) method is called when the environment determines
that your applet needs to be removed completely from memory. At this point, you
should free up any resources the applet may be using. The stop( ) method is
always called before destroy( ).
HTML APPLET TAGS
< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels]
>
...
[alternateHTML]
</APPLET>
CODE: Specifies the name of the applet class to be loaded ,i.e., the name of the already
compiled java applet (.class file) This is mandatory attribute to be specified.
CODEBASE: Specifies the URL of the directory in which the applicant resides. if the
applet resides in the same directory as the HTML file then the CODEBASE attribute
maybe skipped.
WIDTH & HEIGHT: These attributes specifies the width and height of the space on
the HTML page that will be reserved for the applet.
NAME: This is an optional attribute which can be specified so that other applets on the
page may refer to this applet by using its name. This attribute provides inter applet
communication.
ALIGN: It is also an optional attribute to specify where on the page the applet should
appear .Possible values for alignment are:
TOP,BOTTOM,LEFT,RIGHT,MIDDLE,ABSMIDDLE,ABSBOTTOM,TEXTTOP ,
BASELINE.
HSPACE: Used only when ALIGN is set to left or right this attribute specifies the
horizontal blank space the browser should leave surrounding the applet.
VSPACE: It is used only when some vertical alignment is specified with ALIGN
attribute and it specifies the amount of vertical blank space the browser should leave
surrounding the applet .Possible values are TOP and BOTTOM.
ALT: Non java browsers will display district where the applet would normally go.
4.Preparing <APPLET>tag
import java.applet.Applet;
import java.awt.Graphics;
HelloWorld.class
HelloWorld.html file
<html>
<body>
</applet>
</body>
</html>
EVENT HANDLING
❖ EVENT
● Change in the state of an object is known as event .
● Events are generated as result of user interaction with the graphical user
interface components.
The Delegation Event Model has the following key participants namely:
● Source - The source is an object on which event occurs. Source is
responsible for providing information of the occurred event to it's handler.
● Listener - It is also known as event handler.Listener is responsible for
generating response to an event. Listener waits until it receives an event.
Once the event is received , the listener process the event and then returns.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
{
String msg=" ";
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
showStatus("keydown");
int k=ke.getKeyCode();
switch(k)