Вы находитесь на странице: 1из 29

JAVA APPLETS

INTRODUCTION :
Java can be used to create two types of programs:
1.Applications
2.Applets.
An application is a program that runs on your computer, under the operating
system of that computer.
An applet is an application designed to be transmitted over the Internet and
executed by a Javacompatible !eb browser. An applet is actually a tiny
Java program, dynamically downloaded across the networ", #ust li"e an
image, sound file, or video clip.
Applet Fundamentals
$he fundamentals connected to the creation of an applet are presented here
import #ava.awt.%&
import #ava.applet.%&
public class 'impleApplet extends Applet (
public void paint)*raphics g+ (
g.draw'tring),A 'imple Applet,, 2-, 2-+&
.
.
$his applet begins with two import statements.
1. $he first imports the Abstract !indow $ool"it )A!$+ classes. Applets
interact with the user through the A!$, not through the console
based I/0 classes. $he A!$ contains support for a windowbased,
graphical interface.
2. $he second import statement imports the applet pac"age, which
contains the class Applet. 1very applet that you create must be a
subclass of Applet.
$he next line in the program declares the class 'impleApplet. $his class
must be declared as public, because it will be accessed by code that is
outside the program.
Inside 'impleApplet, paint) + is declared. $his method is defined by the
A!$ and must be overridden by the applet. paint) + is called each time that
the applet must redisplay its output. $he paint) + method has one parameter
of type *raphics. $his parameter contains the graphics context, which
describes the graphics environment in which the applet is running.
Inside paint) + is a call to draw'tring) +, which is a member of the *raphics
class. $his method outputs a string beginning at the specified 2,3 location.
It has the following general form:
void drawString(String message, int x, int y)
4ere, message is the string to be output beginning at x,y. In a Java window,
the upperleft
corner is location -,-. $he call to draw'tring) + in the applet causes the
message 5A 'imple Applet6 to be displayed beginning at location 2-,2-.
7otice that the applet does not have a main) + method. 8nli"e Java
programs, applets do not begin execution at main) +. In fact, most applets
don9t even have a main) + method. Instead, an applet begins execution when
the name of its class is passed to an applet viewer or to a networ" browser.
:unning 'impleApplet involves a different process. In fact, there are two
ways in which you can run an applet:
; 1xecuting the applet within a Javacompatible !eb browser.
; 8sing an applet viewer, such as the standard '<= tool, appletviewer. An
applet viewer executes your applet in a window. $his is generally the fastest
and easiest way to test your applet.
In general, you can >uic"ly iterate through applet development by using
these three steps:
1. 1dit a Java source file.
2. ?ompile your program.
@. 1xecute the applet viewer, specifying the name of your applet9s source
file. $he applet viewer will encounter the AAAB1$ tag within the comment
and execute your applet.
$he window produced by 'impleApplet, as displayed by the applet viewer,
is shown in the following illustration:
4ere are the "ey points that you should remember now:
; Applets do not need a main) + method.
; Applets must be run under an applet viewer or a Javacompatible browser.
; 8ser I/0 is not accomplished with Java9s stream I/0 classes. Instead,
applets use the interface provided by the A!$.
; $he Applet class is contained in the #ava.applet pac"age
An Applet Skeleton
// An Applet s"eleton.
import #ava.awt.%&
import #ava.applet.%&
/%
Capplet codeD,Applet'"el, widthD@-- heightD1--E
C/appletE
%/
public class Applet'"el extends Applet (
// ?alled first.
public void init)+ (
// initialiFation
.
/% ?alled second, after init)+. Also called whenever
the applet is restarted. %/
public void start)+ (
// start or resume execution
.
// ?alled when the applet is stopped.
public void stop)+ (
// suspends execution
.
/% ?alled when applet is terminated. $his is the last
method executed. %/
public void destroy)+ (
// perform shutdown activities
.
// ?alled when an appletGs window must be restored.
public void paint)*raphics g+ (
// redisplay contents of window
.
.
Applet Initialization and Termination
It is important to understand the order in which the various methods shown
in the s"eleton are called. !hen an applet begins, the A!$ calls the
following methods, in this se>uence:
1. init) +
2. start) +
@. paint) +
!hen an applet is terminated, the following se>uence of method calls ta"es
place:
1. stop) +
2. destroy) +
Bet9s loo" more closely at these methods.
init) +
$he init) + method is the first method to be called. $his is where you should
initialiFe variables. $his method is called only once during the run time of
your applet.
start) +
$he start) + method is called after init) +. It is also called to restart an applet
after it has been stopped. !hereas init) + is called onceHthe first time an
applet is loadedHstart) + is called each time an applet9s 4$IB document is
displayed onscreen. 'o, if a user leaves a web page and comes bac", the
applet resumes execution at start) +.
paint) +
$he paint) + method is called each time your applet9s output must be
redrawn.$he paint) + method has one parameter of type *raphics. $his
parameter will contain the graphics context, which describes the graphics
environment in which the applet is running. $his context is used whenever
output to the applet is re>uired.
stop) +
$he stop) + method is called when a web browser leaves the 4$IB
document containing
the appletHwhen it goes to another page, for example
destroy) +
$he 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. $he stop) + method is
always called before destroy) +.
Event Handling Mechanisms
The Delegation Event Model
Its concept is >uite simple: A source generates an event and sends it to one or
more listeners. In this scheme, the listener simply waits until it receives an
event. 0nce received, the listener processes the event and then returns. $he
advantage of this design is that the application logic that processes events is
cleanly separated from the user interface logic that generates those events. In
the delegation event model, listeners must register with a source in order to
receive an event notification. $his provides an important benefit:
notifications are sent only to listeners that want to receive them
1vents
In the delegation model, an event is an ob#ect that describes a state change in
a source.
1vent 'ource
A source is an ob#ect that generates an event. 'ources 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. 1ach type of event has its own registration
method.
4ere is the general form:
public void addTypeListener(TypeListener el)
4ere, $ype is the name of the event and el is a reference to the event listener.
A source must also provide a method that allows a listener to unregister an
interest
in a specific type of event. $he general form of such a method is this:
public void removeTypeListener(TypeListener el)
4ere, $ype is the name of the event and el is a reference to the event listener.
Jor example, to remove a "eyboard listener, you would call
remove=eyBistener) +
1vent Bisteners
A listener is an ob#ect that is notified when an event occurs. It has two ma#or
re>uirements.
Jirst, it must have been registered with one or more sources to receive
notifications about specific types of events. 'econd, it must implement
methods to receive and process these notifications.
1vent ?lasses
At the root of the Java event class hierarchy is 1vent0b#ect, which is in
#ava.util.
It is the superclass for all events. Its one constructor is shown here:
1vent0b#ect)0b#ect src+
4ere, src is the ob#ect that generates this event.
1vent0b#ect contains two methods:
get'ource) + and
to'tring) +.
$he get'ource) + method returns the source of the event. Its general form is
shown here:
0b#ect get'ource) +
As expected, to'tring) + returns the string e>uivalent of the event.
Event Class Description
Action1vent *enerated when a button is pressed, a list item is
doubleclic"ed,
or a menu item is selected.
Ad#ustment1vent *enerated when a scroll bar is manipulated.
?omponent1vent *enerated when a component is hidden, moved, resiFed,
or
becomes visible.
?ontainer1vent *enerated when a component is added to or removed
from a
container.
Jocus1vent *enerated when a component gains or loses "eyboard
focus.
Input1vent Abstract super class for all component input event
classes.
Item1vent *enerated when a chec" box or list item is clic"ed& also
occurs
when a choice selection is made or a chec"able menu
item is
selected or deselected.
=ey1vent *enerated when input is received from the "eyboard.
Iouse1vent *enerated when the mouse is dragged, moved,
clic"ed, pressed, or
released& also generated when the mouse enters or
exits a
component.
Iouse!heel1vent *enerated when the mouse wheel is moved
$ext1vent *enerated when the value of a text area or text field is
changed.
!indow1vent *enerated when a window is activated, closed,
deactivated,
deiconified, iconified, opened, or >uit.
Ex.No: 1(A) DEMONSTRATION OF AWT
CONTROLS
DATE: 13.07.10
Aim:
$o write a #ava program for demonstrating the following A!$
controls
1. Kutton 2.?hec"box @.?hoice
L.Bist M.$extJield N.'crollbar
4ardware re>uirements:
Intel pentium Arocessor IO
12Pmb :AI
'oftware re>uirements:
Jd"1.N.-
Algorithm:
1. ?reate a Kutton A!$ control , select a suitable layout manager to
place and demonstrate it9s use by clic"ing the Kutton.
2. ?reate a ?hec"box A!$ control , select a suitable layout manager to
place and demonstrate it9s use by selecting the ?hec"box.
@. ?reate a ?hoice A!$ control , select a suitable layout manager to
place and demonstrate it9s use by selecting a item.
L. ?reate a Bist A!$ control , select a suitable layout manager to place
and demonstrate it9s use by selecting items.
M. ?reate a $extJield and $extArea A!$ control , select a suitable layout
manager to place and demonstrate it9s use by editing the $extJield.
N. ?reate a 'crollbar A!$ control , select a suitable layout manager to
place and demonstrate it9s use by clic"ing the thumb.
DEMONSTRATION OF AWT CONTROLS
PROGRAMS:
)i+$ext Jields:
import #ava.awt.%&
import #ava.awt.event.%&
import #ava.applet.%&
/% Capplet codeD,$extJield<emo, widthDM-- heightD1M-E
C/appletE
%/
public class $extJield<emo extends Applet implements ActionBistener
(
$extJield name,pass&
public void init)+
(
Babel namepDnew Babel),7ame : ,,Babel.:I*4$+&
Babel passpDnew Babel),Aassword : ,,Babel.:I*4$+&
nameDnew $extJield)12+&
passDnew $extJield)P+&
pass.set1cho?har)G%G+&
add)namep+&
add)name+&
add)passp+&
add)pass+&
name.addActionBistener)this+&
pass.addActionBistener)this+&
.
public void actionAerformed)Action1vent ae+
(
repaint)+&
.
public void paint)*raphics g+
(
g.draw'tring),7ame : ,Qname.get$ext)+,N,N-+&
g.draw'tring),Aassword : ,Qpass.get$ext)+,N,P-+&
.
.
08$A8$:
:1'8B$:
$hus the program to demonstrate text field was successfully
executed and the output was verified.
)ii+ Kuttons:
import #ava.awt.%&
import #ava.awt.event.%&
import #ava.applet.%&
/% Capplet codeD,Kutton<emo, widthD@M- heightD1M-E
C/appletE
%/
public class Kutton<emo extends Applet implements ActionBistener
(
Kutton yes,no&
'tring msgD, ,&
public void init)+
(
yesDnew Kutton),3es,+&
noDnew Kutton),7o,+&
add)yes+&
add)no+&
yes.addActionBistener)this+&
no.addActionBistener)this+&
.
public void actionAerformed)Action1vent ae+
(
'tring strDae.getAction?ommand)+&
if)str.e>uals),3es,++
(
msgD,Aressed 3es,&
.
else
(
msgD,Aressed 7o,&
.
repaint)+&
.
public void paint)*raphics g+
(
g.draw'tring)msg,N,1--+&
.
.
08$A8$:
:1'8B$:
$hus the program to demonstrate button was successfully
executed and the output was verified.
)iii+ ?hoice:
import #ava.awt.%&
import #ava.awt.event.%&
import #ava.applet.%&
/% Capplet codeD,?hoice<emo, widthDM-- heightD1M-E
C/appletE
%/
public class ?hoice<emo extends Applet implements ItemBistener
(
?hoice os,br&
public void init)+
(
osDnew ?hoice)+&
brDnew ?hoice)+&
add)os+&
add)br+&
os.add),!indows 2A,+&
os.add),!indows Oista,+&
os.add),'olaris,+&
os.add),Iac 0',+&
br.add),Internet 1xplorer,+&
br.add),JireJox,+&
br.add),0pera,+&
os.addItemBistener)this+&
br.addItemBistener)this+&
.
public void item'tate?hanged)Item1vent ie+
(
repaint)+&
.
public void paint)*raphics g+
(
g.draw'tring),?urrent 0': ,Qos.get'electedItem)+,N,1--+&
g.draw'tring),?urrent Krowser: ,Qbr.get'electedItem)+,N,12-+&
.
.
08$A8$:
:1'8B$:
$hus the program to demonstrate choice was successfully
executed and the output was verified.
)iv+ Bist:
import #ava.awt.%&
import #ava.awt.event.%&
import #ava.applet.%&
/% Capplet codeD,Bist<emo, widthDM-- heightD1M-E
C/appletE
%/
public class Bist<emo extends Applet implements ActionBistener
(
Bist os,br&
'tring msgD, ,&
public void init)+
(
osDnew Bist)L,true+&
brDnew Bist)L,true+&
add)os+&
add)br+&
os.add),!indows 2A,+&
os.add),!indows Oista,+&
os.add),'olaris,+&
os.add),Iac 0',+&
br.add),Internet 1xplorer,+&
br.add),Jirefox,+&
br.add),0pera,+&
os.addActionBistener)this+&
br.addActionBistener)this+&
.
public void actionAerformed)Action1vent ae+
(
repaint)+&
.
public void paint)*raphics g+
(
int indexRS,i&
msgD,?urrent 0': ,&
indexDos.get'electedIndexes)+&
for)iD-&iCindex.length&iQQ+
(
msgQDos.getItem)indexRiS+Q, ,&
.
g.draw'tring)msg,N,1--+&
msgD,?urrent Krowser: ,&
indexDbr.get'electedIndexes)+&
for)iD-&iCindex.length&iQQ+
(
msgQDbr.getItem)indexRiS+Q, ,&
.
g.draw'tring)msg,N,12-+&
.
.
08$A8$:
:1'8B$:
$hus the program to demonstrate list was successfully executed
and the output was verified.
)v+ ?hec" box:
import #ava.awt.%&
import #ava.awt.event.%&
import #ava.applet.%&
/% Capplet codeD,?K<emo, widthDM-- heightD1M-E
C/appletE
%/
public class ?K<emo extends Applet implements ItemBistener
(
?hec"box win2A,winOis,sol,mac&
public void init)+
(
win2ADnew ?hec"box),!indows 2A,,null,false+&
winOisDnew ?hec"box),!indows Oista,,null,false+&
solDnew ?hec"box),'olaris,,null,false+&
macDnew ?hec"box),Iac 0',,null,false+&
add)win2A+&
add)winOis+&
add)sol+&
add)mac+&
win2A.addItemBistener)this+&
winOis.addItemBistener)this+&
sol.addItemBistener)this+&
mac.addItemBistener)this+&
.
public void item'tate?hanged)Item1vent ie+
(
repaint)+&
.
public void paint)*raphics g+
(
g.draw'tring),!indows 2A: ,Qwin2A.get'tate)+,N,P-+&
g.draw'tring),!indows Oista: ,QwinOis.get'tate)+,N,1--+&
g.draw'tring),'olaris: ,Qsol.get'tate)+,N,12-+&
g.draw'tring),Iac 0': ,Qmac.get'tate)+,N,1L-+&
.
.
08$A8$:
:1'8B$:

$hus the program to demonstrate chec" box was successfully executed and
the output was verified.
)vi+ 'croll bar:
import #ava.awt.%&
import #ava.awt.event.%&
import #ava.applet.%&
/% Capplet codeD,'K<emo, widthDM-- heightD1M-E
C/appletE
%/
public class 'K<emo extends Applet implements
Ad#ustmentBistener,IouseIotionBistener
(
'tring msgD, ,&
'crollbar vertsb,horFsb&
public void init)+
(
int widthDInteger.parseInt)getAarameter),width,++&
int heightDInteger.parseInt)getAarameter),height,++&
vertsbDnew 'crollbar)'crollbar.O1:$I?AB,-,1,-,height+&
horFsbDnew 'crollbar)'crollbar.40:IT07$AB,-,1,-,width+&
add)vertsb+&
add)horFsb+&
vertsb.addAd#ustmentBistener)this+&
horFsb.addAd#ustmentBistener)this+&
addIouseIotionBistener)this+&
.
public void ad#ustmentOalue?hanged)Ad#ustment1vent ae+
(
repaint)+&
.
public void mouse<ragged)Iouse1vent me+
(
int xDme.get2)+&
int yDme.get3)+&
vertsb.setOalue)y+&
horFsb.setOalue)x+&
repaint)+&
.
public void mouseIoved)Iouse1vent me+ ( .
public void paint)*raphics g+
(
msgD,Oertical: ,Qvertsb.getOalue)+&
msgQD, , 4oriFontal: ,QhorFsb.getOalue)+&
g.draw'tring)msg,N,12-+&
g.draw'tring),%,,horFsb.getOalue)+,vertsb.getOalue)++&
.
.
08$A8$:
:1'8B$:

$hus the program to demonstrate scroll bar was successfully executed and
the output was verified.
Ex.No: 1(b) APPLICATION OF AWT CONTROLS
(IO!DATA)
DATE: "0.07.10
Aim:
$o write a #ava program for demonstrate the application of A!$
controls.
4ardware re>uirements:
Intel pentium Arocessor IO
12Pmb :AI
'oftware re>uirements:
Jd"1.N.-
Algorithm:
1. ?reate a Kutton A!$ control , select a suitable layout manager to
place and demonstrate it9s use by clic"ing the Kutton.
2. ?reate a ?hec"box A!$ control , select a suitable layout manager to
place and demonstrate it9s use by selecting the ?hec"box.
@. ?reate a ?hoice A!$ control , select a suitable layout manager to
place and demonstrate it9s use by selecting a item.
L. ?reate a Bist A!$ control , select a suitable layout manager to place
and demonstrate it9s use by selecting items.
M. ?reate a $extJield and $extArea A!$ control , select a suitable layout
manager to place and demonstrate it9s use by editing the $extJield.
N. ?reate a 'crollbar A!$ control , select a suitable layout manager to
place and demonstrate it9s use by clic"ing the thumb.
U. 8se all these controls to design a biodata form.
APPLICATION OF AWT CONTROL(IO!DATA)
PROGRAM:
import #ava.awt.%&
import #ava.awt.event.%&
import #ava.applet.%&
/% Capplet codeD,Kiodata, widthDM-- heightDM--E
C/appletE
%/
public class Kiodata extends Applet implements ActionBistener,ItemBistener
(
'tring msgD, ,&
Babel namel,dobl,genderl,intl&
$extJield name&
?hoice date,month,year&
?hec"box male,female&
?hec"box*roup cbg&
Bist interest&
Kutton submit&
'tring str&
public void init)+
(
setBayout)null+&
//?reate Babels
namelD new Babel),7ame : ,,Babel.B1J$+&
namel.setKounds)M-,M-,M-,2M+&
doblD new Babel),<0K : ,,Babel.B1J$+&
dobl.setKounds)M-,V-,M-,2M+&
genderlD new Babel),*ender : ,,Babel.B1J$+&
genderl.setKounds)M-,1@-,N-,2M+&
intlD new Babel),Areas of Interest : ,,Babel.B1J$+&
intl.setKounds)M-,1U-,1--,2M+&
//?reate $extJield
nameD new $extJield)2-+&
name.setKounds)1U-,M-,P-,2M+&
//?reate ?hoices
dateD new ?hoice)+&
date.setKounds)1U-,V-,L-,2M+&
monthD new ?hoice)+&
month.setKounds)22-,V-,M-,2M+&
yearD new ?hoice)+&
year.setKounds)2P-,V-,N-,2M+&
//Add items to choices
year.add),1VPM,+&
year.add),1VPN,+&
year.add),1VPU,+&
year.add),1VPP,+&
year.add),1VPV,+&
year.add),1VV-,+&
month.add),Jan,+&
month.add),Jeb,+&
month.add),Iar,+&
month.add),Apr,+&
month.add),Iay,+&
month.add),Jun,+&
month.add),Jul,+&
month.add),Aug,+&
month.add),'ep,+&
month.add),0ct,+&
month.add),7ov,+&
month.add),<ec,+&
date.add),1,+&
date.add),2,+&
date.add),@,+&
date.add),L,+&
date.add),M,+&
date.add),N,+&
date.add),U,+&
date.add),P,+&
date.add),V,+&
date.add),1-,+&
date.add),11,+&
date.add),12,+&
date.add),1@,+&
date.add),1L,+&
date.add),1M,+&
date.add),1N,+&
date.add),1U,+&
date.add),1P,+&
date.add),1V,+&
date.add),2-,+&
date.add),21,+&
date.add),22,+&
date.add),2@,+&
date.add),2L,+&
date.add),2M,+&
date.add),2N,+&
date.add),2U,+&
date.add),2P,+&
date.add),2V,+&
date.add),@-,+&
date.add),@1,+&
//?reate ?hec"box*roup to use :adioKutton
cbgD new ?hec"box*roup)+&
//?reate :adioKuttons
maleD new ?hec"box),Iale,,false,cbg+&
male.setKounds)1U-,1@-,M-,2M+&
femaleD new ?hec"box),Jemale,,false,cbg+&
female.setKounds)22-,1@-,N-,2M+&
//?reate a Bist
interestD new Bist)L,true+&
interest.setKounds)1U-,1U-,P-,U-+&
//add items to list
interest.add),?,+&
interest.add),?QQ,+&
interest.add),Java,+&
interest.add),?W,+&
//?reate a button
submitD new Kutton),'ubmit,+&
submit.setKounds)1M-,2U-,P-,2M+&
//Add the controls to the window
add)namel+&
add)name+&
add)dobl+&
add)date+&
add)month+&
add)year+&
add)genderl+&
add)male+&
add)female+&
add)intl+&
add)interest+&
add)submit+&
//:egister the source events to the 1ventBistener
name.addActionBistener)this+&
date.addItemBistener)this+&
month.addItemBistener)this+&
year.addItemBistener)this+&
male.addItemBistener)this+&
female.addItemBistener)this+&
interest.addActionBistener)this+&
submit.addActionBistener)this+&
.
public void actionAerformed)Action1vent ae+
(
strDae.getAction?ommand)+&
if)str.e>uals),'ubmit,++
(
repaint)+&
.
.
public void item'tate?hanged)Item1vent ie+
(
repaint)+&
.
public void paint)*raphics g+
(
int idxRS&
//$o display 7ame
g.draw'tring),7ame : ,Qname.get$ext)+,M-,@2-+&
//$o display <0K
msgD,<0K : ,&
msgQD date.get'electedItem)+Q, ,&
msgQD month.get'electedItem)+Q, ,&
msgQD year.get'electedItem)+Q, ,&
g.draw'tring)msg,M-,@L-+&
//$o display *ender
msgD,*ender : ,&
msgQDcbg.get'elected?hec"box)+.getBabel)+&
g.draw'tring)msg,M-,@N-+&
//$o display Areas of Interest
msgD,Areas of Interest : ,&
idxDinterest.get'electedIndexes)+&
for)int iD-&iCidx.length&iQQ+
msgQDinterest.getItem)idxRiS+Q, ,&
g.draw'tring)msg,M-,@P-+&
.
.
08$A8$:
:1'8B$:
$hus the program to design a Kiodata form was successfully
executed and the output was verified.

Вам также может понравиться