Applet & Event handling.pdf

38
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 other applications can do many things example it can perform arithmetic operations, display graphics, play sounds, accept user input, create animation etc. Types Local Applet Remote Applet

Transcript of Applet & Event handling.pdf

Page 1: Applet & Event handling.pdf

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 other applications can do many things

example it can perform arithmetic operations, display

graphics, play sounds, accept user input, create animation

etc.

Types

Local Applet

Remote Applet

Page 2: Applet & Event handling.pdf

Local and Remote Applets

We can embed applets into web pages in two ways.

we can write our own applets and embed them into

web pages.

We can download an applet from a remote computer

system and then embed into web page.

An applet developed locally and stored in a local system

is known as a local applet.

A remote applet is that which is developed by someone

else and stored on a remote computer connected to the

internet.

Page 3: Applet & Event handling.pdf

Applet Differ From ApplicationsApplet Application

The execution of the applet does not start

from main() method, as it does not have

one.

The execution of an application program

starts from main().

Applets cannot run on their own. They

have to be embedded inside a web page

to get executed.

These can run on their own. In order to

get executed, they need not be embedded

inside any web page.

Applets can only be executed inside a

browser or appletviewer.

Applications are executed at command

line.

Applets cannot read from or write to the

files in the local computer.

??

Applets are restricted from using libraries

from other languages such as C or C++.

Applications can import C or C++

programs using JNI

Applet have their own life cycle. Applications have their own lifecycle.

Their execution begins at main().

Page 4: Applet & Event handling.pdf

Applet Differ From Applications

Applet 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 applet code.

Unlike stand-alone applications, applets cannot be run

independently. They are run from inside a web page using

a special feature known as HTML tag.

Applets cannot communicate with other servers on the

network.

Page 5: Applet & Event handling.pdf

Applet cannot run any program from the local

computer.

All above restrictions ensure that an applet

cannot do any damage to the local system.

Page 6: Applet & Event handling.pdf

Dynamic display

Flash outputs

To produce sounds, animations or some special effects

would be useful when displaying certain pages.

Page 7: Applet & Event handling.pdf

Steps involved in developing 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 the web page

6. Creating HTML file

7. Testing the applet code

Page 8: Applet & Event handling.pdf

An Applet Life Cycle Skeleton

States:

Born or initialization state

Running state

Idle state

Dead or destroyed state.

Page 9: Applet & Event handling.pdf

Begin

start()

stop()

stopped

start() destroy()

Destroyed

End

Born

Running idle

dead

Page 10: Applet & Event handling.pdf

Initialization state

Applet enters the initialization state when it is first loaded. This is

achieved by calling the init() method of Applet class.

Create objects needed by the applet

Set up initial values

Load images or fonts

Set up colors

Initialization occurs only once in the applet’s life cycle.

public void init()

{

-----------

-----------

}

Page 11: Applet & Event handling.pdf

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 occurs if

applet is already in stopped state. Start method may be

called more than once.

public void start()

{

----------

----------

}

Page 12: Applet & Event handling.pdf

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 stop() method.

public void stop()

{

---------

----------

}

Page 13: Applet & Event handling.pdf

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 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()

{

---------

---------

}

Page 14: Applet & Event handling.pdf

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.

Page 15: Applet & Event handling.pdf

Example 1

import java.awt.*;

import java.applet.*;

public class AppletEx extends Applet

{

public void paint(Graphics g)

{

g.drawString("Applet Example", 20,20);

}

}

Page 16: Applet & Event handling.pdf

<HTML>

<HEAD><title>applet example</title>

</HEAD>

<body>

<APPLET

CODE=AppletEx.class

WIDTH=200

HEIGHT=200></APPLET>

</body>

</html>

Page 17: Applet & Event handling.pdf

Appletviewer AppletEx.html

Page 18: Applet & Event handling.pdf

Applet Display methods

Void setBackground(Color newColor)

Void setForeground(Color new Color)

Color.black Color.green

Color.blue Color.lightGray

Color.darkGray Color.magenta

Color.gray Color.pink

Color.red Color.yellow

Color.white

Page 19: Applet & Event handling.pdf

import java.awt.*;

import java.applet.*;

public class Sample extends Applet{

String msg;

public void init()

{setBackground(Color.gray);

setForegorund(Color.red);

msg="Inside init()--";

}

public void start()

{

msg+="Inside start()--";

}

public void paint(Graphics g){

msg+="Inisde paint()--";

g.drawString(msg,10,10);

}

}

Example 2

Page 20: Applet & Event handling.pdf

Status window:

By default: Applet started

import java.awt.*;

import java.applet.*;

public class Status extends Applet{

public void init()

{setBackground(Color.gray);

}

public void paint(Graphics g){

g.drawString("This is in the applet window.",10,20);

showStatus("Applet status window");

}

}

Example 3

Page 21: Applet & Event handling.pdf

Passing parameters to applet

1. Include <PARAM…> tag in the HTML document.

2. Provide code in the applet to parse these parameters.

getParameter() method

Page 22: Applet & Event handling.pdf

import java.awt.*;

import java.applet.*;

public class Example extends Applet{

String str;

public void init()

{

str=getParameter("string");

if(str==null)

str="Java";

str="Hello" +str;

}

public void paint(Graphics g){

g.drawString(str,10,100);}

}

Example 4

Page 23: Applet & Event handling.pdf

<HTML><HEAD>

<TITLE>JAVA APPLETS</TITLE>

</HEAD>

<BODY>

<APPLET CODE=Example.class WIDTH=400

HEIGHT=200>

<PARAM NAME="string" VALUE="Applet!">

</APPLET>

</BODY>

</HTML>

Page 24: Applet & Event handling.pdf

import java.awt.*;

import java.applet.*;

public class Example extends Applet{

public void paint(Graphics g)

{

int value1=10;

int value2=20;

int sum=value1+value2;

String s="SUM=" + String.valueOf(sum);

g.drawString(s,100,100);

}

}

Example 5

Page 25: Applet & Event handling.pdf

import java.awt.*;

import java.applet.*;

public class paramex extends Applet{

String author;

int ver;

public void start()

{

String temp;

author=getParameter("author");

if(author==null)author="not found";

temp=getParameter("version");

Example 6

Page 26: Applet & Event handling.pdf

try{

if(temp!=null)

ver=Integer.parseInt(temp);

else

ver=0;

}catch(NumberFormatException exc)

{

ver=-1;

}

}

public void pain(Graphics g)

{

g.drawString("By“ + author,10,40);

g.drawString("version" + ver, 10,60);

}

}

Page 27: Applet & Event handling.pdf

import java.awt.*;

import java.applet.*;

public class Example extends Applet{

TextField text1,text2;

public void init(){

text1=new TextField(8);

text2=new TextField(8);

add(text1);

add(text2);

text1.setText(“10");

text2.setText(“10");

}

Example 7

Page 28: Applet & Event handling.pdf

public void paint(Graphics g){

int x=0,y=0,z=0;

String s1,s2,s;

s1=text1.getText();

x=Integer.parseInt(s1);

s2=text2.getText();

y=Integer.parseInt(s2);

z=x+y;

s=String.valueOf(z);

g.drawString(s,100,100);

}

}

Page 29: Applet & Event handling.pdf

Program

Create an applet that displays the current time,

updated once per second. Here is an hint to help

you get started: one way to obtain the current time

is to use a Calendar object, which is part of the

java.util package.

Page 30: Applet & Event handling.pdf

Develop an applet that receives three numeric

values as input from the user and then displays the

largest of the three on the screen. Write a HTML

page and test the applet.

Page 31: Applet & Event handling.pdf

Event Handling

The delegation event model:

The modern approach to handling events is based on

delegation event model, which defines standard and

consistent mechanisms to generate and process events.

A source generates an event and sends it to one or

more listeners. The listener simply waits until it

receives an event. Once an event is received, the

listener processes the event and then returns.

Page 32: Applet & Event handling.pdf

Event Handling

In delegation event model, listeners must register with

a source in order to receive an event notification. This

provides an important benefit: notification are sent

only to listeners that wants to receive them.

Page 33: Applet & Event handling.pdf

Event Handling

Events

Event source

Event listeners

Page 34: Applet & Event handling.pdf

Events

In the delegation model, an event is an object that

describes a state change in a source. It can be

generated as a consequence of a person

interacting with the elements in a graphical user

interface. Some of the activities that cause events

to be generated are pressing a button, entering a

character via the keyboard, selecting an item in a

list, and clicking the mouse.

Page 35: Applet & Event handling.pdf

Event source

A source is an object that generates an event. This occurs

when the internal state of that object changes in some

way. Sources may generate more than one type of event.

A source must register listeners in order for the listeners

to receive notifications about a specific type of event.

Each type of event has its own registration method.

Public void addTypeListener(TypeListener el)

Here, Type is the name of the event, and el is a reference

to the event listener. For example the method that

registers a keyboard event listener is called

addKeyListener(). The method that resgisters a mouse

motion listener is called addMouseMotionListener().

Page 36: Applet & Event handling.pdf

Event source

When an event occurs, all registered listeners are notified

and receive a copy of the event object. This is known as

multicatsing the event. In all cases, notifications are sent

only to listeners that register to receive them.

Some sources may allow only one listener to register.

Public void addTypeListener(TypeListener el) throws

java.util.TooManyListenersException

Here, type is the name of the event, and el is a

reference to the event listener is notified. This is known

as unicasting the event.

Page 37: Applet & Event handling.pdf

Event Listener

A listener is an object that is notified when an event

occurs. It has two major requirements.

It must have been registered with source to receive

notification about specific type of events.

It must implement methods to receive and process

these notification.

Page 38: Applet & Event handling.pdf

Event source

A source must also provide a method that allows a

listener to unregister an interest in a specific type of

event.

Public void removeTypeListener(TypeListener el)

Here Type is the name of the event, and el is a

reference to the event listener. For example to remove

a keyboard listener, call removeKeyListener().