Professional Documents
Culture Documents
CONTENTS INCLUDE:
n
About JavaFX
JFXPoetry, a simple example
JavaFX
n
By Stephen Chin
instantiation, and allow inline initialization of variables where
About JavaFX the value follows the colon “:”. This is used to instantiate an
ImageView with an Image inside that loads its content from the
JavaFX is an exciting new platform for building Rich Internet given URL. To ensure the image resizes with the window, we
Applications with graphics, animation, and media. It is built set preserveRatio to true and bind the Image. Binding is a very
on Java technology, so it is interoperable with existing Java powerful facility in JavaFX that makes it easy to update values
libraries, and is designed to be portable across different without heavyweight event handlers. Compiling and running
embedded devices including mobile phones and set-top this application will display a picture of a misty morning in
boxes. This Refcard will help you get started programming with Burns Lake, BC, Canada taken by Duane Conlon as shown in
JavaFX Script and also serve as a convenient reference once Figure 1.1 2
you have mastered the language.
}
JavaFX
}
]
}
} Subscribe Now for FREE!
Notice that that JavaFX syntax makes it simple to express Refcardz.com
nested UI structures. The curly braces “{}” are used for object
GaussianBlur Blurs the Node with a configurable radius Interacting With Controls
Glow Makes the Node appear to glow with a given intensity level The JavaFX 1.2 release features a new library of skinnable
Identity Passes an image through to a chained effect controls written in pure JavaFX. Table 3 lists some of the new
InnerShadow Draws a shadow on the inner edges of the Node controls and what they can be used for.
InvertMask Returns a mask that is the inverse of the input Table 3. Controls Available in JavaFX 1.2
Lighting Simulates a light source to give Nodes a 3D effect
MotionBlur Blurs the image at a given angle to create a motion effect Control Description
PerspectiveTransform Maps a Node to an arbitrary quadrilateral for a perspective effect Button Button that can contain graphics and text
Reflection Displays an inverted view of the Node to create a reflected effect CheckBox Selectable box that can be checked, unchecked, or undefined
SepiaTone Creates a sepia tone effect to mimic aged photographs Hyperlink HTML-like clickable text link
Shadow Similar to a DropShadow, but without the overlaid image Label Text that can be associated with anther control
Ths bind operator is used to both hide the button while VBox Lays out its contents in a single, vertical column
the animation is playing and also center the button in the ClipView Clips its content Node to the bounds, optionally allowing panning
window. Initially the button is invisible, but we added a new Flow Lays out its contents either vertically or horizontally with wrapping
SequentialTransition that plays a FadeTransition to show the Stack Layers its contents on top of each other from back to front
button after the translation is complete. Clicking the button Tile Arranges its contents in a grid of evenly sized tiles
shown in Figure 4 will hide it and play the animation from the
beginning. Finishing with Media
JavaFX has built-in media classes that make it very simple to
play audio or video either from the local files or streaming
off the network. To complete the example we will add in a
public domain clip of Indigo Bunting birds chirping in the
background. Adding in the audio is as simple as appending
a MediaPlayer with autoPlay set to true that contains a Media
object pointing to the URL.
MediaPlayer {
autoPlay: true
media: Media {
source: “http://video.fws.gov/sounds/35indigobunting.mp3”
}
}
Figure 4: Button Control to Play the Animation Again In this example we are using an mp3 file, which is supported
across platforms by JavaFX. Table 5 lists some of the common
Panning With Layouts media formats supported by JavaFX, including all the cross-
JavaFX 1.2 comes with several new layouts that make it easy platform formats.
to design complex UIs. One of these is the ClipView, which we
will use to support dragging of the poem text. ClipView takes a Table 5. Common Media Formats Supported by JavaFX
single Node as the input and allows the content to be panned Type Platform Format File Extension
using the mouse: Audio Cross-platform MPEG-1 Audio Layer 3 mp3
http://jfxtras.org/samples/jfxpoetry/JFXPoetry.jnlp ...
fromY: widget.height
The full source code for this application is available on the ...
widget;
JFXtras Samples website: http://jfxtras.org/portal/samples
Running on Mobile The updates to the code include the following three changes:
To run the sample in the Mobile Emulator all you have to do • Wrap your application in a Widget class. The Widget class extends
javafx.scene.layout.Panel, which makes it easy to extend.
is pass in the MOBILE profile to the javafxpackager program • Set the initial widget width/height and modify references from scene to
or switch the run mode in your IDE project properties. JavaFX widget.
• Return the widget at the end of the script.
Mobile applications are restricted to the Common Profile,
which does not include all the features of desktop applications. To run the widget, simply change your project properties
The full list of restrictions is shown in Table 5. to run the application using Web Start Excecution. This will
Table 5. Functionality Not Available in the Common Profile automatically create a JNLP file compatible with WidgetFX
Class(es) Affected Variables and Methods and launch the Widget Runner, which allows you to test your
javafx.ext.swing.* All
widget as shown in the Figure 7.
javafx.reflect.* All
javafx.scene.Scene stylesheets
javafx.scene.effect.* All
javafx.scene.effect.light.* All
javafx.scene.shape.ShapeIntersect All
javafx.scene.shape.ShapeSubtract All
javafx.stage.AppletStageExtension All
javafx.util.FXEvaluator All
Language Reference
JavaFX supports all the Java datatypes plus a new Duration
type that simplifies writing animationed UIs.
Data Types:
DataType Java Equivalent Range Examples
Running as a Desktop Widget Duration <None> -263 to 263-1 milliseconds 1h, 5m, 30s, 500ms
You can deploy your application as a desktop widget using the Character char 0 to 65535 0,20,32
WidgetFX open-source framework. Any JavaFX application can Byte byte -128 to 127 -5, 0,5
be converted to a widget by including the WidgetFX-API.jar Short short -32768 to 32767 -300, 0, 521
and making some small updates to the code.
Long long -263 to 263-1 2009, 03731,0x07d9
The Following code fragment highlights the code changes Float float 1.40x10 45 and 3.40x1038 3.14, 3e8, 1.380E-23
required: Double double 4.94x10 324 and 1.80x10308 3.14, 3e8, 1.380E-123
Operator Meaning Precedence Examples • Sequences require commas after all elements except close
++ Pre/post increment 1 ++i, i++
braces; however it is recommended to always use commas.
• You can declare a sequence as a nativearray. This is an
-- Pre/post decrement 1 --i, i--
optimization so that arrays returned from a Java method
not Boolean negation 2 not (cond)
don’t need to be converted to a sequence.
* Multiply 3 2 * 5, 1h * 4
/ Divide 3 9 / 3, 1m / 3
Access Modifiers:
The JavaFX access modifiers are based upon Java with the
mod Modulo 3 20 mod 3
addition of extra variable-only modifiers.
+ Add 4 0 + 2, 1m + 20s
Modifier Name Description
- Subtract (or negate) 4 (2) -2, 32 - 3, 1h - 5m
<Default> Script only access Only accessible within the same script file
== Equal 5 value1 == value2, 4 == 4
package Package access Only accessible within the same package
!= Not equal 5 value1 != value2, 5 != 4
protected Protected access Only accessible within the same package or by
< Less than 5 value1 < value2, 4 < 5 subclasses
<= Less than or equal 5 value1 <= value2, 5 <= 5 public Public access Can be accessed anywhere
> Greater than 5 value1 > value2, 6 > 5 public- Read access Var/def modifier to allow a variable to be read anywhere
read modifier
>= Greater than or equal 5 value1 >= value2, 6 >= 6
public-init Init access modifier Var/def modifier to allow a variable to be initialized or
instanceof Is instance of class 6 node instanceof Text
read anywhere
as Typecast to class 6 node as Text
• Unlike Java the default permission in JavaFX is script-only
and Boolean and 7 cond1 and cond2
rather than package.
or Boolean or 8 cond1 or cond2
• The var/def access modifiers can be stacked with other
+= Add and assign 9 value += 5
modifiers, such as public-read protected
-= Subtract and assign 9 value -= 3
Expressions:
*= Multiply and assign 9 value *= 2
JavaFX supports many of the same expressions as Java, but
/= Divide and assign 9 value /=4
adds in powerful inline functions and for loop extensions.
= Assign 9 value = 7
Expression Syntax Example
• Multiplication and division of two durations is allowed, but if if (cond) expr1 else expr2 if (grass.green) {
if (cond) then expr1 else expr2 grass.mow();
not meaningful } else {
• Underflows/Overflows will fail silently, producing grass.water();
}
inaccurate results var water = if (grass.color ==
• Divide by zero will throw a runtime exception BLACK) aLot else aLittle;
Predicate seq[x|boolean] nums[n|n mod 2 == 0]; // = [2, 4] Just like in Java programs:
Reverse reverse seq reverse letters; // = [“c”, “b”, “a”] • continue can be used to skip a for or while loop iteration
Insert insert x into seq insert 5 into nums; // = [1, 2, 3, 4, 5] • break can be used to exit a for or while loop
insert x before seq[i] insert “gamma” before letters[2]; // = [“a”, “b”, • return can be used to exite from a function event if inside
insert x after seq[i] “gamma”, “c”]
insert “2.3” after nums[1]; // = [1, 2, 2.3, 3, 4] a loop
Delete delete seq[i] delete letters[1]; // = [“a”, “c”]
delete seq[x..y] delete nums[1..2]; // = [1, 4] Magic Variables:
delete x from seq delete “c” from letters; // = [“a”, “b”] JavaFX provides some built-in variables that can be accessed
delete seq delete letters; // = []
from any code running inside a script.
• The javafx.util.Sequences class provides additional Name Description
functions, which allow you to manipulate sequences, such
__DIR__ Directory the current classfile is contained in
as min, max, search, shuffle, and short.
__FILE__ Full path to the current classfile
• Nested sequences are automatically flattened, so [[1,2],
__PROFILE__ The current profile, which can be ‘desktop’ or ‘mobile’
[3,4]] is equivalent to [1,2,3,4].
API Reference An easy way to view and navigate the full JavaFX API is using
In the short span of a few pages you have already seen quite the JFXplorer application. The following URL will launch it in as
a bit of the JavaFX platform. Some other functionality that a web start application that you can use to start exploring the
JavaFX offers includes: JavaFX API today:
Package Description
http://jfxtras.org/samples/jfxplorer/JFXplorer.jnlp
javafx.animation Animation and Interpolation
A BOUT t h e A u t h o r R E C OMM E N D E D b o o k
Open-Source Developer and Agile Manager, Learn from bestselling JavaFX author Jim
Stephen Chin is founder of numerous open- Weaver and expert JavaFX developers
source projects including WidgetFX and JFXtras Weiqi Gao, Stephen Chin, and Dean Iverson
and Senior Manager at Inovis in Emeryville, to discover the highly anticipated JavaFX
CA. He has been working with Java desktop technology and platform that enables
and enterprise technologies for over a decade, developers and designers to create RIAs that
and has a passion for improving development can run across diverse devices. Covering the
technologies and process. Stephen’s interest in Java technologies JavaFX Script language, JavaFX Mobile, and
has lead him to start a Java and JavaFX focused blog and coauthor development tools, Pro JavaFX™ Platform:
the upcoming Pro JavaFX Platform book together with Jim Weaver, Script, Desktop and Mobile RIA with Java™
Weiqi Gao, and Dean Iverson. Technology provides code examples that cover
virtually every language and API feature.
Stephen’s Blog:
http://steveonjava.com/
BUY NOW
Jim Weaver’s JavaFX Learning Blog: books.dzone.com/books/projavafx
http://learnjavafx.typepad.com/
sig son
McD
simple, short, and to the point.”
De
Ja
By
#8
ired
Insp e
by th
GoF ler con
tinu
ed
and
ler d
oesn
’t ha
ve to
James Ward, Adobe Systems
ity,
ler
E: hand
LUD Best
se
ns ibil
in ld b
Cha d ultip ific
man
n M h
z.co
Download Now
Com reter b
Use se
Spring Dynamic Modules Spring Configuration
n
rp n
n A
Inte Whe erm
tor det
a rd
n
ues
Itera tor ore req
n A
dm
dia
Drupal jQuery Selectors
n
nd
Me rver d an the p io
n ha
re f c
tho e to h
n
Me Exce i
renc
se
Ob NS
Refcardz.com
plate refe listed in
le
ER mp
n
ATT
Tem ick
Grails Windows Powershell
Exa
a qu s
NP s, a ct
it
IG e s e rn b je
vid patt able O
DES
! V is
pro s,
UT ard ign us ram ha
cs
ABO
Patt
erns
refc oF)
ur (G lemen
des
ts o
f Re
es c
lass
diag
exa
mp
le obje
Java Performance Tuning Dependency Injection with EJB 3
arz
n F o d rl d h
Des
ig go
f s: E inclu al w
o suc
Th is G a n
l 23 sign Pa
tt e rn e rn
patt , and a
re je ts
c
t ob mentin
g AN
D
Eclipse RCP Netbeans IDE JavaEditor
c
a h c M
OM
ef
in Ea c n tr u
orig o kD
e
re atio c ons im ple C
Java Concurrency Getting Started with Eclipse
o a rm om
re R
b oftw info to ir
the dS ed the Clie
nt
cre
teC
ente on, us
age : Us d from Con
Ori tt e r ns p le c t cute
()
ti Pa ou b je
lana eo +exe
Selenium Very First Steps in Flex
ws
o
rm ts +exe bject
Cre y to fo bjec an o ela ions
hip
the ed te o hms
, d as
h t d