JavaFX in Action
By Simon Morris
()
About this ebook
JavaFX in Action is a hands-on tutorial that introduces and explores JavaFXthrough numerous bite-sized projects. The book provides a solid groundingin the JavaFX syntax and related APIs by showing web developers how to applythe key features of the JavaFX platform. Readers quickly absorb the fundamentalsof the technology while exploring the possibilities JavaFX provides forcreative, rich designs.
Readers learn to transform variables and operators into bouncing raindrops, brilliant colors, and dancing interface components. They also learn how to interactwith existing Java code to give old apps some new JavaFX sparkle.
Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book.
Simon Morris
Writer, drinker, football fan, evolver of ideas, and trainee Renaissance man. I’m on a quest for original thought and the perfect martini
Related to JavaFX in Action
Related ebooks
AspectJ in Action: Enterprise AOP with Spring Applications Rating: 0 out of 5 stars0 ratingsHTML5 for .NET Developers: Single page web apps, JavaScript, and semantic markup Rating: 0 out of 5 stars0 ratingsPractical Java Programming for IoT, AI, and Blockchain Rating: 0 out of 5 stars0 ratingsStruts 2 in Action Rating: 0 out of 5 stars0 ratingsJavaScript Unleashed: Harnessing the Power of Web Scripting Rating: 0 out of 5 stars0 ratingsThe Well-Grounded Java Developer, Second Edition Rating: 0 out of 5 stars0 ratingsEmber.js in Action Rating: 0 out of 5 stars0 ratingsThird-Party JavaScript Rating: 0 out of 5 stars0 ratingshapi.js in Action Rating: 0 out of 5 stars0 ratingsJava Testing with Spock Rating: 0 out of 5 stars0 ratingsMastering Go: Navigating the World of Concurrent Programming Rating: 0 out of 5 stars0 ratingsFront-End Tooling with Gulp, Bower, and Yeoman Rating: 0 out of 5 stars0 ratingsSass and Compass in Action Rating: 5 out of 5 stars5/5WPF in Action with Visual Studio 2008: Covers Visual Studio 2008 Service Pack 1 and .NET 3.5 Service Pack 1! Rating: 0 out of 5 stars0 ratingsMetaprogramming in .NET Rating: 5 out of 5 stars5/5Silverlight 5 in Action Rating: 0 out of 5 stars0 ratingsNode.js in Practice Rating: 0 out of 5 stars0 ratingsObjective-C Fundamentals Rating: 0 out of 5 stars0 ratingsRestlet in Action: Developing RESTful web APIs in Java Rating: 0 out of 5 stars0 ratingsXamarin in Action: Creating native cross-platform mobile apps Rating: 0 out of 5 stars0 ratingsGroovy in Action Rating: 0 out of 5 stars0 ratingsSails.js in Action Rating: 0 out of 5 stars0 ratingsDependency Injection: Design patterns using Spring and Guice Rating: 0 out of 5 stars0 ratingsFunctional Programming in Java: How functional techniques improve your Java programs Rating: 0 out of 5 stars0 ratingsThe Well-Grounded Java Developer: Vital techniques of Java 7 and polyglot programming Rating: 4 out of 5 stars4/5JavaScript Application Design: A Build First Approach Rating: 0 out of 5 stars0 ratingsContinuous Integration in .NET Rating: 0 out of 5 stars0 ratingsThe Java Module System Rating: 4 out of 5 stars4/5Isomorphic Web Applications: Universal Development with React Rating: 0 out of 5 stars0 ratingsASP.NET AJAX in Action Rating: 0 out of 5 stars0 ratings
Reviews for JavaFX in Action
0 ratings0 reviews
Book preview
JavaFX in Action - Simon Morris
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department
Manning Publications Co.
Sound View Court 3B
Greenwich, CT 06830
email: orders@manning.com
©2010 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without the use of elemental chlorine.
Development Editor: Tom Cirtin
Copyeditor: Linda Recktenwald
Proofreader: Elizabeth Martin
Typesetter: Gordan Salinovic
Cover designer: Leslie Haimes
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 14 13 12 11 10 09
Dedication
To my father, the coolest folk singer this side of the Mersey. (Be honest, Dad, if you’d known how obsessed I was going to get, would you have agreed to buy my first computer?)
Brief Table of Contents
Copyright
Brief Table of Contents
Table of Contents
List of Figures
List of Tables
List of Listings
Preface
Acknowledgments
About this Book
About the Title
About the Cover Illustration
Chapter 1. Welcome to the future: introducing JavaFX
Chapter 2. JavaFX Script data and variables
Chapter 3. JavaFX Script code and structure
Chapter 4. Swing by numbers
Chapter 5. Behind the scene graph
Chapter 6. Moving pictures
Chapter 7. Controls, charts, and storage
Chapter 8. Web services with style
Chapter 9. From app to applet
Chapter 10. Clever graphics and smart phones
Chapter 11. Best of both worlds: using JavaFX from Java
Appendix A. Getting started
Appendix B. JavaFX Script: a quick reference
Appendix C. Not familiar with Java?
Appendix D. JavaFX and the Java platform
Index
Table of Contents
Copyright
Brief Table of Contents
Table of Contents
List of Figures
List of Tables
List of Listings
Preface
Acknowledgments
About this Book
About the Title
About the Cover Illustration
Chapter 1. Welcome to the future: introducing JavaFX
1.1. Introducing JavaFX
1.1.1. Why do we need JavaFX Script? The power of a DSL
1.1.2. Back to the future: the rise of the cloud
1.1.3. Form follows function: the fall and rebirth of desktop Java
1.2. Minimum effort, maximum impact: a quick shot of JavaFX
1.3. Comparing Java and JavaFX Script: Hello JavaFX!
1.4. Comparing JavaFX with Adobe AIR, GWT, and Silverlight
1.4.1. Adobe AIR and Flex
1.4.2. Google Web Toolkit
1.4.3. Microsoft Silverlight
1.4.4. And by comparison, JavaFX
1.5. But why should I buy this book?
1.6. Summary
Chapter 2. JavaFX Script data and variables
2.1. Annotating code with comments
2.2. Data types
2.2.1. Static, not dynamic, types
2.2.2. Value type declaration
2.2.3. Initialize-only and reassignable variables (var, def)
2.2.4. Arithmetic on value types (+, -, etc.)
2.2.5. Logic operators (and, or, not, <, >, =, >=, <=, !=)
2.2.6. Translating and checking types (as, instanceof)
2.3. Working with text, via strings
2.3.1. String literals and embedded expressions
2.3.2. String formatting
2.3.3. String localization
2.4. Durations, using time literals
2.5. Sequences: not quite arrays
2.5.1. Basic sequence declaration and access (sizeof)
2.5.2. Sequence creation using ranges ([..], step)
2.5.3. Sequence creation using slices ( [..<] )
2.5.4. Sequence creation using a predicate
2.5.5. Sequence manipulation (insert, delete, reverse)
2.5.6. Sequences, behind the scenes
2.6. Autoupdating related data, with binds
2.6.1. Binding to variables (bind)
2.6.2. Binding to bound variables
2.6.3. Binding to a sequence element
2.6.4. Binding to an entire sequence (for)
2.6.5. Binding to code
2.6.6. Bidirectional binds (with inverse)
2.6.7. The mechanics behind bindings
2.6.8. Bound functions (bound)
2.6.9. Bound object literals
2.7. Working nicely with Java
2.7.1. Avoiding naming conflicts, with quoted identifiers
2.7.2. Handling Java native arrays (nativearray of)
2.8. Summary
Chapter 3. JavaFX Script code and structure
3.1. Imposing order and control with packages (package, import)
3.2. Developing classes
3.2.1. Scripts
3.2.2. Class definition (class, def, var, function, this)
3.2.3. Object declaration (init, postinit, isInitialized(), new)
3.2.4. Object declaration and sequences
3.2.5. Class inheritance (abstract, extends, override)
3.2.6. Mixin inheritance (mixin)
3.2.7. Function types
3.2.8. Anonymous functions
3.2.9. Access modifiers (package, protected, public, public-read, public-init)
3.3. Flow control, using conditions
3.3.1. Basic conditions (if, else)
3.3.2. Conditions as expressions
3.3.3. Ternary expressions and beyond
3.4. Sequence-based loops
3.4.1. Basic sequence loops (for)
3.4.2. For loops as expressions (indexof)
3.4.3. Rolling nested loops into one expression
3.4.4. Controlling flow within for loops (break, continue)
3.4.5. Filtering for expressions (where)
3.5. Repeating code with while loops (while, break, continue)
3.6. Acting on variable and sequence changes, using triggers
3.6.1. Single-value triggers (on replace)
3.6.2. Sequence triggers (on replace [..])
3.7. Trapping problems using exceptions (try, catch, any, finally)
3.8. Summary
Chapter 4. Swing by numbers
4.1. Swing time: Puzzle, version 1
4.1.1. Our initial puzzle data class
4.1.2. Our initial GUI class
4.1.3. Building the buttons
4.1.4. Model/View/Controller, JavaFX Script style
4.1.5. Running version 1
4.2. Better informed and better looking: Puzzle, version 2
4.2.1. Making the puzzle class clever, using triggers and function types
4.2.2. Group checking up close: function types
4.2.3. Firing the update: triggers
4.2.4. Better-looking GUI: playing with the underlying Swing component
4.2.5. Running version 2
4.3. Game on: Puzzle, version 3
4.3.1. Adding stats to the puzzle class
4.3.2. Finishing off the puzzle grid GUI
4.3.3. Adding a status line to our GUI with a label
4.3.4. Running version 3
4.4. Other Swing components
4.5. Bonus: using bind to validate forms
4.6. Summary
Chapter 5. Behind the scene graph
5.1. What is a scene graph?
5.1.1. Nodes: the building blocks of the scene graph
5.1.2. Groups: graph manipulation made easy
5.2. Getting animated: LightShow, version 1
5.2.1. Raindrop animations
5.2.2. The RainDrop class: creating graphics from geometric shapes
5.2.3. Timelines and animation (Timeline, KeyFrame)
5.2.4. Interpolating variables across a timeline (at, tween, =>)
5.2.5. How the RainDrop class works
5.2.6. The LightShow class, version 1: a stage for our scene graph
5.2.7. Running version 1
5.3. Total transformation: LightShow, version 2
5.3.1. The swirling lines animation
5.3.2. The SwirlingLines class: rectangles, rotations, and transformations
5.3.3. Manipulating node rendering with transformations
5.3.4. The LightShow class, version 2: color animations
5.3.5. Running version 2
5.4. Lost in translation? Positioning nodes in the scene graph
5.5. Bonus: creating hypertext-style links
5.6. Summary
Chapter 6. Moving pictures
6.1. Taking control: Video Player, version 1
6.1.1. The Util class: creating image nodes
6.1.2. The Button class: scene graph images and user input
6.1.3. The GridBox class: lay out your nodes
6.1.4. The Player class, version 1
6.1.5. Running version 1
6.2. Making the list: Video Player, version 2
6.2.1. The List class: a complex multipart custom node
6.2.2. The ListPane class: scrolling and clipping a scene graph
6.2.3. Using media in JavaFX
6.2.4. The Player class, version 2: video and linear gradients
6.2.5. Creating varying color fills with LinearGradient
6.2.6. Running version 2
6.3. Bonus: taking control of fonts
6.4. Summary
Chapter 7. Controls, charts, and storage
7.1. Comments welcome: Feedback, version 1
7.1.1. The Record class: a bound model for our UI
7.1.2. The Feedback class: controls and panel containers
7.1.3. Running version 1
7.2. Chart topping: Feedback, version 2
7.2.1. Cross-platform persistent storage
7.2.2. How Storage manages its files
7.2.3. Adding pie and bar charts
7.2.4. Taking control of chart axes
7.2.5. Other chart controls (area, bubble, line, and scatter)
7.2.6. Running version 2
7.3. Bonus: creating a styled UI control in JavaFX
7.3.1. What is a stylesheet?
7.3.2. Creating a control: the Progress class
7.3.3. Creating a skin: the ProgressSkin class
7.3.4. Using our styled control with a CSS document
7.3.5. Further CSS details
7.4. Summary
Chapter 8. Web services with style
8.1. Our project: a Flickr image viewer
8.1.1. The Flickr web service
8.1.2. Getting registered with Flickr
8.2. Using a web service in JavaFX
8.2.1. Calling the web service with HttpRequest
8.2.2. Parsing XML with PullParser
8.2.3. A recap
8.2.4. Testing our web service code
8.3. Picture this: the PhotoViewer application
8.3.1. Displaying thumbnails from the web service: the GalleryView class
8.3.2. The easy way to animate: transitions
8.3.3. The main photo desktop: the PhotoViewer class
8.3.4. Running the application
8.4. Size matters: node bounds in different contexts
8.5. Summary
Chapter 9. From app to applet
9.1. The Enigma project
9.1.1. The mechanics of the Enigma cipher
9.2. Programmer/designer workflow: Enigma machine, version 1
9.2.1. Getting ready to use the JavaFX Production Suite
9.2.2. Converting SVG files to FXZ
9.2.3. The Rotor class: the heart of the encryption
9.2.4. A quick utility class
9.2.5. The Key class: input to the machine
9.2.6. The Lamp class: output from the machine
9.2.7. The Enigma class: binding the encryption engine to the interface
9.2.8. Running version 1
9.2.9. Shortcuts using NetBeans, Photoshop, or Illustrator
9.3. More cryptic: Enigma machine, version 2
9.3.1. The Rotor class, version 2: giving the cipher a visual presence
9.3.2. The Paper class: making a permanent output record
9.3.3. The Enigma class, version 2: at last our code is ready to encode
9.3.4. Running version 2
9.4. From application to applet
9.4.1. The Enigma class: from application to applet
9.4.2. The JavaFX Packager utility
9.4.3. Packaging up the applet
9.4.4. Dragging the applet onto the desktop
9.5. Bonus: Building the UI in an art tool
9.6. Summary
Chapter 10. Clever graphics and smart phones
10.1. Amazing games: a retro 3D puzzle
10.1.1. Creating a faux 3D effect
10.1.2. Using 2D to create 3D
10.2. The maze game
10.2.1. The MazeDisplay class: 3D view from 2D points
10.2.2. The Map class: where are we?
10.2.3. The Radar class: this is where we are
10.2.4. The Compass class: this is where we’re facing
10.2.5. The ScoreBoard class: are we there yet?
10.2.6. The MazeGame class: our application
10.2.7. Running the MazeGame project
10.3. On the move: desktop to mobile in a single bound
10.3.1. Packaging the game for the mobile profile
10.3.2. Running the mobile emulator
10.3.3. Emulator options
10.3.4. Running the software on a real phone
10.4. Performance tips
10.5. Summary
Chapter 11. Best of both worlds: using JavaFX from Java
11.1. Different styles of linking the two languages
11.2. Adventures in JavaFX Script
11.2.1. Game engine events
11.2.2. Calling the JavaFX Script event code from Java
11.3. Adding FX to Java
11.3.1. The problem with mixing languages
11.3.2. The problem solved: an elegant solution to link the languages
11.3.3. Fetching the JavaFX Script object from within Java
11.4. Summary
Appendix A. Getting started
A.1. Downloading and installing
A.1.1. The Java Development Kit (essential)
A.1.2. NetBeans or other IDEs (optional)
A.1.3. The IDE plug-ins (required, if using an IDE)
A.1.4. The JavaFX SDK (essential)
A.1.5. The JavaFX Production Suite (optional)
A.1.6. Recap
A.2. Compiling JavaFX
A.2.1. Setting the path
A.2.2. Running the compiler
A.2.3. Running the code
A.3. Useful URLs
Appendix B. JavaFX Script: a quick reference
B.1. Comments
B.2. Variables and data types—the basics
Variable declaration (def, var, Boolean, Integer, Number, String)
Arithmetic (+, -, etc.)
Logic operators (and, or, not, <, >, =, >=, <=, !=)
Casting (as, instanceof)
B.3. Strings
String literals and embedded expressions
String formating
String localization
B.4. Durations
B.5. Sequences: lists of objects
Basic sequence declaration and access (sizeof)
Sequence creation using ranges ( [..], step)
Sequence creation using slices ( [..<] )
Sequence creation using a predicate
Sequence manipulation (insert, delete, reverse)
B.6. Binds
Binding to variables (bind)
Binding to a sequence
Binding to code
Bidirectional binds (with inverse)
Bound functions (bound)
B.7. Cooperating with Java
Quoted identifiers
Java native arrays (nativearray of)
B.8. Packages (package, import)
B.9. Developing classes
Scripts
Class definition (class, def, var, function, this)
Object declaration (init, postinit, isInitialized(), new)
Class inheritance (abstract, extends, override)
Mixin inheritance (mixin)
Function types
Anonymous functions
Access modifiers (package, protected, public, public-read, public-init)
B.10 Conditions
Basic conditions (if, else)
Ternary expressions and beyond
B.11 Loops
Basic sequence loops (for)
Rolling nested loops into one expression
Controlling flow within for loops (break, continue)
Filtering for expressions (where)
While loops (while, break, continue)
B.12 Triggers
Single-value triggers (on replace)
Sequence triggers (on replace [..])
B.13 Exceptions (try, catch, any, finally)
B.14 Keywords
B.15 Operator precedence
B.16 Pseudo variables
Appendix C. Not familiar with Java?
C.1. Static types versus dynamic types
C.2. Casts
C.3. Packages
C.3.1. Importing classes from a package
C.3.2. Packages and physical files
C.3.3. Creating packaged classes and dealing with name clashes
C.4. Object orientation
C.4.1. Modeling the world with classes
C.4.2. Classes from classes: subclassing and overriding
C.4.3. An object can be referenced in different ways: polymorphism
C.4.4. Partial implementation: abstract functions and interfaces
C.5. Access modifiers
Appendix D. JavaFX and the Java platform
D.1. How not to go native
D.2. Java SE/ME/EE and JDK/JRE: three editions, two audiences
D.3. Release versions: a rose by any other name
Index
List of Figures
Chapter 1. Welcome to the future: introducing JavaFX
Figure 1.1. A complex GUI typical of modern desktop applications. Two windows host scrolling control palettes, while another holds an editable image and rulers.
Figure 1.2. Google’s Gmail is an example of a website application that attempts to mimic the look and function of a desktop application.
Figure 1.3. Google Docs runs inside a browser and has a much simpler GUI than Microsoft Office or OpenOffice.org. (Google Docs shown.)
Figure 1.4. An applet (the game 3D-Blox) runs inside a web page, living alongside other web content like text and images.
Figure 1.5. The StudioMOTO demo, one of the original JavaFX examples, shows off a glossy UI with animation, movement, and rotating elements all responding to the user’s interaction.
Figure 1.6. The bouncing balls demo, with color shading, reflection effect, and a shaped window (that’s a text editor behind, with source code loaded, demonstrating the app’s transparency).
Figure 1.7. Separated at birth: Hello World!
as a JavaFX application and as a Java application
Chapter 4. Swing by numbers
Figure 4.1. A number puzzle grid, shown both empty and recently completed
Figure 4.2. Groups are rows, columns, or boxes within the grid, which must hold unique values.
Figure 4.3. The game as it appears after clicking on a few cells (note the highlight on the lower 3). Depending on your JRE version, you’ll get Ocean-(left) or Nimbus- (right) themed buttons.
Figure 4.4. The text of each button is bound to the corresponding value in the puzzle’s grid sequence.
Figure 4.5. Model/View/Controller is achieved in JavaFX Script largely by way of bound expressions. Here one such expression depends on two strings for the contents of its SwingLabel.
Figure 4.6. Coordinate translations for column, row, and box groups
Figure 4.7. The restyled user interface, with differentiated boxes using background color and duplicate warnings using foreground (text) color
Figure 4.8. Elements inside a Flow are lined up in rows or columns, wrapping when necessary.
Figure 4.9. The puzzle game with its status panel, implemented using Flow
Figure 4.10. Age must be between 18 and 65 inclusive. Incorrect content shows a red circle and disables Send (left and right); correct content shows a light-green circle and enables Send (middle).
Chapter 5. Behind the scene graph
Figure 5.1. A symbolic representation of retained mode and immediate mode. The former sees the world as a hierarchy of graphical elements, the latter as just pixels.
Figure 5.2. Elements in a scene graph can be manipulated without concern for how the actual pixels will be repainted. For example, hiding elements will trigger an automatic update onscreen.
Figure 5.3. Grouping nodes in a scene graph allows them to be manipulated as one. The upper rocket has been rotated as a group; the lower rocket has been rotated as separate constituent nodes.
Figure 5.4. Raindrops are constructed from several ripples. Each ripple expands outward, fading as it goes.
Figure 5.5. Groups provide a local coordinate space for their children. The Group is laid out to (100,50) and the Circle (positioned around its center) to (80,50), giving an absolute position of (180,100).
Figure 5.6. One use of key frames is to define milestones throughout an animation, recording the state scene graph objects should be in at that point.
Figure 5.7. The master timeline awakes at regular intervals and fires off the next ripple’s timeline. The effect is a raindrop of several ripples with staggered start times.
Figure 5.8. The SwirlingLines class creates a single ring of spokes, rotating around a central origin. Instances demonstrating different attribute settings are displayed.
Figure 5.9. SwirlingLines creates a ring of rectangles, fully customizable from its instance variables.
Figure 5.10. With and without centering: moving the Rectangle negatively in the y axis, by half its height, has the effect of centering it on its origin—in this case the radial spoke of a ring.
Figure 5.11. Two examples of transformations: translate and then rotate (left), and rotate and then translate (right). The order of the operations results in markedly different results.
Figure 5.12. Version 2 of the project application, featuring both swirling lines and raindrops
Figure 5.13. A Rectangle with its own local coordinates, translated within a Group. The local coordinates are not affected when the node is transformed in its parent’s space, like a rotation by 270 degrees.
Figure 5.14. The link text depicted in three states: idle (left), underlined when hovered over (center), and red upon a mouse button press (right)
Chapter 6. Moving pictures
Figure 6.1. A preview of the simple video player we’ll be building in this chapter
Figure 6.2. The interface for version 1 of our application
Figure 6.3. The button is constructed from two bitmap images: a background (blue circle) and icon (arrow). When the button is pressed, a ghost of its icon expands and fades.
Figure 6.4. Ignoring the invisible Rectangle (used for sizing), there are three layers in our button.
Figure 6.5. The GridBox node positions its children into a grid, with flexible column and row sizes.
Figure 6.6. Our custom button and layout nodes on display
Figure 6.7. Lift off! Our control panel (bottom) is combined with a new list (left-hand side) and video node (center) to create the final player.
Figure 6.8. The List and ListPane classes will allow us to present a selection of movie files for the user to pick from.
Figure 6.9. A closer look at our List and ListPane, with hover effect visible on the background of the list items
Figure 6.10. Like other JavaFX user interface elements, video is played via a dedicated MediaView scene graph node. (Note: MediaPlayer is not a visual element; the control icons are symbolic.)
Figure 6.11. The video area layout and sizing are controlled by variables, some at the script level and others local to the scene graph node itself.
Figure 6.12. A gradient paint is one where the pixel tone changes over the course of a given area. Colors are set at stops along a line, and the paint transitions between them as a shape is drawn.
Figure 6.13. A proportional (P) gradient scaled to full height. Then three nonproportional examples, gradient (0,0) to (0,100), painted onto 200 x 200 sized rectangles with various cycle (C) methods.
Figure 6.14. You’ll never walk alone: relive favorite moments with your own homemade video player (like your soccer team lifting the Champion’s League trophy).
Figure 6.15. Text rendered using an embedded TrueType font file
Chapter 7. Controls, charts, and storage
Figure 7.1. A bar chart, with 3D effect, showing feedback scores from contributors
Figure 7.2. Our project’s simple feedback form, complete with text fields, radio buttons, and sliders. Oh, and a Next button in the corner!
Figure 7.3. The flow of updates between the model and the UI: the data and text box are bidirectionally bound, the validity boolean is bound to the data, and the validity label is bound to the validity boolean.
Figure 7.4. To update the model from a ToggleGroup we bind a variable against the group’s selected button and then use a trigger to translate any changes into the button’s index value.
Figure 7.5. The createRow() function is a convenience for manufacturing each part of the feedback form. Each row consists of three parts: a text label and two nodes (controls).
Figure 7.6. Version 1 of the application, running
Figure 7.7. JavaFX has a powerful library of chart controls, including a 3D pie chart.
Figure 7.8. A bar chart and a pie chart, as drawn in 3D by the JavaFX 1.2 chart library
Figure 7.9. This is what the project’s bar chart would look like if it used two data series rather than one. Each category has two bars, and the key at the foot of the chart shows two labels.
Figure 7.10. An area chart (left) and a standard 2D bar chart (right)
Figure 7.11. A bubble chart (left) and a line chart (right)
Figure 7.12. A standard 2D pie chart (left) and a scatter chart (right)
Figure 7.13. Version 2 of the Feedback application runs, complete with form (main image) and two charts (thumbnails).
Figure 7.14. Two style rules and two HTML paragraph elements. The first rule applies to both paragraphs, while the second applies only to paragraphs of class myClass.
Figure 7.15. The data from the control (Progress) and the CSS from the stylesheet are combined inside the skin (ProgressSkin) to produce a displayable UI control, in this example, a progress bar.
Figure 7.16. Three examples of our progress bar in action
Chapter 8. Web services with style
Figure 8.1. Our photo viewer will allow us to contact the online photo service, view thumbnails from a gallery, and then select and toss a full-sized image onto a desktop as if it were a real photo.
Figure 8.2. When start() is called on an HttpRequest object, a second thread takes over and communicates its progress through callback events, allowing the GUI thread to get back to its work.
Figure 8.3. Photos selected from the thumbnail bar fly onto the desktop.
Figure 8.4. The custom scene graph node we are creating
Figure 8.5. The thumbnail bar scene graph is made up on three core components: a group of frame rectangles in the background, a group of images over it, and a text node to display the thumb titles.
Figure 8.6. All three transitions, translate (movement), scale, and rotate, are performed at the same time to the same scene graph node.
Figure 8.7. The coordinate system of a node always matches the rotation of the node itself. The gray rectangle has been rotated 30 degrees clockwise, yet its local coordinate system is unaffected.
Chapter 9. From app to applet
Figure 9.1. Tracing one path inside the Enigma, forming a circuit linking key A with lamp W and key W with lamp A. But as the rotors move, the circuit changes to provide a different link.
Figure 9.2. Our initial version of the Enigma machine will provide only basic encryption, input, and output, served up with a splash of visual flare, naturally!
Figure 9.3. SVG images are formed from a collection of shapes; the key is two circles painted with gradient fills. JavaFX also supports layered bitmaps from Photoshop and vector images from Illustrator.
Figure 9.4. The SVG Converter takes SVG files, using the W3C’s vector format, and translates them into FXZ files, using JavaFX’s declarative scene graph markup.
Figure 9.5. Two disks, with left/right faces. The rotor wiring is not symmetrical (left), but we can create a reflector from a rotor by ensuring 13 of the wires mirror the path of the other 13 (right).
Figure 9.6. The lamp image is constructed from two layers. The lower layer shows the rim of the lamp and its dormant (off) graphic; the upper layer, invisible by default, shows the active (on) graphic.
Figure 9.7. This is what we should see when compiling and running version 1. It works, but not in a very practical way. The stylized button and lamp nodes help lend the application an authentic feel.
Figure 9.8. Quite an improvement: the Enigma emulator acquires a printout display and rotors, as well as an attractive shaded backdrop.
Figure 9.9. Each line of our Paper node is scaled to create the optical effect of a surface curving away, to accompany the shading of the background Rectangle.
Figure 9.10. Our Enigma machine in action, ready to keep all our most intimate secrets safe from prying eyes (providing they don’t have access to any computing hardware made after 1940).
Figure 9.11. Our applet running inside Microsoft’s Internet Explorer
Figure 9.12. Starting life in a web page, our Enigma emulator was then dragged onto the desktop to become an application (note the desktop icon) and finally relaunched from the desktop.
Figure 9.13. Two buttons (left), each formed using four carefully labeled layers (demonstrated right), which are manipulated by JavaFX code to create functioning buttons
Chapter 10. Clever graphics and smart phones
Figure 10.1. Get lost! This is our simple 3D maze game. The whole thing is constructed from the JavaFX scene graph, using basic shapes.
Figure 10.2. The 3D in our maze is all fake. The grid defines the maze geometry without using any complex mathematics.
Figure 10.3. The geometry of our maze. Using a flat 20 x 20 grid as the viewport, the regular numbers describe x coordinates, and the rotated numbers (underlined) describe y coordinates.
Figure 10.4. A plan view of the scene graph pieces that have to be added, in order, from back (row 0) to front (row 3). The shaded area represents the player’s field of view.
Figure 10.5. By plotting the points on our polygon, using the xPos and yPos tables for reference, we can created the illusion of perspective.
Figure 10.6. Having created our scene graph walls, we need to be able to switch them off and on depending on which cells in the map are wall blocks, relative to the player’s location.
Figure 10.7. The maze game, complete with radar in the bottom left-hand corner and a compass in the bottom right
Figure 10.8. The scoreboard sits at the bottom of the display, showing the moves used and a SUCCESS!
message once the end of the maze is reached
Figure 10.9. Our maze game hits the small screen. More specifically, it’s running on the JavaFX 1.2 mobile emulator.
Figure 10.10. The old JavaFX 1.1 mobile emulator (left) and its 1.2 update (right) in action. Strangely, the older version seems to reproduce the gradient paints better.
Figure 10.11. A desktop version of the 3D maze, complete with bitmap walls using a perspective effect. Sadly, the bitmaps had to go when the project was adapted to fit a mobile platform.
Chapter 11. Best of both worlds: using JavaFX from Java
Figure 11.1. A simple Java adventure game engine, using an isometric view. The control panel at the foot of the window, as well as the in-game events, will be written using JavaFX Script.
Figure 11.2. Each cell in the game environment is created from up to five images: one floor tile, two wall tiles, and two faces that modify one side of a wall tile.
Figure 11.3. This is room 1 (room IDs start at 0), which the fragment of data file in listing 11.1 refers to. The player stands on cell (3,1), in front of him is the event cell (4,1), and beyond that the door link cell (5,1).
Figure 11.4. The panel at the foot of the game’s window is written entirely in compiled JavaFX Script.
Figure 11.5. Java’s Game class and the JavaFX Script ControlPanelImpl.fx class communicate via a Java interface, ControlPanel.java.
Appendix D. JavaFX and the Java platform
Figure D.1. From compile time to runtime: the lifecycle of a typical Java application
List of Tables
Chapter 2. JavaFX Script data and variables
Table 2.1. JavaFX Script value types
Table 2.2. List of arithmetic operators
Chapter 3. JavaFX Script code and structure
Table 3.1. Basic access modifiers
Table 3.2. Additive access modifiers
Chapter 6. Moving pictures
Table 6.1. JavaFX media support on various operating systems
Chapter 9. From app to applet
Table 9.1. JavaFXPackager options
Chapter 10. Clever graphics and smart phones
Table 10.1. Emulator options
Appendix B. JavaFX Script: a quick reference
Table B.1. Basic access modifiers
Table B.2. Additive access modifiers
Table B.3. Keywords and reserved words
Table B.4. Operators
Table B.5. Pseudo variables
List of Listings
Chapter 1. Welcome to the future: introducing JavaFX
Listing 1.1. The bouncing ball demo
Listing 1.2. Hello World as JavaFX Script
Listing 1.3. Hello World as Java
Chapter 2. JavaFX Script data and variables
Listing 2.1. JavaFX Script comments
Listing 2.2. Defining value types
Listing 2.3. Defining value types using defaults
Listing 2.4. Defining value types using type inference
Listing 2.5. Declaring variables with def
Listing 2.6. Arithmetic on value types
Listing 2.7. Further examples of arithmetic operations
Listing 2.8. Logic operators
Listing 2.9. Casting types
Listing 2.10. Basic string definitions
Listing 2.11. Multiline strings, double- and single-quoted strings
Listing 2.12. Strings with embedded expressions
Listing 2.13. String formatting
Listing 2.14. String localization: the
Listing 2.15. String localization
Listing 2.16. Declaring Duration types
Listing 2.17. Arithmetic on duration types
Listing 2.18. Sequence declaration
Listing 2.19. Referencing a sequence element
Listing 2.20. Sequence creation using a range
Listing 2.21. Sequence creation using a stepped range
Listing 2.22. Expanding one sequence inside another
Listing 2.23. Sequence declaration using a slice
Listing 2.24. Sequence declaration using a predicate
Listing 2.25. Sequence manipulation: insert
Listing 2.26. Sequence manipulation: delete
Listing 2.27. Sequence manipulation: reverse
Listing 2.28. Binding into a string
Listing 2.29. Binding between variables
Listing 2.30. Binding to bound variables
Listing 2.31. Binding against a sequence element
Listing 2.32. Binding to a sequence itself
Listing 2.33. Binding to a ternary expression
Listing 2.34. A more complex binding example
Listing 2.35. Bidirectional binding
Listing 2.36. Minimal recalculation
Listing 2.37. Bound functions
Listing 2.38. Binding and object literals
Listing 2.39. Quoted identifiers
Listing 2.40. Native arrays
Chapter 3. JavaFX Script code and structure
Listing 3.1. Using the package statement to shorten class names
Listing 3.2. Including a class inside a package
Listing 3.3. Scripts and classes
Listing 3.4. Class definition, with variables and functions
Listing 3.5. A closer look at functions
Listing 3.6. Object declaration, using declarative syntax or the new keyword
Listing 3.7. Sequence declaration
Listing 3.8. Class inheritance, part 1
Listing 3.9. Class inheritance, part 2
Listing 3.10. Class inheritance, part 3
Listing 3.11. Virtual functions demonstrated
Listing 3.12. Mixins
Listing 3.13. Function types
Listing 3.14. Passing functions as parameters to other functions
Listing 3.15. Anonymous functions
Listing 3.16. Access modifiers on a class
Listing 3.17. Testing access modifiers
Listing 3.18. Conditions
Listing 3.19. Conditions as expressions
Listing 3.20. Ternary expressions
Listing 3.21. Beyond ternary expressions
Listing 3.22. Condition expressions taken to an extreme
Listing 3.23. Basic for loops
Listing 3.24. Sequence creation using for expressions
Listing 3.25. Nested loops within one for expression
Listing 3.26. Flow control within for, with break and continue
Listing 3.27. Filtered for expression
Listing 3.28. Basic while loops
Listing 3.29. Trigger on variable change
Listing 3.30. Shorter trigger syntax
Listing 3.31. Triggers on a sequence
Listing 3.32. Exception handling
Chapter 4. Swing by numbers
Listing 4.1. PuzzleGrid.fx (version 1)
Listing 4.2. Game.fx (version 1)
Listing 4.3. PuzzleGrid.fx (version 2)
Listing 4.4. Game.fx (version 2)
Listing 4.5. PuzzleGrid.fx (version 3)
Listing 4.6. Game.fx (version 3)
Listing 4.7. Using bind for form validation
Chapter 5. Behind the scene graph
Listing 5.1. RainDrop.fx
Listing 5.2. LightShow.fx (version 1)
Listing 5.3. SwirlingLines.fx
Listing 5.4. LightShow.fx (version 2)
Listing 5.5. Link.fx
Chapter 6. Moving pictures
Listing 6.1. Util.fx
Listing 6.2. Button.fx (part 1)
Listing 6.3. Button.fx (part 2)
Listing 6.4. GridBox.fx
Listing 6.5. Player.fx (version 1)
Listing 6.6. List.fx (part 1)
Listing 6.7. List.fx (part 2)
Listing 6.8. ListPane.fx (part 1)
Listing 6.9. ListPane.fx (part 2)
Listing 6.10. Player.fx (version 2, part 1)
Listing 6.11. Player.fx (version 2, part 2)
Listing 6.12. Player.fx (version 2, part 3)
Listing 6.13. fonts.mf
Listing 6.14. FontTest.fx
Chapter 7. Controls, charts, and storage
Listing 7.1. Record.fx (version 1)
Listing 7.2. Feedback.fx (version 1, part 1)
Listing 7.3. Feedback.fx (version 1, part 2)
Listing 7.4. Feedback.fx (version 1, part 3)
Listing 7.5. Feedback.fx (version 1, part 4)
Listing 7.6. Record.fx (version 2)
Listing 7.7. Feedback.fx (version 2, part 1)
Listing 7.8. Feedback.fx (version 2, part 2)
Listing 7.9. Feedback.fx (version 2, part 3)
Listing 7.10. Progress.fx
Listing 7.11. ProgressSkin.fx (part 1)
Listing 7.12. ProgressSkin.fx (part 2)
Listing 7.13. TestCSS.fx
Listing 7.14. Test.css
Chapter 8. Web services with style
Listing 8.1. FlickrService.fx (part 1)
Listing 8.2. FlickrService.fx (part 2)
Listing 8.3. FlickrService.fx (part 3)
Listing 8.4. FlickrResult.fx (part 1)
Listing 8.5. FlickrResult.fx (part 2)
Listing 8.6. FlickrPhoto.fx
Listing 8.7. TestWS.fx
Listing 8.8. GalleryView.fx (part 1)
Listing 8.9. GalleryView.fx (part 2)
Listing 8.10. GalleryView.fx (part 3)
Listing 8.11. GalleryView.fx (part 4)
Listing 8.12. PhotoViewer.fx (part 1)
Listing 8.13. PhotoViewer.fx (part 2)
Listing 8.14. PhotoViewer.fx (part 3)
Listing 8.15. PhotoViewer.fx (part 4)
Chapter 9. From app to applet
Listing 9.1. Rotor.fx (version 1)
Listing 9.2. Util.fx
Listing 9.3. Key.fx
Listing 9.4. Lamp.fx
Listing 9.5. Enigma.fx (version 1, part 1)
Listing 9.6. Enigma.fx (version 1, part 2)
Listing 9.7. Enigma.fx (version 1, part 3)
Listing 9.8. Rotor.fx (version 2—changes only)
Listing 9.9. Paper.fx
Listing 9.10. Enigma.fx (version 2, part 1 – changes only)
Listing 9.11. Enigma.fx (version 2, part 2)
Listing 9.12. Enigma.fx (version 2, part 3 – changes only)
Listing 9.13. Enigma.fx (version 3 – changes only)
Listing 9.14. Enigma_browser.jnlp
Listing 9.15. UI.fx
Chapter 10. Clever graphics and smart phones
Listing 10.1. MazeDisplay.fx (part 1)
Listing 10.2. MazeDisplay.fx (part 2)
Listing 10.3. MazeDisplay.fx (part 3)
Listing 10.4. MazeDisplay.fx (part 4)
Listing 10.5. Map.fx (part 1)
Listing 10.6. Map.fx (part 2)
Listing 10.7. Radar.fx
Listing 10.8. Compass.fx
Listing 10.9. ScoreBoard.fx
Listing 10.10. MazeGame.fx
Chapter 11. Best of both worlds: using JavaFX from Java
Listing 11.1. A fragment of the game data file
Listing 11.2. Map.java: calling JavaFX Script from Java
Listing 11.3. ControlPanel.java
Listing 11.4. Game.java (part 1): adding JavaFX UIs to Java code
Listing 11.5. Game.java (part 2): adding JavaFX user interfaces to Java code
Listing 11.6. ControlPanelImpl.fx
Preface
I suppose for many it was just another unremarkable mid-May Wednesday; certainly I don’t recall the weather making any effort to surprise. What might have made the day slightly memorable for some, perhaps, was that Manchester United was playing Chelsea in the final of the ultra-prestigious soccer European Champions League. A couple of days earlier I’d returned from a few weeks’ sampling of random pubs and music clubs in North America, starting in Los Angeles (actually, starting in Dublin, Ireland, but I’ll not complicate the story) and ending in Vancouver. Now I sat in front of my TV, hoping, somewhat optimistically, for a 3–0 destruction of United, to round off the perfect holiday.
And that’s when the phone rang.
Mike Stephens, associate publisher at Manning Publications, was on the other end. Earlier that day he’d emailed me to request a one-to-one, and, bang!, on the agreed time, there he was! Over the next 60 minutes or so I hardly noticed any of the game. We talked about Java and the way the industry was going, and inevitably the topic drifted toward JavaFX. At that time JavaFX was still in an embryonic state: features were being added, evaluated, and then modified or dropped. Anything and everything could change with each prototype release. Yet, to me at least, the ideas behind JFX showed great promise. If enough backing was put behind the project, and with a fair wind to guide it, I thought JavaFX had the potential to really shake up the whole front-end (user-facing) rich internet application market. With those sentiments in mind, some weeks earlier, I’d blogged about my early experiences—good and bad—with the platform on java.net.
I’ve always had an interest in computer graphics. I consider myself fortunate