JavaFX. Working with the JavaFX Scene Graph Release 8 E March 2014 Learn about the concept of a scene graph and how it is used in JavaFX.

Similar documents
Oracle Banking Channels Bank User Base

JavaFX. JavaFX System Requirements Release E

Oracle Utilities Advanced Spatial and Operational Analytics

Defining Constants and Variables for Oracle Java CAPS Environments

Oracle Enterprise Data Quality for Product Data

Oracle Utilities Work and Asset Management Integration to Primavera P6 Enterprise Project Portfolio Management

Copyright 1998, 2009, Oracle and/or its affiliates. All rights reserved.

Oracle Linux. UEFI Secure Boot Signing Key Update Notice

Microsoft Active Directory Plug-in User s Guide Release

Oracle Communications Configuration Management

Oracle Fusion Middleware

Oracle Hospitality OPERA Exchange Interface Cloud Authentication. October 2017

Microsoft Internet Information Services (IIS) Plug-in User s Guide Release

Oracle Cloud E

1 Understanding the Cross Reference Facility

New Features in Primavera Professional 15.2

PeopleSoft Fluid Icon Standards

Oracle Cloud Using Oracle E-Business Suite Adapter Endpoint Configuration Wizard. Release 17.3

Oracle SQL Developer Web Accessibility Guide. Release 18.1

Database Change Reference Release 6.3

Release for Microsoft Windows

Oracle Database Appliance Accessibility Guide. Release

Oracle Fusion Middleware Known Issues in Oracle Stream Analytics

Oracle Fusion Middleware Creating Domain Templates Using the Domain Template Builder. 12c ( )

Oracle NoSQL Database Integration with SQL Developer. Release 18.1

Oracle Cloud Known Issues for Trial and Paid Subscriptions. Release 18.1

Oracle Virtual Desktop Client for ipad. Release Notes for Release 1.2

Importing an SNA Custom Handshake Class

Oracle Utilities Opower Custom URL Configuration

Microsoft.NET Framework Plug-in User s Guide Release

Oracle Utilities Smart Grid Gateway

PeopleSoft Fluid Required Fields Standards

What s New for Cloud at Customer What's New for the Cloud Services on Oracle Cloud at Customer New Documentation for Oracle Cloud at Customer

Oracle Enterprise Manager Ops Center. Introduction. What You Will Need

Oracle Cloud Getting Started with Oracle WebCenter Portal Cloud Service

JD Edwards EnterpriseOne. Overview. Prerequisites. Web Client for ipad Quick Start Guide Release 8.98 Update 4, Service Pack 5

Oracle Fusion Middleware

Oracle Agile Product Lifecycle Management for Process

Oracle Communications Convergent Charging Controller. Sample Message Flows Reference Guide Release 6.0.1

JavaFX. JavaFX 2.2 System Requirements Release 2.2 E

What s New for Oracle Cloud Stack Manager. Topics: July Oracle Cloud. What's New for Oracle Cloud Stack Release

JavaFX. JavaFX System Requirements Release E

JavaFX. Oracle JavaFX Applying Transformations in JavaFX Release 2.1 E

Oracle Identity Manager Connector Guide for Dropbox. Release

JavaFX. Oracle JavaFX Creating Visual Effects in JavaFX Release 2.1 E

Oracle Enterprise Manager

Oracle Hospitality MICROS Commerce Platform Release Notes Release Part Number: E December 2015

Oracle Utilities Mobile Workforce Management

JavaFX. Using Image Ops Release 2.2 E

Oracle Hospitality Suite8 Export to Outlook User Manual Release 8.9. July 2015

Report Management and Editor!

Solution Explorer Guide Release 9.2

Taleo Enterprise Deep Linking Configuration Guide Release 17

JD Edwards World. Service Enablement Guide Release A9.3 E

JavaFX. JavaFX Overview Release E

Recipe Calculation Survey. Materials Control. Copyright by: MICROS-FIDELIO GmbH Europadamm 2-6 D Neuss Date: August 21 st 2007.

Oracle Database Firewall. 1 Downloading the Latest Version of This Document. 2 Known Issues. Release Notes Release 5.

Materials Control. Account Classes. Product Version Account Classes. Document Title: Joerg Trommeschlaeger

Oracle Hospitality Query and Analysis Languages and Translation Configuration Guide. March 2016

Oracle Hospitality BellaVita Hardware Requirements. June 2016

Oracle Fusion Middleware

Oracle Enterprise Manager Ops Center. Introduction. What You Will Need. Creating vservers 12c Release 1 ( )

General Security Principles

Oracle Human Capital Management Cloud Using the HCM Mobile Application. Release 13 (update 18C)

Oracle Retail MICROS Stores2 Functional Document Sales - Receipt List Screen Release September 2015

Oracle. Field Service Cloud Using the Parts Catalog

Insbridge Enterprise Rating Portal Configuration Guide

Oracle Enterprise Manager Ops Center. Introduction. Creating Oracle Solaris 11 Zones Guide 12c Release 1 ( )

Security Guide Release 4.0

IBM WebSphere Portal Reference Guide Release 9.2.x

Oracle SQL Developer Data Modeler Accessibility Guide. Release 18.1

Managing Zone Configuration

Oracle Insurance. Implementing a. Release 5.6

Oracle WebCenter Portal. Starting Points for Oracle WebCenter Portal Installation

Oracle. Sales Cloud Using Sales for Outlook. Release 13 (update 18A)

Oracle Agile Product Lifecycle Management for Process Reporting User Guide Release E

Oracle Cloud E

Export generates an empty file

Oracle Enterprise Manager

Oracle SQL Developer Accessibility Guide. Release 18.1

Oracle Enterprise Manager Ops Center. Introduction. What You Will Need. Installing and Updating Local Software Packages 12c Release

Oracle Cloud Using the Google Calendar Adapter. Release 17.3

Oracle Utilities Customer Care and Billing

Oracle Fusion Middleware Oracle Technology Adapters Release Notes. 12c ( )

OKM Key Management Appliance

Oracle Enterprise Manager Ops Center. Introduction. What You Will Need. Hardware and Software Configuration

Oracle Fusion Middleware Oracle Cloud Adapters Postinstallation Configuration Guide. 12c Release ( )

Oracle Agile Product Lifecycle Management for Process Content Synchronization and Syndication User Guide Release E

Oracle Cloud. Using the Google Calendar Adapter Release 16.3 E

Oracle Banking Channels Bank User Base

Configuring Oracle Java CAPS Master Index (Repository) Connectivity and Environments

JD Edwards EnterpriseOne Licensing

Oracle. Field Service Cloud Using Android and ios Mobile Applications 18B

Oracle Database Mobile Server

Oracle Cloud What's New for Oracle WebCenter Portal Cloud Service

Oracle Utilities Customer Care and Billing

Oracle Utilities Work and Asset Management

Oracle Cloud Using the Google Calendar Adapter with Oracle Integration

Oracle Banking Digital Experience

Oracle Fusion Middleware

Transcription:

JavaFX Working with the JavaFX Scene Graph Release 8 E50683-01 March 2014 Learn about the concept of a scene graph and how it is used in JavaFX.

JavaFX Working with the JavaFX Scene Graph Release 8 E50683-01 Copyright 2011, 2014, Oracle and/or its affiliates. All rights reserved. Primary Author: Scott Hommel Contributing Author: Contributor: This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software License (December 2007). Oracle America, Inc., 500 Oracle Parkway, Redwood City, CA 94065. This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark licensed through X/Open Company, Ltd. This software or hardware and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services.

Contents 1 Working with the JavaFX Scene Graph Overview... 1-1 Exploring the API... 1-2 iii

iv

Preface This preface describes the document accessibility features and conventions used in this tutorial - Working with the JavaFX Scene Graph. Audience This document is intended for JavaFX developers. Documentation Accessibility For information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc. Access to Oracle Support Oracle customers have access to electronic support through My Oracle Support. For information, visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=info or visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=trs if you are hearing impaired. Related Documents Conventions For more information, see the following documents in the JavaFX documentation set: What Is JavaFX? Getting Started with JavaFX The following text conventions are used in this document: Convention boldface italic monospace Meaning Boldface type indicates graphical user interface elements associated with an action, or terms defined in text or the glossary. Italic type indicates book titles, emphasis, or placeholder variables for which you supply particular values. Monospace type indicates commands within a paragraph, URLs, code in examples, text that appears on the screen, or text that you enter. v

vi

1 1Working with the JavaFX Scene Graph This tutorial explores the JavaFX Scene Graph Application Programming Interface (API), the underlying framework that renders your graphical user interfaces (GUI) to the screen. If you are an experienced Java developer, then chances are good that at some point you have created an application with a graphical user interface. This could be anything from small programs in web pages, to standalone Swing applications on the desktop. And if you have ever done any custom painting, you are familiar with the Graphics class and its related methods. The traditional approaches as powerful as they are have always required some amount of effort on the developer's part to correctly render the graphics to the screen. This work is often separate from the bulk of the application logic. Overview The JavaFX Scene Graph API makes graphical user interfaces easier to create, especially when complex visual effects and transformations are involved. A scene graph is a tree data structure, most commonly found in graphical applications and libraries such as vector editing tools, 3D libraries, and video games. The JavaFX scene graph is a retained mode API, meaning that it maintains an internal model of all graphical objects in your application. At any given time, it knows what objects to display, what areas of the screen need repainting, and how to render it all in the most efficient manner. Instead of invoking primitive drawing methods directly, you instead use the scene graph API and let the system automatically handle the rendering details. This approach significantly reduces the amount of code that is needed in your application. The individual items held within the JavaFX scene graph are known as nodes. Each node is classified as either a branch node (meaning that it can have children), or a leaf node (meaning that it cannot have children). The first node in the tree is always called the root node, and it never has a parent. See a general inheritance diagram in Figure 1 1. Working with the JavaFX Scene Graph 1-1

Exploring the API Figure 1 1 Root, Branch, and Leaf Nodes The JavaFX API defines a number of classes that can act as root, branch or leaf nodes. When substituted with actual class names, this same figure might resemble that shown in Figure 1 2 in a real application. Figure 1 2 Specific Root, Branch, and Leaf Classes In Figure 1 2, a Group object acts as the root node. The Circle and Rectangle objects are leaf nodes, because they do not (and cannot) have children. The Region object (which defines an area of the screen with children than can be styled using CSS) is a branch node that contains two more leaf nodes (Text and ImageView). Scene graphs can become much larger than this, but the basic organization that is, the way in which parent nodes contain child nodes is a pattern that repeats in all applications. Exploring the API So what does this all mean in terms of code? Let us start by setting up a basic application frame, populated with only the root node, as shown in Example 1 1. Example 1 1 package scenegraphdemo; Creating the Application Frame import javafx.application.application; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.paint.color; import javafx.stage.stage; public class Main extends Application { 1-2 JavaFX Working with the JavaFX Scene Graph

Exploring the API @Override public void start(stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 500, Color.BLACK); stage.settitle("javafx Scene Graph Demo"); stage.setscene(scene); stage.show(); public static void main(string[] args) { launch(args); This code produces a window that looks like what is shown in Figure 1 3. Figure 1 3 Creating a Scene with Root Node Only The important points to consider are as follows: 1. The Main class is an extension of the javafx.application.application class. Its start method is overridden and receives a Stage object (a top-level GUI container) as its only parameter. 2. The root node (in this case, an instance of the javafx.scene.group class) is created and passed to the scene's constructor, along with the scene's width, height, and fill. 3. The stage's title, scene, and visibility are all set. 4. The main method invokes the Application.launch() method. The resulting application appears as it does because black is the scene's fill color. Because the root node currently has no children, there is nothing else to display. Adding a child to the root node can be accomplished with the modifications shown in Working with the JavaFX Scene Graph 1-3

Exploring the API Example 1 2. Example 1 2 Adding a Leaf Node package scenegraphdemo; import javafx.application.application; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.paint.color; import javafx.scene.shape.rectangle; import javafx.stage.stage; public class Main extends Application { @Override public void start(stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 500, Color.BLACK); Rectangle r = new Rectangle(25,25,250,250); r.setfill(color.blue); root.getchildren().add(r); stage.settitle("javafx Scene Graph Demo"); stage.setscene(scene); stage.show(); public static void main(string[] args) { launch(args); With the change shown in Example 1 2, a blue rectangle (leaf node) that is 250x250 pixels will appear at the specified X and Y coordinates. (By default, X increases from left to right, and Y increases from top to bottom. This can be affected by transformations, however.) Figure 1 4 shows the result of adding a leaf node. 1-4 JavaFX Working with the JavaFX Scene Graph

Exploring the API Figure 1 4 Adding a Leaf Node Because the graphical objects are managed by the scene graph, you can achieve some interesting effects with very little extra code. For example, you could easily animate the rectangle to bounce back and forth across the screen while rotating, changing its size, and transitioning its color from blue to red. Example 1 3 uses transitions to make this happen: Example 1 3 package scenegraphdemo; Animating the Scene import javafx.animation.filltransition; import javafx.application.application; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.paint.color; import javafx.scene.shape.rectangle; import javafx.stage.stage; import javafx.animation.timeline; import javafx.animation.paralleltransition; import javafx.animation.rotatetransition; import javafx.animation.scaletransition; import javafx.animation.translatetransition; import javafx.util.duration; public class Main extends Application { @Override public void start(stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 500, Color.BLACK); Rectangle r = new Rectangle(0, 0, 250, 250); Working with the JavaFX Scene Graph 1-5

Exploring the API r.setfill(color.blue); root.getchildren().add(r); TranslateTransition translate = new TranslateTransition(Duration.millis(750)); translate.settox(390); translate.settoy(390); FillTransition fill = new FillTransition(Duration.millis(750)); fill.settovalue(color.red); RotateTransition rotate = new RotateTransition(Duration.millis(750)); rotate.settoangle(360); ScaleTransition scale = new ScaleTransition(Duration.millis(750)); scale.settox(0.1); scale.settoy(0.1); ParallelTransition transition = new ParallelTransition(r, translate, fill, rotate, scale); transition.setcyclecount(timeline.indefinite); transition.setautoreverse(true); transition.play(); stage.settitle("javafx Scene Graph Demo"); stage.setscene(scene); stage.show(); public static void main(string[] args) { launch(args); While these examples may be simple, they identify and demonstrate some important concepts that you will use in most graphical applications. The javafx.scene package defines more than a dozen classes, but three in particular are most important when it comes to learning how the API is structured: Node: The abstract base class for all scene graph nodes. Parent: The abstract base class for all branch nodes. (This class directly extends Node). Scene: The base container class for all content in the scene graph. These base classes define important functionality that will subsequently be inherited by subclasses, including paint order, visibility, composition of transformations, support for CSS styling, and so on. You will also find various branch node classes that inherit directly from the Parent class, such as Control, Group, Region, and WebView. The leaf node classes are defined throughout a number of additional packages, such as javafx.scene.shape and javafx.scene.text. 1-6 JavaFX Working with the JavaFX Scene Graph