Mastering JavaFX 10
eBook - ePub

Mastering JavaFX 10

Build advanced and visually stunning Java applications

Sergey Grinev

Compartir libro
  1. 268 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Mastering JavaFX 10

Build advanced and visually stunning Java applications

Sergey Grinev

Detalles del libro
Vista previa del libro

Información del libro

Design modern, rich interfaces for Java apps using JavaFX 10About This Book• Become a pro with the latest JavaFX 10 framework• Create dynamic content using the animation API• Create and customize plugins and use them efficiently in different applicationsWho This Book Is ForIf you're a Java developer who wants to upgrade to the latest version of JavaFX to create stunning, feature-rich graphical applications, this book is for you.What You Will Learn• Construct and customize JavaFX windows• Manage UI elements and arrange them on the Scene• Explore the Bindings API and use it to coordinate various UI elements• Use FXML to design amazing FX applications• Write and manage CSS to style your applications• Add audio and video to your projects• Prepare your application to be launched on the target platformIn DetailJavaFX 10 is used to create media-rich client applications. This book takes you on a journey to use JavaFX 10 to build applications that display information in a high-performance, modern user interface featuring audio, video, graphics, and animation.Mastering JavaFX 10 begins by introducing you to the JavaFX API. You will understand the steps involved in setting up your development environment and build the necessary dependencies. This is followed by exploring how to work with the assets, modules, and APIs of JavaFX. This book is filled with practical examples to guide you through the major features of JavaFX 10. In addition to this, you will acquire a practical understanding of JavaFX custom animations, merging different application layers smoothly, and creating a user-friendly GUI with ease.By the end of the book, you will be able to create a complete, feature-rich Java graphical application using JavaFX.Style and approachThe book adopts a practical, step-by-step approach.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Mastering JavaFX 10 un PDF/ePUB en línea?
Sí, puedes acceder a Mastering JavaFX 10 de Sergey Grinev en formato PDF o ePUB, así como a otros libros populares de Ciencia de la computación y Programación en Java. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.



Building Blocks – Shapes, Text, and Controls

To construct a rich user interface, you need the building blocks. JavaFX provides a large range of very customizable graphical instruments. We'll start from the smallest building blocks—shapes, text, and simple controls—and will use them to understand how JavaFX works with graphical elements.
In this chapter, we will cover the following topics
  • Creating and customizing the shapes
  • Working with text
  • Coordinates and bounds
  • Basic controls
By combining and customizing these Nodes and arranging them using the layout managers we reviewed in the previous chapter, you can already build a sophisticated UI. At the end of the chapter, we'll revisit the Clock application from the previous chapter to demonstrate the topics we learned in a more complex application.

Shapes and their properties

Everything you see on the screen of your computer can be split into three large categories:
  • Shapes
  • Text
  • Images
Thus, by being able to create each of these, you can build any UI in reasonable time. Let's start with JavaFX shapes.

JavaFX shapes overview

The simplest object you can put on a scene is a shape. Under the javafx.scene.shape package, the JavaFX API supports a great range of shapes, from circles and rectangles to polygons and SVG paths. Most shapes can be divided then into two categories—lines and closed shapes. The properties that are shared among all shapes will be covered in the next section. After, we will review each shape's specific APIs.

Closed shapes

There are just four options here—Rectangle, Circle, Ellipse, and Polygon. They mostly don't have any special API, just a minimum required by basic math to describe their form.
The only small difference is Rectangle, which can have rounded corners, controlled by the setArcHeight() and setArcWidth() methods.
For the polygon, you need to provide the coordinates of each vertex through the getPoints() method.
For example, take a look at the following code:
 // chapter2/shapes/
Rectangle rect = new Rectangle(50,50);

Circle circle = new Circle(50);

Ellipse ellipse = new Ellipse();

Polygon polygon = new Polygon();
0.0, 0.0,
50.0, 30.0,
10.0, 60.0);

// adding 4 shapes to the scene
HBox hbox = new HBox(20);
hbox.setPadding(new Insets(20));
hbox.getChildren().addAll(rect, circle, ellipse, polygon);
primaryStage.setScene(new Scene(hbox, 500, 150));
You can match all four shapes on this screenshot:
Note that you can have crossing edges for the polygon. JavaFX will do its best to determine which parts of such polygons are internal, judging by the starting point:
0., 0.,
50., 0.,
0., 50.,
50., 50.,
30., -10.,
The preceding code draws the following shape:


Line is as simple as it sounds. You write start and end coordinates, and they get connected:
Line line = new Line(10, 10, 100, 50);
Polyline is a set of consecutive lines. You need to provide several pairs of coordinates where the end of each line is the start of the next one. Make sure you are providing an even number of parameters:
// chapter2/shapes/
Polyline polyline = new Polyline();
0.0, 0.0,
50.0, 30.0,
10.0, 60.0);
Note that despite not always having a full border, line-type shapes can have a background. If you assign it using the setFill() method, these shapes will use invisible edge, connecting the first and last points of the line. Here is an example of the same polylines with and without a background:
The third shape is a Polygon with the same points set. The only difference between Polygon and Polyline is that the former automatically adds a line between the first and the last points to create a closed figure.


Arc is a piece of the ellipse. It has the extra startAngle and length properties compared to Ellipse. Both these parameters are measured in degrees, ranging from 0 to 360.
The following is an example of an ellipse and a similar arc with a length of 180 degrees:
// chapter2/shapes/
Ellipse ellipse = new Ellipse();

Arc arc = new Arc();
QuadCurve and CubicCurve represent quadratic and cubic Bezier parametric curves. This is a popular method for modeling a smooth curve.
To draw a QuadCurve, you need to set a start point, an end point, and a control point for the curve. After that, JavaFX will draw a curve by shifting tangent with vertexes on the...