Professional Documents
Culture Documents
JavaFX GUI Basics
JavaFX GUI Basics
Topics
&uic' JavaFX GUI (vervie) Using *eclarative s+nta$ for creating GUI Step ,+ step process for creating a simple GUI JavaFX Application structure - Stage. Scene Scene Graph /ustom node Graphical o,"ects 0ffects Interactions
3
"olors
/olor patterns for all constants of the javafx.scene.paint.Color class
Te&t
*ifferent formatting st+les applied to the same te$t string
! ects
0ffects can ,e applied to the JavaFX UI elements
Trans ormation
1ransformations can ,e performed for the graphics. images. or te$t
10
'a%out
1here are several methods of la+ing out UI elements using the javafx.scene.layout pac'age#
11
"ursors
*ifferent vie)s of cursor +ou can appl+ to an+ UI element
12
14
16
18
19
21
3/ "reate a 4ectangle
Stage . ... scene- Scene . content- 2 // !he x and y instance varia les specify the pixel location of the // rectangle# arc4idth and arc5eight define the roundness of // corners# and the fill varia le defines the color that fills the rectangle. Rectangle . x- %' y- 6' width- ('7 height- ('7 arc4idth- (' arc5eight- (' fill- Color.8R,,1 0 3 0 22
5/ "reate a "ircle
Stage { ... scene: Scene { content: [ Rectangle { ... }, // Because the rectangle is declared before any other objects, it is painted // first. The rectangle ill be behind any other objects painted later. !ircle{ center": ##$ center%: ##& radius: $' fill: !olor.()*T+ stro,e: !olor.R+} . }
23
#tage
1op-level container for a scene
> /ontains onl+ one Scene
:otentiall+ represented ,+
> JFrame on des'top > JApplet on )e, page > S;G pla+er on mo,ile
*efines the characteristics li'e title. )idth. height. location. handling e$it. etc
26
!&le7 #tage
Stage . title- *4elcome to 9ava:;/* scene- Scene . content- 2 !ext . content- *5ello 4orld/* x-$' y-$' fill- Color.<=>C? font- :ont. si)e- 6$ 0 0 3 0 0
27
class definition
run@A function
> Will ,e e$ecuted as thread
Main.fx
Scene { ... } function run(): Void { ...
Displayed
Executed as a thread 28
#cene
/anvas upon )hich Scene Graph is displa+ed /an set multiple /SS st+lesheets /an set ,ac'ground color 8or set to null9 /an set canvas )idth!height
29
#cene Grap$
has no parent# > 0ach node is either a leaf node or a ,ranch# > A leaf node has no children# > A ,ranch node has =ero or more children#
31
JavaFX Arc$itecture
Java&' $cript $" tware
!r"#ect $ce%e Graph E ects Java 2D
Graphics hardware
32
shape > 0ffect < t+pe of effect. eg# ,lurring. shado)ing. to appl+ > 0vents < mouse. 'e+,oard > (pacit+ < setting the translucenc+
33
34
35
x : 1 5 y: 1 5
Text
Circle
36
"ustom 0ode
"ustom 0ode
:rimar+ means of Scene Graph encapsulation
> All other nodes are not e$tenda,le
38
#$apes
#$apes
Arc /ircle 0llipse @ine :ath :ol+gon 5ectangle stro'e stro'eWidth fill smooth
40
Geometric #$apes
Arc. ellipse. line. pol+gon. circle. rectangle ;er+ similar to te$t
Circle { centerX: 70, centerY: 70 radius: 50 fill: Color.PINK stroke: Color.RED strokeWidth: 3 strokeDashArray: [ 7 ] strokeDashOffset: 2 }
41
"ustom #$apes
1)o )a+s of defining custom shapes
> /om,ining e$isting shapes > *ra)ing a totall+ ne) shape
etc#
!&le : #$apeIntersect
ShapeIntersect { transforms: [ Translate { x: 170 } ] fill: Color.LIGHTGREEN stroke: Color.GREEN strokeWidth: 3 //Union of the 2 shapes a: [rectangle diamond ] }
43
!&le : -at$
Path { fill: Color.LIGHTGRAY stroke: Color.GRAY strokeWidth: 3 elements: [ MoveTo { x: 15 y: 15 }, ArcTo { x: 50 y: 10 radiusX: 20 radiusY: 20 sweepFlag: true}, ArcTo { x: 70 y: 20 radiusX: 20 radiusY: 20 sweepFlag: true}, ArcTo { x: 50 y: 60 radiusX: 20 radiusY: 20 sweepFlag: true}, ArcTo { x: 20 y: 50 radiusX: 10 radiusY: 5 sweepFlag: false}, ArcTo { x: 15 y: 15 radiusX: 10 radiusY: 10 sweepFlag: true}, ] }
44
Te&t
$. +. 1e$t(rigin B+ default. te$t positioned such that 8$. +9 is left ,aseline Fonts can ,e specified on 1e$t Favor fill over stro'e Supports multiple te$t Use Alignment to align multi-line te$t 1o center te$t. compute the center via la+out ,ounds
46
Te&t
*efines a node for displa+ing te$t
Text { effect: DropShadow { offsetX: -10 offsetY: -10 } font: Font { name: DirtyBakersDozen size: 50 } fill: Color.ROYALBLUE stroke: Color.BLUE, strokeWidth: 3 x: 15, y: 80 content: "Hello World" }
47
Image
"mageBiew node sho)s images Image represents an in-memor+ image Image can load images in foreground thread or ,ac'ground thread Both Image;ie) and Image can scale
> :reserve ratio > Fit )ithin a specific )idth!height > When fit on Image level. 'eeps smaller image in memor+
48
Images
ImageView = ImageView { clip: Rectangle { y: 30 x: 50 width: 350 height: 100 } image: Image { url: "..."} }
49
Te&tBo&
Used for te$t input Use /SS to st+le the 1e$tBo$ te$t is changed to reflect the actual te$t in the 1e$tBo$ value is changed )hen the te$t is committed via 0>105. 1AB. etc action function is invo'ed )hen 0>105 is pressed columns specifies the preferred )idth ,ased on the font si=e and num,er of characters to displa+
50
! ects
53
! ects7 Drop#$adow
> default /olor#B@A/D offsetX 1he shado) offset in the $ direction. in pi$els# > default E#E offsetF 1he shado) offset in the + direction. in pi$els# > default E#E radius 1he radius of the shado) ,lur 'ernel# > default %E#E. ma$ 73#E spread 1he spread of the shado)# > default E#E. ma$ %#E. min E#E
55
!&le7 Drop#$adow
!ext . effect- +ropShadow . offsetC- 6 color- Color.color@7.%# 7.%# 7.%A 0; ... 0# Circle . effect- +ropShadow . offsetC- % 0# ... 0
56
!&le7 Drop#$adow
!ext . effect- +ropShadow . offsetC- 6 color- Color.8R,,1 radius- $7.7 0; ... 0# Circle . effect- +ropShadow . offset;- (7 offsetC- $7 color- Color.<=D, radius- 67.7 0
57
! ects7 'ig$ting
'ig$ting ! ect
effect: Lighting{ surfaceScale: 7 light: DistantLight{ azimuth: 90 elevation: 30 } }
effect: Lighting{ surfaceScale: 7 light: SpotLight { x: 0 y :0 z: 50 pointsAtX: 10 pointsAtY: 10 pointsAtZ: 0 color: Color.YELLOW } }
60
62
'inear Gradients
startX. startF. endX. endF
> *efine the direction of the gradient > (n the unit sGuare
64
+ri,i%al i-a,e
66
Interactions
;andling !vents
All nodes have either mouse or 'e+,oard events
> (verride the appropriate method
5eleased. WheelAoved
69
70
71