Introduction To Java FX
Introduction To Java FX
schematic
Vibe
Partnered with Sun @ - JavaFX Launch - Mobile World Congress - SXSW
schematic
Topics
5 minute JavaFX script introduction Transforms & Effects Animation Events Production Suite
schematic
schematic
schematic
schematic
What is a SceneGraph?
Declarative method to dening visual
structure
A composite hierarchy of objects: Node, a leaf (terminal node) Group, a composite node (contains
others)
schematic
Description
Device idependendent container The container for the SceneGraph A node that knows how draw rectangles A node that draws Text
schematic
Features of Nodes
Have one parent Transforms (translation, scaling, etc) Effects (shadows, blurs, lighting, etc) Event handlers (keys & mouse) Visual Properties (coordinates, height,
width, etc)
schematic
UIs are constructed using a Scene Graph Can call existing Java code Java can call FX with a little effort
schematic
schematic
Structure of Examples
package presentation.transform; import import import import import javafx.scene.transform.*; javafx.stage.Stage; javafx.scene.Scene; javafx.scene.shape.Rectangle; javafx.scene.paint.Color; var rect = Rectangle { x: 100 y: 10 width: 200 height: 200 fill: Color.DARKOLIVEGREEN }; var rotateRect = Rectangle { x: 100 y: 250 // start lower than original width: 200 height: 200 fill: Color.DARKOLIVEGREEN transforms: [ Rotate { angle: 45 pivotX: 150 pivotY: 350 } ] }; // continued from snippet on left Stage { title: "Rotate" scene: Scene { width: 400 height: 600 content: [ rect, rotateRect ] } }
schematic
Transform: Rotate
var rotateRect = Rectangle { x: 100 y: 250 width: 200 height: 200 fill: Color.DARKOLIVEGREEN transforms: [ Rotate { angle: 45 pivotX: 150 pivotY: 350 } ] };
schematic
Additional Transforms
From javafx.scene.transform, you can also
apply:
schematic
schematic
schematic
Other Effects
From javafx.scene.effect, you can also apply: Blend, Flood, Glow, InnerShadow,
Lighting, Motion Blur, and more!
schematic
Animation
schematic
schematic
Animation: Tweening
variable => endValue TWEEN Interpolator
xPosition => 325 TWEEN Interpolator.LINEAR yPosition => 525 TWEEN Interpolator.LINEAR
time:
0s
1s
2s 325 525
3s
xPosition 0 yPosition 0
schematic
Animation: Interpolation
Interpolators: specify how to calculate
current value of variable
Animation: Images
// we put Duke images T0-T16.gif the // duke directory, directly below this // directory var frame = 0; var duke = ImageView { image: bind Image { url: "{__DIR__}duke/T{frame}.gif"; } };
schematic
Animation: Images
var timeline = Timeline { repeatCount: Timeline.INDEFINITE autoReverse: true keyFrames: [ KeyFrame { time: 3s values: [ frame => 16 tween Interpolator.LINEAR ] } ] }; timeline.playFromStart();
schematic
Events
schematic
Events: Mouse
var message: String = ""; var text = Text { x: 150, y: 150, content: bind "{message}"; } var rect = Rectangle { x: 150, y: 10, width: 100, height: 100 fill: Color.ROYALBLUE onMouseMoved: function( e: MouseEvent ):Void { message = "mouse moved!" } onMouseClicked: function( e: MouseEvent ):Void { message = "mouse clicked!" } onMousePressed: function( e: MouseEvent ):Void { message = "mouse pressed!" } };
schematic
Events: Mouse
Other Mouse Events (variables on Nodes): onMouseEntered onMouseReleased onMouseDragged onMouseWheelMoved All accept the same anonymous function
schematic
Events: Keys
Similar to Mouse events 3 types: onKeyPressed, onKeyReleased,
onKeyTyped
Production Suite
schematic
Production Suite
Tools to greatly simplify designer/developer
workow
Update Design
FXZ
Update UI Stub
Workflow Overview
schematic
Pig:Dice Game
schematic
Demo of Game
schematic
Demo of Artwork
schematic
NetBeans View
schematic
Rolling Dice
var die = fxz.getNode(die); var rotateDie = RotateTransition { duration: 150ms byAngle: 36 fromAngle: 0 toAngle: 360 repeatCount: 4 node: die } rotateDie.playFromStart();
schematic
Flashing Scores
public var value : Integer = -1 on replace { if (value != -1) { text.content = getContent(value); var t = ScaleTransition { repeatCount: 2 autoReverse: true node: text fromX: 1, toX: 1.3, byX: .05 fromY: 1, toY: 1.3, byY: .05 interpolate: Interpolator.EASEIN duration: 175ms } t.playFromStart(); } }
var bigNoseWiggle = SequentialTransition { repeatCount: Timeline.INDEFINITE node: board.big_pig_nose; content: [ PauseTransition { duration: 8s } TranslateTransition { repeatCount: 4 autoReverse: true toY: -3, byY: 1 toX: -3, byX: 1 duration: 300ms node: board.big_pig_nose; } ] }; bigNoseWiggle.playFromStart();
schematic
Swapping Players
var shrink = Scale { x: .8, y: .8; } /** True if the current player is player1, false otherwise. */ public var isPlayerOne: Boolean on replace { if (isPlayerOne) { player1Spot.visible = true; player2Spot.visible = false; swap(player1Label, player2Label); } else { player1Spot.visible = false; player2Spot.visible = true; swap(player2Label, player1Label); } } /** Swap the tabs to show the next player. */ function swap( node1:Node, node2:Node ) { delete shrink from node1.transforms; var bounds = node2.boundsInParent; transform.pivotX = bounds.minX + bounds.width / 2; transform.pivotY = bounds.minY + bounds.height / 2; insert shrink into node2.transforms; node1.opacity = 1; node2.opacity = .6;
schematic
UI
Board UI
Load UI
Board fxz
UI Facade Logic
Controller
Structure of Game
schematic
Pig Highlights
Most of the code is the logic, effects &
animation
Thank You!
Special thanks to Jonathan Wei from
Schematic
http://newfoo.net/presentations/introduction-to-javafx/
schematic