UP | HOME
2015-10-14 | ../../ | Slides

JavaFX Intro

Table of Contents

1 Abstract

JavaFX is the GUI building mechanism for Java applications. It replaces Swing. Other alternatives are libGDX and Unity. JavaFX GUI uses threads, lambda expressions, etc.

2 JavaFX Examples

2.1 Trivial Example 1

  1. ./javaFxApp1.java Blank pane
  2. javac javaFxApp1.java; java javaFxApp1 is the simplest way to run this

2.2 Not So Trivial Example 1

  1. ./javafxGrid.java Grid of Rectangles
  2. Relevant to the grid of Game of Life

2.3 Examples From the Web

  1. http://www.dummies.com/how-to/content/how-to-create-lines-and-shapes-in-javafx.html
  2. http://www.java2s.com/Tutorials/Java/JavaFX/0040__JavaFX_Line.htm similar to my Java Whiteboard
  3. http://www.cs.armstrong.edu/liang/intro10e/html/TicTacToe.html Y. Daniel Liang, author of "Introduction to Java Programming", 10th Edition
  4. https://www.safaribooksonline.com/library/view/javafxtm-developing-rich/9780137013524/ch13.html Eric Bruno; Jim Connors; Jim Clarke, "JavaFX: Developing Rich Internet Applications", RIA, Addison-Wesley Professional, 2009. Safari On-Line. We can read through WSU Library proxy.
  5. https://github.com/hallvard/javafx/blob/master/games/src/games/sudoku/ Not sure how good this is.

3 JavaFX Overview

  1. Graphics, UI, video, audio, etc.
  2. Java APIs, integrated with Java 8; Not to be confused with JavaFX 1.x, which is a scripting language, not a Java API
  3. Effective JavaFX: Understand concurrency
    1. ../Concurrency/concurrency-fundas.html
    2. ../Concurrency/concurrent-java.html
  4. Effective JavaFX: Understand event driven programming
    1. ../EventDriven/ Programming

3.1 Replaces Swing

  1. "Is JavaFX replacing Swing as the new client UI library for Java SE?"
  2. "Yes. However, Swing will remain part of the Java SE specification for the foreseeable future, and therefore included in the JRE. While we recommend developers to leverage JavaFX APIs as much as possible when building new applications, it is possible to extend a Swing application with JavaFX, allowing for a smoother transition." http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html#6

3.2 JavaFX Getting Started

  1. https://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm Work with the source code as you read. Highly Recommended Reading
  2. Ensemble "provides a gallery of over 100 sample applications that use a wide range of JavaFX features, such as graphics, UI controls, animation, charts, media and WebView. Source code for each sample and links to API documentation are also provided. Ensemble will provide developers with an interactive reference tool for what they can build with JavaFX."
  3. http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html
  4. http://docs.gluonhq.com/javafxports/ JavaFX tutorial, 2015. Presents JavaFX in the context of Android and iOS also.
  5. JavaFX GUI Basics, slides
  6. JavaFX UI Controls and Multimedia, slides

4 JavaFX App

  1. Extend javafx.application.Application
  2. Override the start() method
  3. follow along ./javafxGrid.java Grid of Rectangles

4.1 Stage

  1. Think of it as a window. (Theater analogy)

4.2 Scene

  1. Container for a scene graph.
  2. A stage can have multiple scenes.

4.3 Pane

  1. Organizer of nodes in scene graph
  2. StackPane, FlowPane, TilePane, GridPane, etc

4.4 Scene Graph

  1. Tree of nodes; there is a root node.
  2. A branch may have children (e.g., Group, Region)
  3. Leaf does not have children, (e.g., Rectangle, Circle)
  4. http://docs.oracle.com/javafx/2/scenegraph/jfxpub-scenegraph.htm
  5. https://www.google.com/search?q=javafx+scene+graph+example
  6. Oracle JavaFX Scene Builder

5 Events

5.1 Std Control Flow

  1. Branches (a conditional or switch selects control flow)
  2. Loops (a loop repeats control flow)
  3. Methods (a method call nests control flow)

5.2 Even Based Control

  1. The occurrence of event changes control flow
  2. Event handling in UIs
  3. Events are instances of javafx.event.Event

5.3 Event properties:

  1. Event type
  2. Source
  3. Target

5.4 Event Driven Programming

  1. Language Independent Discussion
  2. Java8 + JavaFX actions are typically written as Lambda Expressions.
  3. PDF Event-Driven Programming in JavaFX, slides, SUNY Stonybrook 2015; includes src code of examples
    1. Explains Anonymous Classes
    2. Explains Inner Classes
    3. Explains Lambda Expressions

5.5 Event handlers

  1. An event handler will execute the code given when a certain event occurs.
  2. Lambda expressions
  3. For example: scene.setOnKeyTyped(event -> { ... code ... })

6 JavaFX Games

  1. Translation: Shifts a node from one position to another
  2. Rotation: Rotates a node around some point

6.1 The Game Loop

  1. Classically, a game will have at its heart a loop like this:
  2. while (notEndOfGame) { updateGameState(); renderGame(); }
  3. The frequency of the loop is referred to as the frame rate, and can determine the “smoothness” of the game play. 35mm film runs at 24FPS. Video games often run at about 30-60FPS.

6.2 A Game Loop in JavaFX

  1. JavaFX takes care of rendering, and can update at about 60FPS when possible.
  2. The AnimationTimer class can be used to get a call back every frame.
new AnimationTimer() {
   @Override
   public void handle(long now) { updateGameState(now); }
}.start();

7 JavaFX Multithreaded

  1. ../Concurrency Language independent discussion.
  2. Java8 Example of threads: ../Java8/src/Meenie.java
  3. Example of Tasks and UI Updates in JavaFX; just the src
  4. javafx.concurrent Package
  5. ExecutorService API
  6. java.util.concurrent.Future Interface
  7. FXML
    1. FXML is a scriptable, XML-based declarative language for constructing JavaFX scene graphs.
    2. https://blogs.oracle.com/sundararajan/entry/using_javafx_fxml_with_nashorn
  8. Java Rich Internet Applications (RIA) Guide

7.1 State Diagram

thread-states.png

8 Refernces

  1. Oracle.com, http://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm Concurrency in JavaFX 2015 (Note that http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm is for v2. 2012) Reference.
  2. Hendrik Ebbers, author of a JavaFX8 book. http://www.guigarage.com/2015/01/concurrency-ui-toolkits-part-1/; http://www.guigarage.com/2015/02/concurrency-ui-toolkits-part-2/;
  3. http://jperedadnr.blogspot.com/2015/03/javafx-on-mobile-dream-come-true.html A pretty good blog on how the author ported the 2048 game to Android using JavaFX etc. 2015.
  4. http://docs.gluonhq.com/javafxports/ JavaFX ported to Android and iOS. 2015. Reference.
  5. https://github.com/JacpFX/JacpFX "The JacpFX Project is an API to create Rich Clients in MVC style with JavaFX, Spring (or other DI frameworks) and an Actor like component approach." Reference.
  6. https://www.cs.rit.edu/~csci242/Lectures/14/Event-diagrams.pdf Java GUIs in JavaFX. 2015. Recommended Reading.
  7. From cs.stonybrook.edu JavaFX GUI Basics, slides; JavaFX UI Controls and Multimedia, slides; Event-Driven Programming in JavaFX, slides; Recommended Reading.

Copyright © 2015 www.wright.edu/~pmateti