xvi
Introduction
Goal of This Book
This book is intended to show off some interesting visual effects that can be implemented in JavaFX. The
chapters explore particle effects, realistic animations driven by physics, and a number of other
techniques. The first set of chapters look at each effect in isolation, and then the last chapters look at
some of these effects in combination. The book concludes by presenting an entire sample application.
The book's goal is to provide a developer with a number of new tools that can be used to spruce up
an old application or used in a new application. Given the newness of JavaFX at the time this book was
written, I suspect that most developers are in the evaluation phase, exploring whether JavaFX is a
worthwhile technology for some new project. I hope some of the effects in this book will convince you
that JavaFX has a lot of promise.
While always keeping the needs of developers in mind, I will also spend some time looking at why
applications require graphics at all, and how leveraging experts in visual design and user experience will
greatly improve the end result.
This book was written by first messing around with JavaFX code to produce something interesting,
then writing a little about it, and finally cleaning up the source code with the help of my technical
reviewer. I bring this up to point out that the accompanying source code is really the heart of this book; it
drove the content of these chapters. For the book to achieve its goal, I believe it is best consumed with
the code handy. Go fire up NetBeans and run the example code as you get to each chapter, I think it will
add a lot. Besides, this book is in black and white, while many of the examples are rich in color.
Who This Book Is For
This book is not an introduction to JavaFX. I assume you have played around with JavaFX already and
hopefully have written an application or two. JavaFX is different enough from Java and other common
languages that I urge anyone interested in this book to spend some time with JavaFX first. Just becoming
familiar with the numerous curly and square brackets that get littered throughout JavaFX source code is
a good start. This will help you to understand the concepts being presented, instead of fighting with
syntax. JavaFX code can be surprisingly short and powerful, but some of that brevity comes with a cost in
clarity or obviousness.
Additionally I expect you, the reader, to have some experience with computer graphics, such as
coordinate systems, polygons, transformations, colors and gradients, and trigonometry. If you feel that
brushing up on any of these topics is worth your while, I would like to recommend the online course
"Computer Sc-Computer Graphics" from the Indian Institute of Technology Madras (IIT Madras) by
Download at WoweBook.com
■ INTRODUCTION
xvii
professor Dr. Sukhendu Das. The class is presented on YouTube at the following URL:
If you are a JavaFX expert already and just want to get to the effects, jump to Chapter 2 – but come
back and read the section Designers and Developers in Chapter 1 sometime.
Who This book Is By
I think it is worth giving a little bit of background on my development history. My hope is that by sharing
where I come from as a developer, you will be able to better understand my opinions and my interest in
JavaFX.
Like so many of us, I started playing with computers at a young age. My early, pre-career computer
programming was mostly in BASIC and Pascal. Using these tools, I tried in vain to reproduce a number
of video games I was very fond of. I had neither the time nor the experience to create anything of any real
value, but I learned a lot about 2D graphics in the process.
In college I was introduced to Java and fell in love with the language and with Swing. When I
graduated I got a number of contract jobs writing Swing applications. This was a great experience, since
I had to learn the hard way about things such as testing code and the truth about cross-platform
deployment, and, most important, it taught me about how users use applications.
I remember presenting a new feature to a client; I think it was a tool that laid out printed labels. My
client sat down at my app and starting clicking randomly; he had no idea how to use the “clever”
interface I had devised. I tried to explain how some combination of mouse gestures and key presses
produced the desired results. He didn’t get it and I rewrote it. This showed me the value of using
standard things such as buttons, sliders, and wizards to meet the expectation of the user. This is not to
say there is no place for innovation, of course there is; computer interfaces have been evolving since
they were invented, but the changes come in small steps.
Later, a small company that specializes in Rich Internet Applications (RIAs) hired me. This was my
first experience working with user experience designers who have made it their career to understand
how people use applications, and to create interfaces that meet those expectations. Most of these
designers don’t know how to code; they create PDF documents explaining the interface and a developer
implements it. There is an example of one of these wire frame documents in Chapter 2 in the section
Designers and Developers, where I explain how they are used.
Working at a company that put design first showed me the importance of having these specialists
involved in a project. Many developers and graphical designers use the term “developer ugly” to refer to
the quick and dirty graphics that developers create while in the process of getting the app to work.
Developer ugly can also apply to user experience decisions as well. I don’t intend to say that all
developers lack the skills or the design sense to create nice-looking images or elegant UIs. I am pointing
out that when you have your head down in the code, worrying about thread synchronization or
something, it is not the best time to create an icon for a button. Just as graphic designers have all day to
iterate over their creations, a user experience designer has all day to contemplate the UI. So I say, let
specialists do their job. And of course, great projects take shape when all of the specialists work together.
So now my day job is implementing designs by other people, and at night I continue to try and
create simple but fun video games. It is this endless tinkering that led me to JavaFX and to write this
book. So while I try to address design issues as they come up in this book, my primary concern is getting
it to work. I hope that the techniques in this book will enable a developer presented with a design
challenge to say, “We can do that.”
■ INTRODUCTION
xviii
Chapter Overview
The following is a brief description of each chapter. Most of the chapters present a stand-alone effect
and show how it is implemented. Feel free to read the chapters out of order, except for the last two,
Chapters 10 and 11. Those two chapters rely on the foundation presented in the earlier chapters.
Chapter 1 briefly investigates why user interfaces work the way that they do by looking at them in a
historical context. It further explains the roles of designers and developers and offers some suggestions
on how they can work together. The chapter finishes by showing how this collaboration works when
using JavaFX.
Chapter 2 shows how to implement a particle effect in JavaFX. We look at how particles are
animated and how they can be blended together to create some surprisingly nice results. The sample
code that accompanies this chapter is a playground for testing different combinations of properties of a
particle effect.
Chapter 3 introduces how JavaFX can create animations, which are handy for transitioning the user
from one panel to another. These transitions are common in RIAs and cell phones, and JavaFX is very
capable of reproducing these effects.
Chapter 4 reviews lighting in JavaFX and how the different types of lighting affect the appearance of
a node. The chapter also explores how lights can be animated to create a number of compelling visuals.
Chapter 5 looks at the Interpolator API in Java FX and shows how it can be extended to create new
interpolators. The source for this chapter is an application for testing the effects of using different
interpolators and is fully explained. The last part of the chapter shows how interpolators can be used in
conjunction with the transitions from chapter 3.
Chapter 6 shows how to include an open source physics engine call Phys2d in your application. This
chapter uses a number of examples to show how the physics engine is used and how JavaFX can be used
as a presentation layer for the simulation.
Chapter 7 presents a technique where animations created with other tools can be used in a JavaFX
application. The code provides a pattern for efficiently loading a large number of images and animating
them.
Chapter 8 looks at gradients in JavaFX and how they can be animated. The animation of gradients in
JavaFX is a little different than other animations in JavaFX and explains how they work. A final example
shows why you might want to use animated graphics in your application.
Chapter 9 combines Java Sound and JavaFX to create visualizations of audio in a JavaFX scene. This
chapter covers the basics of using Java Sound as well as how to expose the audio stream in a JavaFX-
friendly way.
Chapter 10 combines the techniques from Chapter 2 on particles and Chapter 6 on physics to show
how the two can be used together to create some interesting animations.
Chapter 11 presents an entire JavaFX application called Clown Cannon reviewing the design process
for the application and how the techniques from the previous chapters are combined. How the
application manages its state is also explored.