JavaFX in Action - Manning Publications
JavaFX in Action - Manning Publications
JavaFX in Action - Manning Publications
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
8 CHAPTER 1 Welcome to the future: <strong>in</strong>troduc<strong>in</strong>g <strong>JavaFX</strong><br />
1.2 M<strong>in</strong>imum effort, maximum impact: a quick shot of <strong>JavaFX</strong><br />
It’s hard to visualize the difference a<br />
new technology will make to your<br />
work<strong>in</strong>g life from a description alone.<br />
What’s often needed is a short but<br />
powerful example of what’s possible.<br />
A picture is worth a thousand words,<br />
and so <strong>in</strong> lieu of a few pages of text, I<br />
give you figure 1.6.<br />
Six shaded balls bounce smoothly<br />
up and down onto a reflective shaded<br />
surface, as the desktop is exposed<br />
beh<strong>in</strong>d the balls. The w<strong>in</strong>dow has<br />
no title bar (the title bar you see<br />
belongs to the text editor beh<strong>in</strong>d),<br />
but click<strong>in</strong>g <strong>in</strong>side its boundary will<br />
close the w<strong>in</strong>dow and exit the bounc<strong>in</strong>g<br />
ball application.<br />
Now, the sixty-four-thousand-dollar question: how many l<strong>in</strong>es of code does it take<br />
to construct an application like this? Consider what’s <strong>in</strong>volved: multiple objects mov<strong>in</strong>g<br />
<strong>in</strong>dependently, circular shad<strong>in</strong>g on each ball, l<strong>in</strong>ear shad<strong>in</strong>g on the ground, a<br />
reflection effect, transparency aga<strong>in</strong>st the desktop, and a click event handler. If you<br />
said “less than 70,” then you’d be right! Indeed, the whole source file is only 1.4k <strong>in</strong><br />
size and weighs <strong>in</strong> at a mere 69 l<strong>in</strong>es. Don’t believe me? Take a look at list<strong>in</strong>g 1.1.<br />
List<strong>in</strong>g 1.1 The bounc<strong>in</strong>g ball demo<br />
Figure 1.6 The bounc<strong>in</strong>g balls demo, with color<br />
shad<strong>in</strong>g, reflection effect, and a shaped w<strong>in</strong>dow<br />
(that’s a text editor beh<strong>in</strong>d, with source code loaded,<br />
demonstrat<strong>in</strong>g the app’s transparency).<br />
import javafx.animation.*;<br />
import javafx.stage.*;<br />
import javafx.scene.*;<br />
import javafx.scene.effect.*;<br />
import javafx.scene.shape.*;<br />
import javafx.scene.<strong>in</strong>put.*;<br />
import javafx.scene.pa<strong>in</strong>t.*;<br />
var balls = for(i <strong>in</strong> [0..