Coding Java1

29
JAV A 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 execu ted by a Javac ompa tible !eb browser. An appl et is actua lly a tiny Java pro gra m, dyn ami cal ly downlo ade d across the net wor ", #us t 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 'impleApp let extends A pplet (  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 !i ndow $ ool"i t )A! $+ classes. A pplets inte ract with the user through the A !$ , not through the cons ole  based I/0 classes. $he A !$ contains support for a windowbased, graphical interface. 2. $he second import st at ement import s the appl et pa c"age, whic h con tains the class Apple t. very applet tha t you create mus t 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.

Transcript of Coding Java1

Page 1: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 1/29

Page 2: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 2/29

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 ,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 themessage 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:

; xecuting 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. dit a Java source file.

2. ?ompile your program.

@. xecute the applet viewer, specifying the name of your applet9s sourcefile. $he applet viewer will encounter the AB$ tag within the comment

and execute your applet.

Page 3: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 3/29

$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 codeDApplet'"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

Page 4: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 4/29

Applet Initialization and Termination

It is important to understand the order in which the various methods shownin 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$B document isdisplayed 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$B

document containing

the appletHwhen it goes to another page, for example

Page 5: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 5/29

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 ModelIts 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 toreceive an event notification. $his provides an important benefit:

notifications are sent only to listeners that want to receive them

vents

In the delegation model, an event is an ob#ect that describes a state change in

a source.

vent 'ource

A source is an ob#ect that generates an event. 'ources may generate morethan one type of event. A source must register listeners in order for the

listeners to receive notifications

about a specific type of event. ach 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.

or example, to remove a "eyboard listener, you would call

remove=eyBistener) +

Page 6: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 6/29

Page 7: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 7/29

  when a choice selection is made or a chec"able menu

item is

selected or deselected.

=eyvent *enerated when input is received from the "eyboard.

ousevent *enerated when the mouse is dragged, moved,

clic"ed, pressed, or 

  released& also generated when the mouse enters or 

exits a

component.

ouse!heelvent *enerated when the mouse wheel is moved

$extvent *enerated when the value of a text area or text field is

changed.!indowvent *enerated when a window is activated, closed,

deactivated,

 deiconified, iconified, opened, or >uit.

Ex.No: 1(A) DEMONSTRATION OF AWT

CONTROLS

Page 8: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 8/29

Page 9: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 9/29

DEMONSTRATION OF AWT CONTROLS

PROGRAMS:

)i+$ext ields:

import #ava.awt.%&

import #ava.awt.event.%&

import #ava.applet.%&/% Capplet codeD$extield<emo widthDM-- heightD1M-E

  C/appletE

%/

 public class $extield<emo extends Applet implements ActionBistener 

(

$extield name,pass&

 public void init)+

(

Babel namepDnew Babel)7ame : ,Babel.I*4$+&

Babel passpDnew Babel)assword : ,Babel.I*4$+&

nameDnew $extield)12+&

 passDnew $extield)P+&

 pass.setcho?har)G%G+&

add)namep+&

add)name+&

add)passp+&

add)pass+&

name.addActionBistener)this+&

 pass.addActionBistener)this+&

 public void actionerformed)Actionvent ae+

(

repaint)+&

 public void paint)*raphics g+

Page 10: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 10/29

(

g.draw'tring)7ame : Qname.get$ext)+,N,N-+&

g.draw'tring)assword : Qpass.get$ext)+,N,P-+&

08$8$:

'8B$:

  $hus the program to demonstrate text field was successfully

executed and the output was verified.

)ii+ Kuttons:

Page 11: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 11/29

import #ava.awt.%&

import #ava.awt.event.%&

import #ava.applet.%&

/% Capplet codeDKutton<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 actionerformed)Actionvent ae+

(

'tring strDae.getAction?ommand)+&

if)str.e>uals)3es++

(

msgDressed 3es&

else

(

msgDressed 7o&

repaint)+&

 public void paint)*raphics g+(

g.draw'tring)msg,N,1--+&

Page 12: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 12/29

08$8$:

Page 13: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 13/29

'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 +&

os.add)!indows Oista+&

os.add)'olaris+&

os.add)ac 0'+&

 br.add)Internet xplorer+&

 br.add)ireox+&

 br.add)0pera+&

os.addItemBistener)this+& br.addItemBistener)this+&

 public void item'tate?hanged)Itemvent ie+

(

repaint)+&

Page 14: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 14/29

Page 15: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 15/29

)iv+ Bist:

import #ava.awt.%&

import #ava.awt.event.%&

import #ava.applet.%&

/% Capplet codeDBist<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 +&

os.add)!indows Oista+&

os.add)'olaris+&

os.add)ac 0'+&

 br.add)Internet xplorer+&

 br.add)irefox+&

 br.add)0pera+&

os.addActionBistener)this+&

 br.addActionBistener)this+&

 public void actionerformed)Actionvent ae+

(

repaint)+&

 public void paint)*raphics g+(

int indexRS,i&

msgD?urrent 0': &

indexDos.get'electedIndexes)+&

for)iD-&iCindex.length&iQQ+

(

Page 16: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 16/29

Page 17: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 17/29

'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 win,winOis,sol,mac&

 public void init)+

(

winDnew ?hec"box)!indows ,null,false+&

winOisDnew ?hec"box)!indows Oista,null,false+&

solDnew ?hec"box)'olaris,null,false+&

macDnew ?hec"box)ac 0',null,false+&

add)win+&

add)winOis+&

add)sol+&

add)mac+&

win.addItemBistener)this+&

winOis.addItemBistener)this+&sol.addItemBistener)this+&

mac.addItemBistener)this+&

 public void item'tate?hanged)Itemvent ie+

(

repaint)+&

Page 18: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 18/29

 public void paint)*raphics g+

(

g.draw'tring)!indows : Qwin.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)ac 0': Qmac.get'tate)+,N,1L-+&

08$8$:

Page 19: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 19/29

'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,ouseotionBistener (

'tring msgD &

'crollbar vertsb,horFsb&

 public void init)+

(

int widthDInteger.parseInt)getarameter)width++&

Page 20: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 20/29

int heightDInteger.parseInt)getarameter)height++&

vertsbDnew 'crollbar)'crollbar.O$I?AB,-,1,-,height+&

horFsbDnew 'crollbar)'crollbar.40IT07$AB,-,1,-,width+&

add)vertsb+&

add)horFsb+&

vertsb.addAd#ustmentBistener)this+&

horFsb.addAd#ustmentBistener)this+&

addouseotionBistener)this+&

 public void ad#ustmentOalue?hanged)Ad#ustmentvent ae+

(

repaint)+&

 public void mouse<ragged)ousevent me+

(int xDme.get)+&

int yDme.get3)+&

vertsb.setOalue)y+&

horFsb.setOalue)x+&

repaint)+&

 public void mouseoved)ousevent me+ (

 public void paint)*raphics g+

(

msgDOertical: Qvertsb.getOalue)+&

msgQD , 4oriFontal: QhorFsb.getOalue)+&

g.draw'tring)msg,N,12-+&

g.draw'tring)%,horFsb.getOalue)+,vertsb.getOalue)++&

08$8$:

Page 21: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 21/29

'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 rocessor IO

Page 22: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 22/29

12Pmb A

'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 $extield and $extArea A!$ control , select a suitable layoutmanager to place and demonstrate it9s use by editing the $extield.

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 codeDKiodata widthDM-- heightDM--E  C/appletE

%/

 public class Kiodata extends Applet implements ActionBistener,ItemBistener 

(

'tring msgD &

Page 23: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 23/29

Babel namel,dobl,genderl,intl&

$extield 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.B$+&

namel.setKounds)M-,M-,M-,2M+&doblD new Babel)<0K : ,Babel.B$+&

dobl.setKounds)M-,V-,M-,2M+&

genderlD new Babel)*ender : ,Babel.B$+&

genderl.setKounds)M-,1@-,N-,2M+&

intlD new Babel)Areas of Interest : ,Babel.B$+&

intl.setKounds)M-,1U-,1--,2M+&

//?reate $extield

nameD new $extield)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+&

Page 24: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 24/29

year.add)1VV-+&

month.add)Jan+&

month.add)eb+&

month.add)ar+&

month.add)Apr+&

month.add)ay+&

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+&

Page 25: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 25/29

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)ale,false,cbg+&

male.setKounds)1U-,1@-,M-,2M+&

femaleD new ?hec"box)emale,false,cbg+&

female.setKounds)22-,1@-,N-,2M+&

//?reate a BistinterestD 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+&

Page 26: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 26/29

add)submit+&

//egister the source events to the ventBistener 

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 actionerformed)Actionvent ae+(

strDae.getAction?ommand)+&

if)str.e>uals)'ubmit++

(

repaint)+&

 public void item'tate?hanged)Itemvent 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 &

Page 27: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 27/29

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

msgDAreas of Interest : &

idxDinterest.get'electedIndexes)+&

for)int iD-&iCidx.length&iQQ+

msgQDinterest.getItem)idxRiS+Q &

g.draw'tring)msg,M-,@P-+&

Page 28: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 28/29

08$8$:

Page 29: Coding Java1

8/12/2019 Coding Java1

http://slidepdf.com/reader/full/coding-java1 29/29