using javafx - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfnodes 12 java.lang.object...

16
Using JavaFX Scene graphs Stage and scenes Core Node classes 1

Upload: others

Post on 16-Apr-2020

28 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Using JavaFX

Scene graphs

Stage and scenes

Core Node classes

1

Page 2: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

History of Java FX

2

§ Java 1.0 (1996)

§ Cross-platform

§ Java wrappers for native widgets

§ In practice, underlying platform differences meant that they looked and behaved differently across platforms

§ Support imperative programming

§ “heavyweight” toolkit

§ Java 1.1 (1998)

§ Cross-platform

§ Java implementations of core widgets

§ Often lower than native widgets, and missing modern features like animations, shading and so on.

§ Support imperative programming

§ “lightweight” toolkit

§ Java 6 (2007)

§ Cross-platform

§ Java implementation of full framework + widgets

§ Competitor w. Adobe Flash; designed for “rich multimedia apps”

§ A “better Swing” with 3D, graphs, more controls.

§ Imperative + declarative programming with GUI builder

§ Hardware acceleration

§ “lightweight” toolkit

Swing

Page 3: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Reasons to use Java FX instead of Swing

3

§ Improvements- Better API for many controls- Layout is greatly simplified- Hardware acceleration makes it more responsive

§ New features- 3D API- Graphics and animation support- Playback of audio and video media- Graphs and charting capabilities- GUI Builder!

Page 4: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Installing JavaFX

4

It’s not included in the Java JDK, so you need to install: https://openjfx.ioYou need to include JavaFK libraries in IntelliJ:

1. In your IntelliJ project, open Project Settings and add libraries.

2. Edit Configurations and add a line to the VM options:

--module-path /Users/javery/lib/javafx-sdk-11.0.2/lib --add-modules javafx.controls, javafx.base, javafx.graphics

Page 5: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Application Workflow

5

Step 0: Main - optional

Step 1: init() - optional

Step 2: start()

Step 3: stop() - optional

Java FX Application base

class contains these methods

This is the only required method.

Derive from Application

Page 6: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

(Full) Hello JavaFX

6

We don’t need a main() method!!

Page 7: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

(Minimal) Hello JavaFX

7

§ JavaFX apps only require a start() method. - main(), init(), stop() are optional- if you extend Application (base class) it will run with just a start() method

HelloFX.java

Page 8: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Key Concept: Scene Graph

8

§ In computer graphics, a scene graph is a tree structure that arranges all ofthe elements of a screen into a hierarchy.- Lets us manage dependencies between objects on the screen- Makes drawing and other operations much more efficient!

Home

Copy-Paste Tools

Paste

Cut

Copy

Slide Tools. . .

Page 9: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Stages and Scenes

9

§ JavaFX composes an interface as a scene graph – a tree where every item has zero or one parent.

§ One “root” node, typically a top-level layout.- Stage is the main window- Scene is the scene-graph that you want to display- Everything in a scene is a Node, ordered in a hierarchy

Scene graph == View hierarchy (other toolkits)

Page 10: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Stage

10

java.lang.Objectjavafx.stage.Window

javafx.stage.Stage

§ Automatically created by the platform

§ Stage is the top-level container, representing the application window.

§ Methods- setMinWidth (double)- setMaxWidth (double)- setResizable (boolean)- setTitle (String)- setScene(Scene)- show()

Stage replaces JFrame from

Swing

Page 11: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Scene

11

java.lang.Objectjavafx.scene.Scene

§ Container for the content in a scene-graph.

§ Must specify the root Node for the scene graph by setting the root property.

§ Methods- setRoot (Parent)- setFill(Paint)- getX()- getY()

Scene replaces any top-level

container from Swing

Page 12: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Nodes

12

java.lang.Objectjavafx.scene.Node

§ Base class for scene graph nodes.

§ These are the displayable objects!

§ Root Nodes- If a Group is used as the root, the contents of the scene graph will be clipped

by the scene's width and height.- If a resizable node (layout Region or Control is set as the root, then the

root's size will track the scene's size, causing the contents to be resized as necessary.

§ Leaf Nodes- Circle, Line, Polygon, Rectangle, Text (javafx.scene.shape)- Button, Choicebox, Label, Slider, Spinner (javafx.scene.control)

Page 13: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Building a UI

13

§ Construct Nodes and add to the scene graph

HelloFX.java

Page 14: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Building a UI (cont’d)

14

§ You can use any of the Node classes when building a UI.

JavaVersion.java

Page 15: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

Managing Scenes

15

§ You can have multiple stages (windows) and multiple scenes for each stage.

§ Stage.setScene() allows you to dynamically load different scenes.

SwitchScenes.java

Page 16: Using JavaFX - student.cs.uwaterloo.cacs349/w20/slides/07.javafx.pdfNodes 12 java.lang.Object javafx.scene.Node §Base class for scene graph nodes. §These are the displayable objects!

What can we draw on a Scene?

16

§ Node is the base class for all Leaf nodes in the Scene Graph- Camera, Canvas, ImageView, LightBase, MediaView, Parent, Shape,

Shape3D, SubScene, SwingNode

§ Graphics Primitives (Shape subclass)- Arc, Circle, CubicCurve, Ellipse, Line, Path, Polygon, Polyline, QuadCurve,

Rectangle, SVGPath, Text

§ Widgets (Control subclass)- Accordion, ButtonBar, ChoiceBox, ComboBoxBase, HTMLEditor, Labeled,

ListView, MenuBar, Pagination, ProgressIndicator, ScrollBar, ScrollPane, Separator, Slider, Spinner, SplitPane, TableView, TabPane, TextInputControl, ToolBar, TreeTableView, TreeView

§ We’ll talk about each of these in order starting with graphics primitives.