Sei sulla pagina 1di 126

1oplcs

WAW1 Classes
W Wlndow lundamenLals
WWorklng wlLh lrame Wlndows
W CreaLlng a lrame Wlndow ln an AppleL
WCreaLlng a Wlndowed rogram
Wulsplaylng lnformaLlon wlLhln a Wlndow
W Worklng wlLh Craphlcs
W Worklng wlLh color
WSeLLlng Lhe alnL Mode
WWorklng wlLh lonLs
WManaglng 1exL CuLpuL uslng lonL MeLrlcs
AW1 C|asses
one of !ava's largesL packages
W AW1 AbsLracL Wlndow 1oolklL
W 1he maln purpose of Lhe AW1 ls Lo supporL appleL
wlndows
W lL can also be used Lo creaLe sLandalone wlndows
LhaL run ln a Cul envlronmenL such as Wlndows
W1he AW1 classes are conLalned ln Lhe jovoowt package
AW1 Classes
AW1 Classes some of Lhe orlglnal meLhods were deprecaLed
and replaced by new ones when !ava 11 was released lor backward
compaLlblllLy !ava 2 sLlll supporLs all Lhe orlglnal 10 meLhods
AW1 Classes
AW1 Classes
AW1 Classes
AW1 Classes
W|ndow
Iundamenta|s
Wlndow lundamenLals
W 1he AW1 deflnes wlndows accordlng Lo a class hlerarchy
LhaL adds funcLlonallLy and speclflclLy wlLh each level
W1he Lwo mosL common wlndows are
W Lhose derlved from looel whlch ls used by appleLs
W Lhose derlved from ome whlch creaLes a sLandard
wlndow
Wlndow lundamenLals
Mocb of tbe fooctlooollty of tbese wloJows ls JelveJ fom tbel
oeot closses
ComponenL
Wo obstoct closs tbot
WencapsulaLes all of Lhe aLLrlbuLes of a vlsual componenL(All user
lnLerface elemenLs are subclasses)
WlL deflnes over a hundred publlc meLhods for managlng evenLs
(such as mouse and keyboard lnpuL poslLlonlng and slzlng Lhe wlndow and
repalnLlng)
W8emembers Lhe currenL foreground and background colors and
Lhe currenLly selecLed LexL fonL
ConLalner
W sobcloss of comooeot
W cootoloe ls esooslble fo loyloq oot ooy comooeots
tbot lt cootolos
Wlncludes meLhods LhaL allow oLher ComponenL ob[ecLs Lo
be nesLed wlLhln lL
WCLher ConLalner ob[ecLs can be sLored lnslde of a
ConLalner
anel
W looel ls o wloJow tbot Joes oot cootolo o tltle bo
meoo bo o boJe
WA concreLe subclass of Conta|ner
W1he superclass for App|et
Wt Joesot oJJ ooy oew metboJs lt slmly lmlemeots
cootoloe
anel
WCLher componenLs can be added Lo a 9ane| ob[ect by |ts
add( ) method
W add( ) meLhod lnherlLed from ConLalner
W you can poslLlon and reslze Lhe added componenLs
manually uslng Lhe setLocat|on( ) setS|ze( ) or set8ounds( )
meLhods deflned by ComponenL
Wlndow
W1hls class creaLes a Loplevel wlndow
W A Loplevel wlndow ls noL conLalned wlLhln any oLher
ob[ecL (lL slLs dlrecLly on Lhe deskLop)
W ?ou wlll use a subclass of W|ndow called Irame Lo creaLe
Wlndow ob[ecLs
lrame
WA subclass of W|ndow and has a t|t|e bar menu bar
borders and res|z|ng corners
W lf you creaLe a an appleL wlndow lL wlll conLaln a
warnlng message such as !ava AppleL Wlndow"
W When a lrame wlndow ls creaLed by a program raLher
Lhan an appleL a normal wlndow ls creaLed
Work|ng w|th
Irame W|ndows
W lL creaLes a sLandardsLyle wlndow
W?ou wlll use lL Lo creaLe
Wchlld wlndows wlLhln appleLs
W Loplevel or chlld wlndows for appllcaLlons
Work|ng w|th Irame W|ndows
1wo of Irame's constructors
1 lrame( ) creaLes a sLandard wlndow LhaL does noL conLaln a
LlLle
2 lrame(SLrlng tltle) creaLes a wlndow wlLh Lhe LlLle speclfled by
tltle
Several meLhods are used when worklng wlLh Irame
w|ndows
W Sett|ng the W|ndow's D|mens|ons
1. void solSizo(inl nculid|n, in| ncuHcign|)
2. void solSizo(Dinonsion ncuSizc)
nculid|n and ncuHcign| oew slze of tbe wloJow
ncuSizc tbe ulmeosloo object cootolos wlJtb ooJ belqbt
flelJs
1be Jlmeosloos oe seclfleJ lo tems of lxels
. Dinonsion golSizo( ) used Lo obLaln Lhe currenL slze of a
wlndow
W Sett|ng a W|ndow's 1|t|e
void solTilIo(Sliing ncuTi||c)
ncuTi||c tbe oew tltle fo tbe wloJow
W C|os|ng a Irame W|ndow
solVisilIo(faIso).
Wyou musL lmplemenL Lhe w|ndowC|os|ng( ) method of the W|ndowL|stener
lnLerface
Wlnslde w|ndowC|os|ng( ) you must remove the w|ndow from the screen
Creat|ng a Irame
W|ndow |n an
App|et
S1L9S
1 CreaLe a subclass of lrame
2 Cverrlde any of Lhe sLandard wlndow meLhods
(InIt( ), start( ), stnp( ), and paInt( ))
llnally lmplemenL Lhe w|ndowC|os|ng( ) meLhod of
Lhe W|ndowL|stener lnLerface
( calllng setV|s|b|e(fa|se) when Lhe wlndow ls closed )
you creaLe an ob[ecL of frame's subclass 1hls causes a frame wlndow Lo come
lnLo exlsLence and you make wlndow vlslble by calllng setV|s|b|e( )
-oLe Lhe followlng
W Samp|eIrame ca||s Irame's constructor (w|th super()) 1hls
causes a sLandard frame wlndow Lo be creaLed wlLh Lhe LlLle
passed ln LlLle
W1hls example overrldes Lhe appleL wlndow's sLarL( ) and sLop( )
meLhods 1hls causes Lhe wlndow Lo be removed auLomaLlcally
when you LermlnaLe Lhe appleL when you close Lhe wlndow
Sample ouLpuL
Adapter C|asses (Not |n the sy||abus )
W An adapLer class provldes an empLy lmplemenLaLlon of all
meLhods ln an evenL llsLener lnLerface
W AdapLer classes are useful when you wanL Lo recelve and process
only some of Lhe evenLs LhaL are handled by a parLlcular evenL
llsLener lnLerface
W ?ou can deflne a new class Lo acL as an evenL llsLener by
exLendlng one of Lhe adapLer classes and lmplemenLlng only
Lhose evenLs ln whlch you are lnLeresLed
Adapter C|asses (Not |n the sy||abus )
Adapter C|asses (Not |n the sy||abus )
Adapter C|asses (Not |n the sy||abus )
Adapter C|asses (Not |n the sy||abus )
and||ng Lvents |n a Irame W|ndow
WSlnce Irame ls a subclass of Component lL lnherlLs all Lhe
capablllLles deflned by ComponenL
W So you can use and manage a frame wlndow LhaL you creaLe [usL
llke you manage your appleL's maln wlndow
W Whenever an evenL occurs ln a wlndow Lhe evenL handlers
deflned by LhaL wlndow wlll be called
W Lach wlndow handles lLs own evenLs
and||ng Lvents |n a Irame W|ndow
1he MouseLlsLener lnLerface
1 vold mouseCllcked(MouseLvenL me)
2 vold mouseLnLered(MouseLvenL me)
vold mouseLxlLed(MouseLvenL me)
4 vold mouseressed(MouseLvenL me)
3 vold mouse8eleased(MouseLvenL me)
1he MouseMoLlonLlsLener lnLerface
1 vold mouseuragged(MouseLvenL me)
2 vold mouseMoved(MouseLvenL me)
and||ng Lvents |n a Irame W|ndow
1he WlndowLlsLener lnLerface
1 vold wlndowAcLlvaLed(WlndowLvenL we)
2 vold wlndowClosed(WlndowLvenL we)
vold wlndowCloslng(WlndowLvenL we)
4 vold wlndowueacLlvaLed(WlndowLvenL we)
3 vold wlndowuelconlfled(WlndowLvenL we)
6 vold wlndowlconlfled(WlndowLvenL we)
7 vold wlndowCpened(WlndowLvenL we)
W1he followlng program creaLes a wlndow LhaL responds Lo mouse
evenLs 1he maln appleL wlndow also responds Lo mouse evenLs
Sample ouLpuL
Creat|ng a W|ndowed
9rogram
lL ls posslble Lo creaLe sLandalone AW1based appllcaLlons Loo 1o
do Lhls slmply creaLe an lnsLance of Lhe wlndow or wlndows you
need lnslde ma|n( )
Sample ouLpuL
Cnce creaLed a frame wlndow Lakes on a llfe of lLs own -oLlce LhaL ma|n( ) ends
wlLh Lhe call Lo appw|nsetV|s|b|e(true)
Work|ng w|th
Graph|cs
Work|ng w|th Graph|cs
W All graphlcs are drawn relaLlve Lo a wlndow
(maln wlndow of an appleL a chlld wlndow of an appleL or a sLandalone
appllcaLlon wlndow)
W 1he orlgln of each wlndow ls aL Lhe LoplefL corner and ls 00
W CoordlnaLes are speclfled ln plxels
W All ouLpuL Lo a wlndow Lakes place throuqh o qrophics context
Work|ng w|th Graph|cs
W A graphlcs conLexL ls encapsulaLed by Lhe Graph|cs c|ass and ls
obLalned ln Lwo ways
WlL ls passed Lo an appleL when one of lLs varlous meLhods such
as pa|nt( ) or update( ) |s ca||ed
WlL ls reLurned by Lhe getGraph|cs( ) method of Component
W1be rophics c/oss defines o number of drowinq functions
Draw|ng L|nes
;4id drauLlnein| s|ar|X, in| s|ar|Y, in| cndX, in| cndY)
dlsplays a llne ln Lhe currenL drawlng color LhaL beglns aL
stot\stot ooJ ends aL eoJ\eoJ
Draw|ng L|nes
urawlng 8ecLangles
;4id drauRectin| |45, in| |cf|, in| uid|n, in| ncign|)
2. told fl||Rectin| |45, in| |cf|, in| uid|n, in| ncign|)
|45 ,|cf| 1he upperlefL corner of Lhe recLangle
uid|n and ncign| 1he dlmenslons of Lhe recLangle
;4id drauRoundRectin| |45, in| |cf|, in| uid|n, in| ncign|,
in| xDian, in| qDian) Jows oooJeJ ectooqle
xDian 1he dlameLer of Lhe roundlng arc along Lhe x axls
qDian 1be Jlomete of Lhe roundlng arc along Lhe ? axls
urawlng 8ecLangles
;4id fl||RoundRec|in| |45, in| |cf|, in| uid|n, in| ncign|, in|
xDian, in| qDian) Jows o fllleJ ectooqle
urawlng 8ecLangles
Sample ouLpuL
Sample ouLpuL
urawlng Llllpses and Clrcles
;4id drauOta|in| |45, in| |cf|, in| uid|n, in| ncign|)
2 ;4id fl||Ota|in| |45, in| |cf|, in| uid|n, in| ncign|)
1be elllse ls Jowo wltblo o boooJloq ectooqle whose upperlefL
corner ls speclfled by |45, |cf| and whose wldLh and helghL are
speclfled by uid|n and ncign|
1o draw a clrcle speclfy a square as Lhe boundlng recLangle
urawlng Llllpses and Clrcles
urawlng Llllpses and Clrcles
Sample ouLpuL
urawlng Arcs
;4id drauArcin| |45, in| |cf|, in| uid|n, in| ncign|, in|
s|ar|Ang|c, in| succ5Ang|c)
2 ;4id fl||Arcin| |45, in| |cf|, in| uid|n, in| ncign|, in|
s|ar|Ang|c, in| succ5Ang|c)
|45,|cf| upperlefL corner of Lhe boundlng recLangle
uid|n and ncign| wlJtb ooJ belqbt of Lhe boundlng recLangle
s|ar|Ang|c , succ5Ang|c 1he arc ls drawn from stotoqle tbooqb
tbe ooqolo Jlstooce seclfleJ by sweeoqle
urawlng Arcs
W 1he arc ls drawn counLerclockwlse lf succ5Ang|c ls osltlve
ooJ clockwlse lf succ5Ang|e ls negaLlve
W Angles are speclfled ln degrees
WZero degrees ls on Lhe horlzonLal aL Lhe Lhree o'clock poslLlon
1o draw an arc from Lwelve o'clock Lo slx o'clock Lhe sLarL angle
would be 90 and Lhe sweep angle 180
urawlng Arcs
urawlng Arcs
Sample ouLpuL
urawlng olygons
;4id drauPo|gonin| xj ], in| qj ], in| nunP4in|s)
2 ;4id fl||Po|gonin| xj ], in| qj ], in| nunP4in|s)
W1he polygon's endpolnLs are speclfled by Lhe coordlnaLe palrs
conLalned wlLhln Lhe x ooJ y ooys
WnunP4in|s 1he number of polnLs deflned by x ooJ y
urawlng olygons
urawlng olygons
Sample ouLpuL
Slzlng Craphlcs
W 1o slze a graphlcs ob[ecL flrsL obLaln Lhe currenL dlmenslons of
Lhe wlndow by calllng getS|ze( ) on Lhe wlndow ob[ecL
WCnce you have Lhe currenL slze of Lhe wlndow you can scale your
graphlcal ouLpuL accordlngly
WgetS|ze( ) reLurns Lhe dlmenslons of Lhe wlndow encapsulaLed
wlLhln a D|mens|on ob[ect
Slzlng Craphlcs
Slzlng Craphlcs
Slzlng Craphlcs
Work|ng w|th
Co|or
Work|ng w|th Co|or
W !ava supporLs color ln a porLable devlcelndependenL fashlon
W Color ls encapsulaLed by Lhe Co|or c|ass and def|nes constants for common
co|ors
. Co|orin| rcd, in| grccn, in| o|uc)
2. Co|orin| rgoVa|uc)
3. Co|orf|4a| rcd, f|4a| grccn, f|4a| o|uc)
W ou can a|so create your own co|ors us|ng one of the co|or constructors
Work|ng w|th Co|or
1 1he flrsL consLrucLor Lakes Lhree lnLegers LhaL speclfy Lhe color
as a mlx of red green and blue 1hese values musL be beLween
0 and 233
2 1he second color consLrucLor Lakes a slngle lnLeger LhaL
conLalns Lhe mlx of red green and blue packed lnLo an lnLeger
Wred ln blLs 16 Lo 2
W green ln blLs 8 Lo 13 and
Wblue ln blLs 0 Lo 7
1he flnal consLrucLor Lakes Lhree floaL values (beLween 00 and
10) LhaL speclfy Lhe relaLlve mlx of red green and blue
Ds|ng ue Saturat|on and 8r|ghtness (S8)
W An alLernaLlve color model Lo redgreenblue (8C8)
W hue speclfled wlLh a number beLween 00 and 10
W Saturat|on ranges from 00 Lo 10 represenLlng llghL pasLels
Lo lnLense hues
W 8rlghLness values also range from 00 Lo 10 where 1 ls brlghL
whlLe and 0 ls black
Co|or Methods
1he Color class deflnes several meLhods LhaL help manlpulaLe
colors
1. slalic inl HSloRC(fIoal nuc, f|4a| sa|ura|i4n, f|4a| orign|ncss)
2. slalic fIoal| j RCloHS(inl rcd, in| grccn, in| o|uc, f|4a| ;a|ucsj ])
. inl golRod( ) 8ed componenLs of 88C
. inl golCioon( ) Creen componenLs of 88C
. inl golIuo( ) 8lue componenLs of 88C
. inl golRC( ) packod RC iopiosonlalion of a coIoi
Sett|ng the Current Graph|cs Co|or
?ou can change Lhls color by calllng Lhe Graph|cs meLhod setCo|or( )
;4id 8etCo|or4|4r ncu4|4r)
ncu4|4r speclfles Lhe new drawlng color
W?ou can obLaln Lhe currenL color by calllng getCo|or( )
Color geLColor( )
A Color uemonsLraLlon AppleL
A Color uemonsLraLlon
AppleL
Sett|ng the
kCk Mode
Sett|ng the kCk Mode
W 8y defaulL new ouLpuL Lo a wlndow overwrlLes any preexlsLlng
conLenLs
W Powever lL ls posslble Lo have new ob[ecLs xC8ed onLo Lhe
wlndow by uslng seLxC8Mode( )
;4id sc|XORM4dc4|4r x4r4|4r)
x4r4|4r speclfles Lhe color LhaL wlll be xC8ed Lo Lhe wlndow
when an ob[ecL ls drawn
Sett|ng the 9a|nt Mode
W 1o reLurn Lo overwrlLe mode call seLalnLMode( ) shown here
;4id 8etPalntMode )
1be oJvootoqe of \Ok moJe ls tbot tbe oew object ls olwoys
qooooteeJ to be vlslble oo motte wbot colo tbe object ls Jowo
ove
Lhe followlng program dlsplays cross halrs LhaL Lrack Lhe mouse
polnLer 1he cross halrs are xC8ed onLo Lhe wlndow and are always
vlslble no maLLer whaL Lhe underlylng color ls
Sample ouLpuL
Work|ng w|th Ionts
Work|ng w|th Ionts
W 1he AW1 provldes
W flexlble fonLmanlpulaLlon
W Allows dynamlc selecLlon of fonLs
W Ionts have
4 fomi/y nome Lhe general name of Lhe fonL such as
Courler
4 /oqico/ font nome speclfles a caLegory of fonL such as
Monospaced
4 foce nome a speclflc fonL such as Courler lLallc
Work|ng w|th Ionts
W lonLs are encapsulaLed by Lhe Iont class
Work|ng w|th Ionts
W Some MeLhods ueflned by lonL
Work|ng w|th Ionts
W Some MeLhods ueflned by lonL
Determ|n|ng the Ava||ab|e Ionts
1 1o obLaln fonL lnformaLlon you can use Lhe
getAtal|ab|e|ont|aml|Name8 meLhod deflned by Lhe
rahlc8ntlronmenL class
Sliing| j gc|A;ai|ao|c|4n||ani|qNancs )
1bls metboJ etoos oo ooy of stloqs tbot cootolos tbe oomes
of tbe ovolloble foot fomllles
ueLermlnlng Lhe Avallable lonLs
2 ln addlLlon Lhe gc|A|||4n|s ) meLhod ls deflned by Lhe
Graph|csLnv|ronment class
lonL gc|A|||4n|s )
1bls metboJ etoos oo ooy of ont objects for o// of the
ovoi/ob/e fonts
Slnce Lhese meLhods are members of Graph|csLnv|ronment you
need a Graph|csLnv|ronment reference to ca|| them ?ou can
obLaln Lhls reference by uslng
slalic ra5nics|n;ir4nncn| gc||4ca|ra5nics|n;ir4nncn| )
ueLermlnlng Lhe Avallable lonLs
ueLermlnlng Lhe Avallable lonLs
Sample ouLpuL
CreaLlng and SelecLlng a lonL
1o selecL a new fonL you musL flrsL consLrucL a Iont ob[ect LhaL
descrlbes LhaL fonL Cne lonL consLrucLor has Lhls general form
|ontS|ring f4n|Nanc, in| f4n|S|q|c, in| 54in|Sizc)
W f4n|Nanc speclfles Lhe name of Lhe deslred fonL
W f4n|S|q|c 1he sLyle of Lhe fonL
W 54in|Sizc 1he slze ln polnLs of Lhe fonL
CreaLlng and SelecLlng a lonL
W f4n|Nanc
All !ava envlronmenLs wlll supporL Lhe followlng fonLs
io/oq io/oqlnput 5ons5erif 5erif Monospoced ond
5ymbo/
W f4n|S|q|c
one or more of Lhese Lhree consLanLs
W ontPL4lN
W ont8OL
W ontl14Llc
1o comblne sLyles C8 Lhem LogeLher
lor example ont8OL ] ontl14Llc
CreaLlng and SelecLlng a lonL
W 1o use a fonL LhaL you have creaLed you musL selecL lL uslng
seLlonL( ) whlch ls deflned by Component
;4id 8et|ont|4n| f4n|Ooj)
f4n|Ooj tbe object tbot cootolos tbe JesleJ foot
CreaLlng and SelecLlng a lonL
1he followlng program ouLpuLs a sample of each sLandard fonL
Lach Llme you cllck Lhe mouse wlLhln lLs wlndow a new fonL ls
selecLed and lLs name ls dlsplayed
CreaLlng and SelecLlng a lonL
CreaLlng and SelecLlng a lonL
CreaLlng and SelecLlng a lonL
CreaLlng and SelecLlng a lonL
CbLalnlng lonL lnformaLlon
W 1o obLaln lnformaLlon abouL Lhe currenLly selecLed fonL you
musL flrsL geL Lhe currenL fonL by calllng gc||4n| )
1hls meLhod ls deflned by Lhe Graph|cs c|ass
|4n| get|ont
Cnce you have obLalned Lhe currenLly selecLed fonL you can
reLrleve lnformaLlon abouL lL uslng varlous meLhods deflned by
Iont
CbLalnlng lonL lnformaLlon
WLhls appleL dlsplays Lhe name famlly slze and sLyle of Lhe
currenLly selecLed fonL
CbLalnlng lonL lnformaLlon
Manag|ng 1ext Cutput
Ds|ng IontMetr|cs
IontMetr|cs c|ass
W LncapsulaLes varlous lnformaLlon abouL a fonL
8as|c 1erms
Jhen drow5trinq{ ) method is used the /ocotion specifies left
eJqe of tbe boselloe of tbe cbooctes oot ot tbe oeleft cooe
os ls osool wltb otbe Jowloq metboJs
IontMetr|cs c|ass
W IontMetr|cs def|nes severa| methods that he|p you manage text
output
IontMetr|cs c|ass
W IontMetr|cs def|nes severa| methods that he|p you manage text
output
IontMetr|cs c|ass
W IontMetr|cs def|nes severa| methods that he|p you manage text
output
ulsplaylng MulLlple Llnes of 1exL Ds|ng IontMetr|cs
ulsplaylng MulLlple Llnes of 1exL Ds|ng IontMetr|cs
ulsplaylng MulLlple Llnes of 1exL Ds|ng IontMetr|cs
ulsplaylng MulLlple Llnes of 1exL Ds|ng IontMetr|cs
Center|ng 1ext
Center|ng 1ext
Center|ng 1ext
Center|ng 1ext

Potrebbero piacerti anche