JavaFX
JavaFX-Architecture
• complete API with a rich set of classes and interfaces
• to build GUI applications with rich graphics
Packages
• javafx.animation:classes to add transition based animations such as fill,
fade, rotate, scale and translation, to the JavaFX nodes.
• javafx.application: a set of classes responsible for the JavaFX application life
cycle.
• javafx.css: classes to add CSS–like styling to JavaFX GUI applications.
• javafx.event: classes and interfaces to deliver and handle JavaFX events.
• javafx.geometry: classes to define 2D objects and perform operations
• javafx.stage: This package holds the top level container classes for JavaFX
application.
• javafx.scene: This package provides classes and interfaces to support the
scene graph. In addition, it also provides sub-packages such as canvas, chart,
control, effect, image, input, layout, media, paint, shape, text, transform,
web, etc. There are several components that support this rich API of JavaFX.
Scene Graph
• GUI Applications were coded using a Scene Graph.
• starting point of the construction of the GUI Application.
• application primitives that are termed as nodes.
• A node is a visual/graphical object and it may include
• Geometrical (Graphical) objects – (2D and 3D) such as circle, rectangle,
polygon, etc.
• UI controls – such as Button, Checkbox, Choice box, Text Area, etc.
• Containers – (layout panes) such as Border Pane, Grid Pane, Flow Pane, etc.
• Media elements – such as audio, video and image objects. In general, a
collection of nodes makes a scene graph.
• All these nodes are arranged in a hierarchical order as shown below:
• Each node in the scene graph has a single parent,
• node which does not contain any parents is known as the root node.
• every node has one or more children
• node without children is termed as leaf node
• a node with children is termed as a branch node.
• The nodes of a scene graph can have Effects, Opacity, Transforms, Event Handlers, Event
Prism
• high performance hardware
• accelerated graphical pipeline that is used to render the graphics
• both 2-D and 3-D graphics.
• To render graphics
• DirectX 9 on Windows XP and Vista.
• DirectX 11 on Windows 7.
• OpenGL on Mac and Linux,
• Embedded Systems.
• Prism uses the software render path to process the graphics.
GWT(Glass Windowing Toolkit)
• To manage Windows, Timers, Surfaces and Event Queues.
• connects the Platform to the Native Operating System.
Quantum Toolkit
• Abstraction over the low-level components of Prism, Glass, Media
Engine, and Web Engine.
• It ties Prism and GWT together and makes them available to JavaFX.
WebView
• Embed HTML content in to a scene graph.
• used to process this content.
• Web Kit, which is an internal open-source web browser engine
• supports different web technologies
• like HTML5, CSS, JavaScript, DOM and SVG.
• Using WebView
• Render HTML content from local or remote URL.
• Support history and provide Back and Forward navigation.
• Reload the content.
• Apply effects to the web component.
• Edit the HTML content.
• Execute JavaScript commands.
• Handle events.
Media Engine
• based on an open-source engine known as a Streamer.
• supports the playback of video and audio content.
• javafx.scene.media
• Media Object: This represents a media file.
• Media Player: To play media content.
• Media View: To display media.
JavaFX Application Structure
Stage
• contains all the objects.
• javafx.stage.
• Primary stage: created by the platform itself.
• The created stage object is passed as an argument to the start() method of the Application
class
• A stage has two parameters determining its position namely Width and Height.
• It is divided as Content Area and Decorations (Title Bar and Borders).
• There are five types of stages available –
Decorated
Undecorated
Transparent
Unified
Utility
• to call the show() method to display the contents of a stage
Scene
• physical contents of a JavaFX application.
• all the contents of a scene graph.
• javafx.scene
• At an instance, the scene object is added to only one stage.
• create a scene by instantiating the Scene Class.
• size of the scene by passing its dimensions (height and width) along
with the root node to its constructor.
Scene Graph and Nodes
• tree-like data structure (hierarchical) representing the contents of a scene.
• a node is a visual/graphical object of a scene graph.
• A node may include — Geometrical (Graphical) objects (2D and 3D) such
as – Circle, Rectangle, Polygon, etc.
UI Controls such as – Button, Checkbox, Choice Box, Text Area, etc.
Containers (Layout Panes) such as Border Pane, Grid Pane, Flow Pane, etc.
Media elements such as Audio, Video and Image Objects.
• javafx.scene represents a node in JavaFX, this class is the super class of all
the nodes.
Creating a JavaFX Application
• Application Class
• javafx.application is the entry point of the application
• To inherit this class and implement its abstract
method start(). to write the entire code for the JavaFX
graphics
• In the main method, to launch the application using
the launch() method. This method internally calls
the start() method of the Application class as shown in
the following program.
• Step 1: Creating a Class
• Step 2: Creating a Group Object
• Step 3: Creating a Scene Object
• Step 4: Setting the Title of the Stage
• Step 5: Adding Scene to the Stage
• Step 6: Displaying the Contents of the Stage
• Step 7: Launching the Application
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavafxSample extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
//creating a Group object
Group group = new Group();
//Creating a Scene by passing the group object, height and width Scene scene = new
Scene(group ,600, 300);
//setting color to the scene
scene.setFill(Color.BROWN);
//Setting the title to Stage.
primaryStage.setTitle("Sample Application"); //Adding the scene to Stage
primaryStage.setScene(scene); //Displaying the contents of the stage
primaryStage.show(); } public static void main(String args[]){ launch(args); } }