Upload
labhnesh-jindal
View
36
Download
0
Tags:
Embed Size (px)
Citation preview
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
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.
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().
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.
Applet cannot run any program from the local
computer.
All above restrictions ensure that an applet
cannot do any damage to the local system.
Dynamic display
Flash outputs
To produce sounds, animations or some special effects
would be useful when displaying certain pages.
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
An Applet Life Cycle Skeleton
States:
Born or initialization state
Running state
Idle state
Dead or destroyed state.
Begin
start()
stop()
stopped
start() destroy()
Destroyed
End
Born
Running idle
dead
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()
{
-----------
-----------
}
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()
{
----------
----------
}
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()
{
---------
----------
}
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()
{
---------
---------
}
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.
Example 1
import java.awt.*;
import java.applet.*;
public class AppletEx extends Applet
{
public void paint(Graphics g)
{
g.drawString("Applet Example", 20,20);
}
}
<HTML>
<HEAD><title>applet example</title>
</HEAD>
<body>
<APPLET
CODE=AppletEx.class
WIDTH=200
HEIGHT=200></APPLET>
</body>
</html>
Appletviewer AppletEx.html
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
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
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
Passing parameters to applet
1. Include <PARAM…> tag in the HTML document.
2. Provide code in the applet to parse these parameters.
getParameter() method
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
<HTML><HEAD>
<TITLE>JAVA APPLETS</TITLE>
</HEAD>
<BODY>
<APPLET CODE=Example.class WIDTH=400
HEIGHT=200>
<PARAM NAME="string" VALUE="Applet!">
</APPLET>
</BODY>
</HTML>
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
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
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);
}
}
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
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);
}
}
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.
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.
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.
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.
Event Handling
Events
Event source
Event listeners
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.
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().
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.
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.
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().