0% found this document useful (0 votes)
21 views13 pages

Applet, Event Handling

Uploaded by

Karthik G B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

Applet, Event Handling

Uploaded by

Karthik G B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

APPLET PROGRAMMING

❖ 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

1.Applet based on AWT(Abstract Window Toolkit)

2.JApplets based on Swings

Advantages of Applet

There are many advantages of applet. They are as follows:

● It works at client side so less response time.


● Secured
● It can be executed by browsers running under many platforms, including Linux,
Windows, Mac Os etc.
Drawback of Applet

● Plugin is required at client browser to execute applet.

❖ LIFE CYCLE OF AN APPLET


When an applet begins, the following methods are called,

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]

>

[< PARAM NAME = appletParameter1 VALUE = value >]

[< PARAM NAME = appletParameter2 VALUE = value >]

...

[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.

STEPS INVOLVED IN DEVELOPING AND TESTING AN APPLET


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 web page

6.Testing the applet code


BUILDING AN APPLET

import java.applet.Applet;

import java.awt.Graphics;

public class HelloWorld extends Applet

public void paint(Graphics g)

g.drawString("Hello World", 20, 20);

CREATING AN EXECUTABLE APPLET


Compile the previous applet program to create .class dile(executable applet)

Ex: compilation: javac HelloWorld.java

HelloWorld.class

HelloWorld.html file

<html>

<body>

<applet code=”HelloWorld.class” width=”400” height=”400”>

</applet>
</body>

</html>

This HTML code satisfies following steps:

● Designing Web page using HTML tags

● Preparing <APPLET> tag

● Embedding <APPLET> into webpage

● Creating HTML file

TESTING THE APPLET CODE


Applet code can be tested by using “appletviewer”.Use .html file name to test applet.

Before testing an applet, it should be compiled.

For compiling applet: javac HelloWorld.java

For testing applet : appletviewer HelloWorld.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.

For example, clicking on a button, moving the mouse, entering a character


through keyboard.
❖ EVENT HANDLING
● Event Handling is the mechanism that controls the event and decides what
should happen if an event occurs.
● This mechanism have the code which is known as event handler that is
executed when an event occurs.
● Java Uses the Delegation Event Model to handle the event which defines
the standard mechanism to generate and handle the events.

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.

Steps involved in event handling


● The User clicks the button and the event is generated.
● Now the object of concerned event class is created automatically and
information about the source and the event get populated within same
object.
● Event object is forwarded to the method of registered listener class.
● The method is now get executed and returns.
● Event will be registered using addEventListener(e)

❖ HANDLING MOUSE EVENTS


import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MouseEvt extends Applet implements


MouseListener,MouseMotionListener
{
String msg=" ";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.drawString(msg,20,20);
}
public void mousePressed(MouseEvent me)
{
msg=" mouse pressed";
repaint();
}
public void mouseClicked(MouseEvent me)
{
msg=" mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
msg=" mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
msg=" mouse exited";
repaint();
}
public void mouseReleased(MouseEvent me)
{
msg=" mouse released";
repaint();
}
public void mouseMoved(MouseEvent me)
{
msg=" mouse moved";
repaint();
}
public void mouseDragged(MouseEvent me)
{
msg=" mouse dragged";
repaint();
}
}
MouseEvents.html
<html>
<body>
<applet code = “MouseEvt.class” width=500 height=500>
</applet>
</body>
</html>

HANDLING KEYBOARD EVENTS

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class KeyBoard extends Applet implements KeyListener

{
String msg=" ";
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
showStatus("keydown");
int k=ke.getKeyCode();
switch(k)

case KeyEvent.VK_F1: msg="F1 pressed";


break;
case KeyEvent.VK_F2: msg="F2 pressed";
break;
case KeyEvent.VK_PAGE_UP: msg="Pageup pressed";
break;
case KeyEvent.VK_PAGE_DOWN: msg="Pagedown pressed";
break;
}
repaint();
}
public void keyReleased(KeyEvent ke)
{
showStatus("key up");
}
public void keyTyped(KeyEvent ke)
{
msg=ke.getKeyChar()+” “+”Pressed”;
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,50);
}
}
KeyBoard.html
<html>
<title>Keyboard Events</title>
<body><applet code="KeyBoard.class" height=100 width=700></applet>
</body>
</html>

You might also like