Mastering JavaFX 10 Build Advanced and Visually Stunning Java Applications 1st Edition Sergey Grinev instant download
Mastering JavaFX 10 Build Advanced and Visually Stunning Java Applications 1st Edition Sergey Grinev instant download
https://ebookfinal.com/download/mastering-javafx-10-build-
advanced-and-visually-stunning-java-applications-1st-edition-
sergey-grinev/
https://ebookfinal.com/download/javafx-developing-rich-internet-
applications-1st-edition-jim-clarke/
https://ebookfinal.com/download/mastering-aspectj-aspect-oriented-
programming-in-java-1st-edition-joseph-d-gradecki/
Teach Yourself VISUALLY Algebra Teach Yourself Visually
1st Edition David Alan Herzog
https://ebookfinal.com/download/teach-yourself-visually-algebra-teach-
yourself-visually-1st-edition-david-alan-herzog/
https://ebookfinal.com/download/teach-yourself-visually-golf-teach-
yourself-visually-consumer-1st-edition-cheryl-anderson/
https://ebookfinal.com/download/mastering-kali-linux-for-advanced-
penetration-testing-1st-edition-robert-w-beggs/
https://ebookfinal.com/download/core-java-2-advanced-features-5th-
edition-cay-s-horstmann/
https://ebookfinal.com/download/professional-blazeds-creating-rich-
internet-applications-with-flex-and-java-1st-edition-shashank-tiwari/
Mastering JavaFX 10 Build Advanced and Visually
Stunning Java Applications 1st Edition Sergey Grinev
Digital Instant Download
Author(s): Sergey Grinev
ISBN(s): 9781788293822, 1788293827
Edition: 1
File Details: PDF, 7.41 MB
Year: 2018
Language: english
Mastering JavaFX 10
Sergey Grinev
BIRMINGHAM - MUMBAI
Mastering JavaFX 10
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form
or by any means, without the prior written permission of the publisher, except in the case of brief quotations
embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented.
However, the information contained in this book is sold without warranty, either express or implied. Neither the
author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to
have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products
mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy
of this information.
ISBN 978-1-78829-382-2
www.packtpub.com
To my mom, Raisa, and my wife, Irina, without whom this book would have never happened.
To my son, Alexander, who'll learn to read way after this book becomes obsolete.
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as
well as industry leading tools to help you plan your personal development and advance
your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos
from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters, and receive exclusive discounts and offers on Packt books and
eBooks.
Contributors
Sergey enjoys sharing his experience: he regularly presents Java conferences, gives lessons,
and solves questions on Stack Overflow.
Writing a book is way harder than I thought, and I'm deeply grateful to the people who
helped me.
Thanks to my mother, Raisa, who made me study English from the age of 5.
Thanks to my beloved family, Irina and Alexander, for always inspiring me and coping
with a half-absent father.
Thanks to my English teacher and friend, April, for the support and inspiration.
Also, great thanks to my editor, Gauri, and the whole Packt team.
About the reviewer
Naman Nigam is a philomath currently working as a software developer with Flipkart. He
is heavily involved in gamification and personalization platforms to develop services and
features that are responsible for customer engagement at Flipkart. He keeps up to date with
the technological upgrades available and their usages, while helping with code reviews,
ensuring a consistent performance alongside.
The Flipkart tech team has been extremely helpful during the review process by providing
environments where Naman was able to experiment with some of the latest Java releases.
I would like to thank my wife, Neha, who stood by me heedfully whenever I'd take out
some time reviewing the book, and my friends who persistently encouraged me to be a part
of it.
Paint 36
ImagePattern 37
Gradients 38
Customizing lines with Stroke API 39
Basic Stroke 39
Dashed lines 40
Connecting line designs using Line Join 41
Working with the Shape operations 41
Transformations 42
Coordinates and bounds 44
Working with Bounds Demo 44
Using the ScenicView tool to study JavaFX scenegraph 45
Basic Controls 47
Button and Event Handlers 48
Size of the Controls 49
Clock demo 50
Summary 54
Chapter 3: Connecting Pieces – Binding 55
Working with the Property API 55
Using the Observable API 56
Introducing the Binding API 57
Rules of binding 59
Read-only properties 59
Binding is a one-to-many relation 60
Binding blocks setters 60
Bidirectional binding 60
Using binding for visual help 61
The role of listeners 62
Using binding operations 62
String operations 63
Arithmetic operations 65
Boolean operations 67
Working with bidirectional binding and converters 69
Creating custom bindings 71
Implementing base binding classes 71
Bindings helper function 72
Understanding binding collections 73
Summary 75
Chapter 4: FXML 76
Introduction to FXML 76
Basics of FXML 76
Benefits of FXML 78
Limitations of FXML 79
Working with FXML loaders 79
[ ii ]
Table of Contents
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[v]
Table of Contents
Sorting 199
Observable Model 199
Making a table editable 201
Working with JavaFX charts 202
Creating a Pie chart 202
Creating a Line chart 204
Review of other XY charts 205
Custom controls 208
Skins 209
ClockControl demo 209
Java 9 and 10 API for skins 214
Summary 215
Chapter 11: Packaging with Java9 Jigsaw 216
Handmaking JAR files 216
Running the demo project 217
Basic commands of the javapackager tool 219
Creating self-contained applications 219
Preparing OS-native installers with javapackager 220
Working with Jigsaw modules 221
Making our own module 222
Summary 224
Chapter 12: 3D at a Glance 225
Introduction to the JavaFX 3D API 225
Basic 3D elements 225
Adding Camera to the Scene 226
Lighting the Scene 228
Using Materials 229
Using Bump Map 229
Working with the Diffuse and Self-Illumination maps 230
Summary 232
Chapter 13: What's Next? 233
Other materials 233
Official documentation of JavaFX 233
Stackoverflow.com 234
Working with JavaFX source code 234
Beyond the official API 234
Frameworks and libraries 234
Blogs 235
Future of JavaFX 235
Summary 236
Other Books You May Enjoy 237
Index 240
[ vi ]
Preface
JavaFX is a set of libraries added to Java in order to provide capabilities to build a modern
UI. It was added to Java a few releases ago, as old libraries—Swing and AWT—proved to
be outdated and too burdened with backward compatibility issues.
JavaFX was designed and developed from scratch to provide Java developers with the
capabilities to build modern, rich UI applications with a large set of shapes, controls, and
charts. It was designed with performance in mind, is capable of using graphics cards, and is
based on the new graphical engine.
In this book, we will study many aspects of JavaFX and go through a large set of examples
based on these topics.
Chapter 2, Building Blocks – Shapes, Text, and Controls, fills the window we created in the
previous chapter with various building blocks provided by the JavaFX API.
Chapter 3, Connecting Pieces – Binding, explains that binding is a new method to greatly
simplify communication between components.
Chapter 4, FXML, introduces FXML is a powerful tool for building a complex JavaFX UI
and separating business logic from UI design. We will design an FXML application and try
SceneBuilder—an FXML-based UI designer tool.
Chapter 5, Animation, demonstrates how to use the JavaFX Animation API to create
various types of dynamic content.
Preface
Chapter 6, Styling Applications with CSS, outlines that JavaFX supports Cascading Style
Sheets (CSS) to style applications similar to web pages.
Chapter 7, Building a Dynamic UI, reviews several JavaFX practices to build a modern
adaptive UI.
Chapter 8, Effects, explains that effects such as a reflection or shadow are an essential part
of modern applications.
Chapter 9, Media and WebView, showcases how JavaFX provides special controls dedicated
to video, audio, and web content.
Chapter 10, Advanced Controls and Charts, reviews complex controls such as tables and
charts.
Chapter 11, Packaging with Java9 Jigsaw, goes through the next step after writing an
application—deploying it to end users. We will review JavaFX app building, packaging,
and Jigsaw modularization options.
Chapter 12, 3D at a Glance, teaches us to create several small 3D applications and study
JavaFX's 3D capabilities.
Chapter 13, What's Next?, looks at further options for mastering JavaFX.
Before starting to read this book, I strongly encourage you to get a Git and download the
accompanying GitHub repository, which you can find at https://github.com/sgrinev/
mastering-javafx-9-10-book.
There are about 130 code samples, grouped by chapters numbers. For your convenience,
each chapter is a NetBeans project, but almost every code sample is self-sufficient and can
be run separately without the help of an IDE. If you opt for the latter approach, you can
find the guidelines for running JavaFX apps from the command line in Chapter
11, Packaging with Java9 Jigsaw.
[2]
Preface
While you can get a good grasp of the topics described by just reading the book and
looking at the screenshots, toying with the code samples and seeing the results of your
changes will give you a much better understanding of JavaFX APIs.
Also, although the book has been released once and for all, I plan to update and fix these
samples if any issues are found later on. Refer to GitHub's README.md for a history of
changes.
Once the file is downloaded, please make sure that you unzip or extract the folder using the
latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/
PacktPublishing/Mastering-JavaFX-10. In case there's an update to the code, it will be
updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available
at https://github.com/PacktPublishing/. Check them out!
[3]
Preface
Conventions used
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames,
file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an
example: "You can import one CSS from another using the @import keyword."
When we wish to draw your attention to a particular part of a code block, the relevant lines
or items are set in bold:
@DefaultProperty("children")
public class Pane
extends Region
Bold: Indicates a new term, an important word, or words that you see onscreen. For
example, words in menus or dialog boxes appear in the text like this. Here is an example:
"For example, we see that the area under the mouse cursor is a DIV, as shown in the
following screenshot."
[4]
Preface
Get in touch
Feedback from our readers is always welcome.
General feedback: Email feedback@packtpub.com and mention the book title in the
subject of your message. If you have questions about any aspect of this book, please email
us at questions@packtpub.com.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you have found a mistake in this book, we would be grateful if you would
report this to us. Please visit www.packtpub.com/submit-errata, selecting your book,
clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we
would be grateful if you would provide us with the location address or website name.
Please contact us at copyright@packtpub.com with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in
and you are interested in either writing or contributing to a book, please visit
authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on
the site that you purchased it from? Potential readers can then see and use your unbiased
opinion to make purchase decisions, we at Packt can understand what you think about our
products, and our authors can see your feedback on their book. Thank you!
[5]
1
Stages, Scenes, and Layout
During the last decade, user interfaces have evolved beyond the capabilities of the old Java
technologies. Modern users want to work with visually appealing applications and are
used to the rich user interfaces brought by Web 2.0 and smartphones.
To address that, JavaFX was envisioned and added to Java a few releases ago. It was
created from scratch to avoid any backward compatibility issues, and with a great
understanding of the needs of modern user interfaces.
In this book, we will review the most important JavaFX APIs and will look into resolving
some of the most common problems that JavaFX developers face, based on my
development experience and over 500 questions I've answered in the JavaFX section of
stackoverflow.com.
In the first chapter, we will start with the backstage of a JavaFX application, including its
windows and content area, and see which API is responsible for each of these main
building blocks:
At the end of the chapter, we will create a clock demo that will demonstrate the concepts
from this chapter.
Stages, Scenes, and Layout Chapter 1
Initializes JavaFX toolkit (subsystems and native libraries required to run JavaFX)
Starts JavaFX Application Thread (a thread where all UI work happens) and all
working threads
Constructs the Application instance (which provides a starting point for your
program) and calls the user-overridden methods
Handles application command line parameters
Handles all cleanup and shutdown once the application ends
Glass toolkit
This toolkit is responsible for low-level interaction with operating systems. It uses native
system calls to manage windows, handle system events, timers, and other components.
Note that Glass is written from scratch; it doesn't use AWT or Swing libraries. So, it's better
to not mix old Swing/AWT components and JavaFX ones for the sake of performance.
[7]
Discovering Diverse Content Through
Random Scribd Documents
are called by the compound names of father and mother, in which the name
of the father is the given name and that of the mother the surname. Thus the
son of Chel and Chan would be Na-Chan-Chel, which means son of Chel by
his wife Chan. A stranger coming to a village, especially if he be poor, will be
received in all kindness by any family of his name. Men and women of the
same name do not marry, for this is considered very wrong.
CHAPTER II
THE CHURCH OF SAN ISIDRO AND ITS FRAGRANT LEGEND
The group of books called the Chilan Balam, which are chiefly
ideographic transcripts of the more ancient works, written in the
Maya tongue but in Spanish characters, probably were made
surreptitiously by some of the educated natives soon after the
Conquest. There are sixteen of these books still extant. The meaning
of this Maya name, Chilan Balam, is interesting. Chi means “mouth”;
lan indicates action. Therefore Chilan is “mouth action,” or “speech.”
Balam is synonymous for either “tiger” or “ferocity.” But the tiger
was worshiped as a deity and the combination of the words, Chilan
Balam, means “Speech of the Gods.” The Maya priests were
sometimes called by the name, indicating that they were the
mouthpieces of the gods, and doubtless these records took their
name from the priestly appellation.
The individual books of the Chilan Balam are known by the names
of the villages in which they were found, and in a few cases the
name of the village may have been derived from the presence of the
book. The most important of these books are Nabula, Chun-may-el
(which means “something of the first” or “original”), Kua, Man, X-
kutz-cab, Ixil, Tihosuco, and Tixcocob.
Just when these books were written is not known, but there is
evidence that the book of Mani was written prior to 1595 and the
book of Nabula tells of an epidemic which occurred in 1663. While
teaching the natives to write the Maya language in Spanish
characters, Bishop Landa employed a rather original method, which
is our only key to reading these writings and which serves as our
only clue to the more ancient hieroglyphs. The ancient Maya writings
were purely picture writings, but to some extent the hieroglyphs had
lost their original picture significance and had come to have a
somewhat symbolic meaning.
In arranging the so-called Maya alphabet (which was first used by
the priests in writing out the prayers for the Mayas), Landa
employed a very ingenious method and one that was practical at the
time. He took the Spanish alphabet and beginning with “A” he asked
the educated Indian to draw the character for him in which the
sound of “A” was predominant. Naturally, after many attempts by the
Indian to furnish such a character he finally selected the hieroglyph
ac, which is a picture of a turtle’s head and which in Maya means
“turtle” or “dwarf” or something having a slow movement. Next he
took the letter “B” and eventually chose the character be, which
means “road,” “walk,” “run,” and consists of the picture of a
footprint. Therefore—not to go into a lengthy description of the
system—he had “A” from ac, “B” from be, etc. With this
extemporized alphabet the priests were able to write out the
Catholic prayers in such a way that the Indian could repeat them in
Spanish by using the sound of the first part of his hieroglyph for the
sound of each Spanish letter.
It may be seen from the foregoing that Landa’s alphabet cannot
be used for translating Maya, for when the hieroglyphs are made to
represent the sounds of the Spanish alphabet the result does not
indicate the original connection of a Maya word with its glyph. This
fact was a great disappointment among archæologists, who at first
expected to translate the Maya Codices by the use of the Landa
alphabet. Their hopes, however, were short-lived and they even
pronounced Landa an impostor. On the contrary, he has
unintentionally given us what is almost a Rosetta Stone.
The Codices, I fear, will never yield a connected story, as they are
written in a stenographic or shorthand style consisting of
disconnected sentences.
Many of the stones, or stelæ, may contain history, and as soon as
we know the meanings of, possibly, a thousand glyphs we shall be
able to make a decided advance in the art of reading the books.
Landa in his book explains not the Maya glyphs but the way the
priests used these Maya characters for religious purposes. For
example, he says Ma-in-kati means “I do not want,” represented in
the ancient Maya by three simple glyphs. Written as the priests had
arranged, with a glyph for each sound of a Spanish letter, the result
is a combination of five glyphs, which, if given their original Maya
pictured meanings, leads to the rather surprising knowledge that “no
dead animal was seen at this place,” or, literally, “not see tail
[animal] death place.”
Besides the Codices and the Chilan Balam, which together are
frequently alluded to as the Maya Chronicles, there are some other
documents such as titles to land, records of surveys, etc. There is a
unique history of the Conquest, written by a contemporary native
chief called Na Kuk Pech, whose name means “house of the
feathered wood-tick.” The story was written in the native language,
by means of Spanish characters, and has been translated recently by
Señor Juan Martinez, whose profound knowledge of the Maya
language has eminently fitted him for this task.
The history of Chi-chen Itza is of especial interest because this
was the Holy City, the Mecca of all the ancient Maya people.
According to the Maya Chronicles, one or several tribes set out from
a place called Nonual, in 160 a.d., and apparently spent many years
in aimless wandering, arriving finally, in 241 a.d., at a place they
named Chac Nouitan. Then follows a gap in our knowledge and the
next we learn of these people is that in 445 a.d., while they were
residing at a place called Bak-Halal, they heard of Chi-chen Itza. It is
clear that Chi-chen Itza was already an inhabited city at that time.
Soon after this, these tribes moved to Chi-chen Itza, where they
lived until about 600 a.d., when, for some unaccountable reason,
they abandoned it utterly and migrated to the land of Chan Kan
Putun. And this residence was in turn abandoned two hundred and
sixty years later, because of some calamity; one Chronicle speaks of
a great fire.
For nearly a hundred years, to quote from the Chronicles, “the
Itzas lived in exile and great distress under the trees and under the
branches.” Then, some of them reëstablished Chi-chen Itza in 950
a.d., while others founded the city of Uxmal or went to Mayapan.
The second residence lasted for some two hundred years. About
1200 a.d., the Itzas, under the ruler Ulumil, invaded the city of
Mayapan and at about this same time Chi-chen Itza was attacked
and depopulated by foreigners—in all probability the Nahuas
(Mexicans), who came down from the north. The last event alluded
to in the Chronicles is the coming of the Spaniards under Montejo,
who found the Mayas already decadent and their cities long ruined
and abandoned.
We have no authentic description of the actual condition of Chi-
chen Itza when the Spaniards came, but it is known with certainty
that Tiho (place of the five temples), one of the ancient cities, the
site of the modern city of Mérida, was in ruins. The temples were
dilapidated and overgrown with vegetation and great trees were
rooted in the walls. The few inhabitants living around these ruins
knew virtually nothing of the founders of the city, nor of those who
had lived there when it was in its prime.
At the coming of the Spaniards to Chi-chen Itza, about 1541, the
city was inhabited by a few people who were, I think, nothing more
than campers—inferior people using as shelters the buildings which
they had found there and of whose history they were quite ignorant.
While it has no place in this book, the last known migration of
some of the Mayas is interesting and it is certain that a considerable
number emigrated between the years 1450 and 1451 southward to
Lake Peten,[3] where they built a city on an island and there they
survived, together with their ancient culture, until conquered in 1697
by the Spaniards, who destroyed all their temples and books and
perforce made either good Christians or “good Indians” of all the
inhabitants.
Landa says, under the heading, “Various Misfortunes Experienced
in Yucatan in the Century before the Conquest”:
These people had over twenty years of abundance and health and
multiplied greatly. All of the land looked like one town and they built many
temples which can be seen to-day in all parts; and crossing the mountains,
one can see through the leaves of the trees sides of houses and buildings
wonderfully constructed. After all this happiness, one evening in the winter a
wind arose about six o’clock and increased until it became a hurricane of the
Four Winds.[4] This wind tore out the large trees, made a great slaughter of
all kinds of game, tore down all the high houses, which, as they were
thatched with straw and had fire inside against the cold, caught fire. Great
numbers of people were burned and those that escaped were torn to pieces
by falling trees.
This hurricane lasted until noon of the next day. Some who lived in small
houses escaped—the young people who were just married, who were
accustomed to build small houses in front of those of their parents or parents-
in-law, where they lived the first years.
Thus this land then lost its name, which was U-Lumil-Ceh, U-Lumil-Cutz,
Land of the Deer, Land of the Wild Turkey, and was without trees. The trees
now seen all appear to have been planted at the same time, as they are all of
the same height, and, looking at this land from some spot, it seems as though
it had been trimmed off with shears.
Those who escaped felt encouraged to rebuild and cultivate the land and
they again multiplied greatly, having fifteen years of health and good weather
and the last year was the most fruitful of all. At the time of harvest, there
came upon the land some contagious fevers which lasted twenty-four hours.
After the fever the victim would swell up and burst open, being full of worms,
and of this pestilence many people died leaving the fruit ungathered.
After this pestilence there was another sixteen good years in which they
renewed their passions and ravagings. In this way one hundred and fifty
thousand men died in battle. After this massacre they were more calm and
made peace and rested for twenty years. Then came another pestilence.
Large pimples formed and they rotted the body and emitted offensive odors in
a way that the members fell off by pieces within four or five days.
This plague has passed more than fifty years ago, the massacres of the
wars twenty years before that; the pestilence of the swelling and worms
sixteen years before the wars; and the hurricane another sixteen years before
that and twenty-two years after the destruction of Mayapan, which, according
to this record, makes one hundred twenty-five years since the destruction.
Thus by the wars and other punishments which God sent, it is a wonder there
are as many people as are now living, although there are not many.
This quaint account by Landa sheds some light upon the condition
of the Mayas during the century preceding the Spanish invasion and
indicates that the golden age of the race had occurred not many
centuries before.
The legendary history of the coming of the Mayas to Chi-chen Itza
is alluded to by Landa in several passages. He states:
It is the opinion among the Indians that with the Itzas who populated Chi-
chen Itza, there reigned a great man called Kukul Can, and the principal
temple of the city is called Kukul Can. They say he entered from the west,
that he was very genteel, and that he had neither wife nor children. After he
left Chi-chen Itza he was considered in Mexico one of their gods and called
Quetzal Coatl and in Yucatan they also had him for a god.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com