Beautiful User Interfaces with JavaFX

Similar documents
CST141 JavaFX Basics Page 1

Java Programming Layout

Chapter 14 JavaFX Basics. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Building Graphical user interface using JavaFX

Week 5: Images & Graphics. Programming of Interactive Systems. JavaFX Images. images. Anastasia Bezerianos. Anastasia Bezerianos

Essential JavaFX. Using layouts. Panes in JavaFX. Layouts. Tobias Andersson Gidlund LAYOUTS

JavaFX Technology Building GUI Applications With JavaFX - Tutorial Overview

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.

Composite Pattern Diagram. Explanation. JavaFX Subclass Hierarchy, cont. JavaFX: Node. JavaFX Layout Classes. Top-Level Containers 10/12/2018

C14: JavaFX: Overview and Programming User Interface

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Event-Driven Programming with GUIs. Slides derived (or copied) from slides created by Rick Mercer for CSc 335

Advanced Java for Bioinformatics Winter 2017/18. Prof. Daniel Huson

COMP6700/2140 Scene Graph, Layout and Styles

46 Advanced Java for Bioinformatics, WS 17/18, D. Huson, December 21, 2017

CON Visualising GC with JavaFX Ben Evans James Gough

C12: JavaFX Scene Graph, Events, and UI Components

JavaFX Basics. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.

C15: JavaFX: Styling, FXML, and MVC

CS 4300 Computer Graphics

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Week 12 Thursday. For milestone #2 and #3, also submit a screenshot of your GUI once it is launched.

Java Programming Hello FX

Multimedia-Programmierung Übung 3

5 Drawing Stuff in 2D

Graphical User Interface (GUI)

Graphical User Interface (GUI)

Computational Expression

Java Foundations. 9-1 Introduction to JavaFX. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Java FX 2.0. Dr. Stefan Schneider Oracle Deutschland Walldorf-Baden

C16a: Model-View-Controller and JavaFX Styling

Event Handling in JavaFX

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2015/2016

JavaFX. Working with Layouts in JavaFX Release 8 E

Multimedia-Programmierung Übung 4

Java FX GUI. Pieter van den Hombergh. May 31, Fontys Hogeschool voor Techniek en Logistiek. Java FX GUI. Pieter van den Homb

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Java Programming Lecture 6

Window Interfaces Using Swing Objects

//Create BorderPane layout manager. layout = new BorderPane(); //This is the "root node".

JavaFX. Getting Started with JavaFX Scene Builder Release 1.1 E

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2016/2017

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

<Insert Picture Here> JavaFX 2.0


c 2017 All rights reserved. This work may be distributed or shared at no cost, but may not be modified.

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2017/2018

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

IT In the News. Login tokens have been reset for those affected and vulnerabilities have been fixed. o Vulnerabilities existed since July 2017

JAVAFX 101 [CON3826]

light side dark side canoo

Microsoft PowerPoint The Microsoft PowerPoint 2000 Screen. Leander ISD Technology Services CMB Title.

interactive systems graphical interfaces Week 2 : a. Intro to JavaFX Programming of Interactive Systems

Widget. Widget is a generic name for parts of an interface that have their own behaviour. e.g., buttons, progress bars, sliders, drop-down

CS Exam 1 Review Suggestions

Starting Excel application

C30c: Model-View-Controller and Writing Larger JavaFX Apps

Introduction to the JAVA UI classes Advanced HCI IAT351

CS 465 Program 4: Modeller

COMP6700/2140 GUI and Event Driven Programming

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CST141 JavaFX Events and Animation Page 1

CSCI-142 Exam 2 Review September 25, 2016 Presented by the RIT Computer Science Community

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

UI Elements. If you are not working in 2D mode, you need to change the texture type to Sprite (2D and UI)

Graphical User Interfaces (GUIs)

Window Interfaces Using Swing Objects

CS 116x Winter 2015 Craig S. Kaplan. Module 03 Graphical User Interfaces. Topics

Event-Driven Programming

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Java Programming Lecture 7

Java: Graphical User Interfaces (GUI)

Creating Presentations with Prezi

Oracle JavaFX. JavaFX Scene Builder User Guide Release 1.1 E

1. (5 points) In your own words, describe what an instance is.

CS 251 Intermediate Programming GUIs: Components and Layout

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

(Incomplete) History of GUIs

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

1. New document, set to 5in x 5in, no bleed. Color Mode should be default at CMYK. If it s not, changed that when the new document opens.

Wednesday, November 16, 11

Working with PDF s. To open a recent file on the Start screen, double click on the file name.

COMP1406 Tutorial 5. Objectives: Getting Started: Tutorial Problems:

Educational Fusion. Implementing a Production Quality User Interface With JFC

Table Basics. The structure of an table

Microsoft Word

ver Wfl Adobe lif Sams Teach Yourself Betsy Bruce Robyn Ness SAMS 800 East 96th Street, Indianapolis, Indiana, USA WlM John Ray ^lg^

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Fall UI Design and Implementation 1

Text and Lists Use Styles. What Are Styles?

GUI Software Architecture

GUI Event Handlers (Part I)

Spatial Data Structures

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

JavaFX Scene Builder

XPEL DAP SUPPORT. DAP Tool List & Overview DESCRIPTION ICON/TOOL (SHORTCUT)

COMP3421 Computer Graphics

Transcription:

Beautiful User Interfaces with JavaFX Systémes d acquisition 3EIB S. Reynal September 20, 2017 The current document is dedicated to giving you a small and quick insight into the JavaFX API, an extra Java library that will allow you to build beautifully styled user interfaces (UI). JavaFX has become a neat, modern and efficient replacement to the Swing API (the JFrame, JButton, etc. components), an API that surfaced more than ten years ago as an attempt to create the first Java cross-platform widget 1 kit and a replacement to the native AWT widget kit. If you have been used to Swing components, you might find the philosophy behind JavaFX quite familiar at first, but this is merely the tip of the iceberg! JavaFX can do much more than Swing does, from fluid animations to CSS style sheet based styling to 3D shapes and transforms. Above all, JavaFX is hardware accelerated on most platform, either through DirectX on Windows, or through OpenGL on OS X or Linux. Building a User Interface in JavaFX revolves mostly around creating a component tree made of nodes (branches and leaves), where leaves can be widgets (buttons and co), shapes (lines, rectangle, etc), text (with tons of styling options), images, sound or videos, charts and tables, and branches are actually groups thereof (e.g., menu, toolbars, complex drawings), see Figure 1. There are also special nodes that support dynamic processing, like transforms (rotations, translations) and animation effects (e.g., you want to make a 3D box rotate for 3 seconds), and every visible node can have an event listener attached to it, just as was already the case with Swing. Finally, every component in the tree supports a CSS based styling scheme, with the usual classes and id parameters of CSS styling. This makes it possible to split appearance issues from functionality issues, and have someone specialized in graphic design take care of the UI appearance while developers are busy designing the interface itself. 1 A widget is a component of a user interface like a button, a menu or a toolbar. 1

Figure 1: JavaFX component graph: the upper node is the root of the tree. It has three children, a Shape3D, a Shape2D, and a parent of a Region, a group of nodes which itself aggregates together a Pane and a Control (e.g., a Button or a TextField). Applying a transform to a Region applies the transform to each of its children in turn, so that this is very efficient in terms of performance. 2

1 A basic application Writing a JavaFX application begins with overriding the start() method of the javafx.application.application class. The argument of the start() method is a Stage object which represents the primary stage of the UI. You can have several stages: these simply correspond to distinct windows. Every Stage has a title (which is displayed in the window upper bar) and a Scene object attached to it, where the Scene is somehow the top of the component UI tree. A Scene has a dimension, a location on screen, and contains a root component (a BorderPane in the example given in Fig. 1) which can itself have children if it is a branch node (containers are always branch nodes so this is ok). Finally, a Scene can have one or several CSS style sheets attached to it. 2 Displaying text, images, 2D and 3D shapes You can easily add text and 2D or 3D geometrical shapes to your interface by creating the appropriate object (e.g., a Line) and adding it to a parent container. This container may either be a : Group (in which case it just acts as a way to maintain a link between every node belonging to it, for example, if you add a Transform to a Group, then every node in the group undergoes this transform) Or a sophisticated container, like e.g. BorderPane, that can also layout its children neatly on the screen, in particular because it takes care of how its dimension influences the layout. Figure 3 shows a basic code block for adding text. Literally every parameter in the Text object can be tweaked, from font size to stroke color and width. Changes are reflected immediately on the screen through a hidden event signaling mechanism. Styling can also be specified using the associated CSS style sheet, using the.text class. The package javafx.scene.shape contains a lot of 2D shapes, from lines to circles to Bezier splines. It also includes classes dedicated to 3D graphics (boxes, spheres, cylinders) which, when associated with transforms (see package javax.scene.transform), makes it possible to build complicated 3D worlds. More information is available in the API document of javafx, http://docs.oracle.com/javase/8/javafx/api/toc.htm. Figure 4 shows how to add a single line to your interface. Here again you may either want to add it to a parent group, or to a more sophisticated container. Finally, it is also possible to add images, videos or sound directly to your scene by creating the appropriate objects and wrapping them into a parent 3

import j a v a f x. animation. A p p l i c a t i o n ; p u b l i c c l a s s Main extends A p p l i c a t i o n { p u b l i c void s t a r t ( Stage primarystage ) { try { BorderPane r o o t = new BorderPane ( ) ; root. settop ( createtoolbar ( ) ) ; root. setbottom ( c r e a t e S t a t u s b a r ( ) ) ; root. setcenter ( createmaincontent ( ) ) ; Scene scene = new Scene ( root, 8 0 0, 4 0 0 ) ; scene. g e t S t y l e s h e e t s ( ). add ( g e t C l a s s ( ). getresource ( a p p l i c a t i o n. c s s ). toexternalform ( ) ) ; primarystage. s e t S c e ne ( scene ) ; primarystage. s e t T i t l e ( Demo JavaFX ) ; primarystage. show ( ) ; catch ( Exception e ) { e. printstacktrace ( ) ; p r i v a t e Node createtoolbar ( ) { return new ToolBar ( new Button ( New ), new Button ( Open ), new Separator ( ), new Button ( Clean ), e t c ) ; p r i v a t e Node c r e a t e S t a t u s b a r ( ) { HBox s t a t u s b a r = new HBox ( ) ; s t a t u s b a r. getchildren ( ). addall (new Label ( Name : ), new TextField ( ) e t c. ) ; return s t a t u s b a r ; p r i v a t e Node createmaincontent ( ) { Group g = new Group ( ) ; // here you f i l l g with whatever Nodes you want // using g. getchildren ( ). add (... ) return g ; e t c. Figure 2: Building the component UI tree is simple: create a root node (here a BorderPane), fill its various inner areas (top, bottom, center), then attach it to a Scene and attach the Scene to the Stage. 4

Text t e x t = new Text ( Text can\ n s t r a d d l e two l i n e s ) ; t e x t. settextalignment ( TextAlignment. JUSTIFY ) ; t e x t. setfont ( Font. f o n t ( Times New Roman, FontWeight.BOLD, FontPosture.REGULAR, 4 0 ) ) ; t e x t. s e t F i l l ( Color.CYAN) ; t e x t. setstrokewidth ( 2 ) ; t e x t. s e t S t r o k e ( Color.BLACK) ; t e x t. s e t U n d e r l i n e ( true ) ; t e x t. setx ( 5 0 ) ; t e x t. sety ( 1 5 0 ) ;... ( then add to parent group ) Figure 3: Adding text to your interface reduces to instanciating a Text object, setting its font, location, stroking and alignement properties, and adding it to a Group or any other type of parent node (e.g., a Borderpane). Line l i n e = new Line ( ) ; l i n e. setstartx ( 3 0 0. 0 ) ; l i n e. setstarty ( 1 5 0. 0 ) ; l i n e. setendx ( 5 0 0. 0 ) ; l i n e. setendy ( 1 5 0. 0 ) ; l i n e. s e t S t r o k e ( Color.GREEN) ; Figure 4: Creating geometrical shapes is not much complicated: create a shape using one of the many classes available in the javafx.geometry package, and add it to a parent container. group. Adding an image is quite straightforward, and is exemplified in Figure 5. Just create an Image with the appropriate image URL (many format are supported, but PNG is the preferred one), wrap it into an ImageView (which is the node responsible for rendering the image to the screen), set the ImageView parameters so that the image has the correct location and size on screen, possible add an effect to the image by attaching an effect object (see javafx.scene.effect package), and finally attache the ImageView to a parent group, as always. 3 Adding widgets and handling events Figure 6 and 7 show how to implement a simple UI with widgets (buttons, menus, etc) and how to handle events triggered on them by a mouse click. The approach is very similar to Swing, since in Swing components were already handled by a component graph made up of leaves and nodes. 5

Image image = new Image ( u r l o f PNG image ) ; ImageView imageview = new ImageView ( image ) ; imageview. setx ( 5 0 0 ) ; imageview. sety ( 7 0 ) ; imageview. s e t F i t H e i g h t ( 2 0 0 ) ; imageview. setfitwidth ( 4 0 0 ) ; Glow glow = new Glow ( ) ; glow. s e t L e v e l ( 0. 9 ) ; imageview. s e t E f f e c t ( glow ) ; Figure 5: To add an image to the UI, create an Image object, wrap it into an ImageView, possibly add visual effets like Glow (or any other in the javafx.scene.effect package), and finally add the ImageView to a parent container. import j a v a f x. scene. input. MouseEvent ;... node. setonmouseclicked ( new EventHandler<MouseEvent >() { p u b l i c void handle ( MouseEvent e ) { System. out. p r i n t l n ( mouse c l i c k! ) ; ) ; Figure 6: Handling events in JavaFX: it is enough to just attach an event handler to a (visible) node to capture any event occuring on this node (a button, a shape, a piece of text, etc). Those used to Swing will surely recognize the familiar Button and Grid- Pane objects: as it turns out they work pretty much like in Swing and you can seamlessly translate a piece of Swing code into a piece of JavaFX code with minimum effort. Mouse and keyboard events are handled using EventHandler objects with appropriate filters (here a MouseEvent filter), and bear strong resemblance with Swing s ActionListener s. 6

Text txt = new Text ( Password ) ; PasswordField p a s s F i e l d = new PasswordField ( ) ; Button button = new Button ( Submit ) ; GridPane g ridpa ne = new GridPane ( ) ; gridpane. setminsize (400, 2 0 0 ) ; gridpane. setpadding ( new I n s e t s (10, 10, 10, 1 0 ) ) ; gridpane. setvgap ( 5 ) ; gridpane. sethgap ( 5 ) ; gridpane. setalignment ( Pos.CENTER) ; gridpane. add ( txt, 0, 1 ) ; gridpane. add ( passfield, 1, 1 ) ; gridpane. add ( button, 0, 2 ) ; button. s e t S t y l e ( fx background c o l o r : d a r k s l a t e b l u e ) ; Figure 7: Basic steps for creating a UI with buttons and textfields. 7