As the preface hints, JavaFX has a combination of features that makes it unique. This chapter gives you a taste of the language and some of these features. Our goal is to choose a representative example so you get a feel for the kinds of programs possible with JavaFX. The example (a guitar tuner) illustrates language constructs while keeping the discussion concrete. We’ll veer away from the example at times to illustrate additional JavaFX features that are relevant. While this overview is in no way complete (remember, it’s just a taste), we hope to entice you to explore JavaFX further.
The source code for GuitarTuner appears at the end of the chapter. To keep the text flowing, we’ll show snippets from this application throughout the overview.What You Will Learn
- What makes JavaFX unique as a scripting language
- All about object literals and declarative constructs
- Introducing the JavaFX scene graph
- Declaring variables, properties, and objects
- Initializing objects and object properties
- Basics in container coordinate space and layout
- Creating a custom node
- Manipulating objects with color, effects, and gradients
- Getting things done with binding, event handlers, and animation
2.1 Introducing JavaFX
What is JavaFX? JavaFX is a scripting language with static typing. You can call a Java API as needed from JavaFX and create new object types with classes, but JavaFX also provides an easy declarative syntax. (Declarative means you say what you want and the system figures out how to do it for you.) JavaFX provides properties for manipulating objects within a 2D coordinate system, specifying fill and pen stroke colors, and creating special effects. You can create shapes and lines, manipulate images, play videos and sounds, and define animations.
Let’s begin exploring JavaFX by introducing the basics. Our introduction begins with project GuitarTuner where you’ll see the main structure of a JavaFX program. Then, you’ll explore a few JavaFX language constructs and see how to improve the appearance of your applications. Finally, you’ll see how to make applications do things.