Comparison of JavaFX Script to java

Color
JavaFX Scriptjava
Rectangle { fill: Color.BLACK }g.setColor(Color.black) fillRect(0, 0, size.width, size.height);
Rectangle { fill: Color.rgb(255,204,0) }g.setColor(new Color(255, 204, 0)); fillRect(0, 0, size.width, size.height);
Rectangle { fill: Color.web("#ff0066") }g.setColor(new Color(255, 0, 102)); fillRect(0, 0, size.width, size.height);
Rectangle { stroke: Color.BLACK }g.setColor(Color.black); fillRect(0, 0, size.width, size.height);
Shape
JavaFX Scriptjava
Line { startX: 30 startY: 20 endX: 30 endY: 20 }g.drawLine(30, 20, 30, 20);
Rectangle { x: 10 y: 20 width: 30 height: 30 fill: Color.WHITE stroke: Color.BLACK }g.setColor(Color.WHITE); g.fillRect(10, 20, 30, 30); g.setColor(Color.BLACK); g.drawRect(10, 20, 30, 30);
Path { stoke: Color.BLACK MoveTo LineTo LineTo LineTo }
Variables and Datatypes
JavaFX Scriptjava
var x = 70;//initialize x = 30;int x = 70;//initialize x = 30;
var x = 70.0;float x = 70f;
var a = [5,6,7]; //initialize a[0] = 12; //reassignint a = {5,6,7}; //initialize a[0] = 12; //reassign
var a = [5,6,7]; //initialize insert 12 into a; //appendList<Integer> a = new ArrayList<Integer>(); a.add(5); a.add(6); a.add(7); a.add(12);//append
Loops and Conditionals
JavaFX Scriptjava
while(true) { // do stuff }while(true) { // do stuff }
for(i in [0..10]) { // do stuff }for(int i=0; i<10; i++) { // do stuff }
//loop over a sequence for(i in a) { // do stuff }//loop over a java.util.List for(Integer i : a) { // do stuff }
if(c == 1) { // do stuff }if(c == 1) { // do stuff }
if(c != 1) { // do stuff }if(c != 1) { // do stuff }
if(c < 1) { // do stuff }if(c < 1) { // do stuff }
if(c < 1 and c > 1) { // do stuff }if(c == 1) { // do stuff }
if(c != 1) { // do stuff } else if (d != 1) { // do other stuff } else { // do last stuff }if(c != 1) { // do stuff } else if (d != 1) { // do other stuff } else { // do last stuff }
Structure
JavaFX Scriptjava
//comment//comment
/* comment *//* comment */
public function doStuff(x:Number):Void { // do stuff } doStuff(12);public void doStuff(Number x) { // do stuff } doStuff(12);
public function doStuff(x:Number):Number { // do stuff // return num; } x = doStuff(12);public Number doStuff(Number x) { // do stuff return num; } x = doStuff(12);
Input
JavaFX Scriptjava
Rectangle { onMousePressed: function(e:MouseEvent) { x = e.getX(); } onMouseDragged: function(e:MouseEvent) { x = e.getX(); } } MouseAdapter ma = new MouseInputAdapter(){ public void mousePressed(MouseEvent e) { int x = e.getX(); } public void mouseDragged(MouseEvent e) { int x = e.getX(); } }; component.addMouseListener(ma); component.addMouseMotionListener(ma);
Rectangle { onKeyPressed: function(e:KeyEvent) { var key = e.getKeyChar(); } }KeyAdapter ka = new KeyAdapter() { public void keyPressed(KeyEvent e) { char key = e.getKeyChar(); } }; component.addKeyListener(ka);
Animation
JavaFX Scriptjava
var rect = Rectangle { } var anim = Timeline { keyFrames: [ at(0s) { rect.x => 100}, at(1s) { rect.x => 200}, ] repeatCount: Timeline.INDEFINITE toggle: true };// Using the Scenegraph Framework SGRectangle rect = new SGRectangle(); Clip c = Clip.create(1000, rect, "x", 100,200); c.setRepeatCount(Clip.INDEFINITE); c.setAutoReverse(true); c.start();
Binding and Triggers
JavaFX Scriptjava
var foo = 0.0; var bar = bind foo; foo = 2.2; // bar is updated N/A
// create a class with a trigger public class Foo { public attribute bar:Number = 0.0 on replace { System.out.println("bar was changed to {bar}"); } } N/A
Packages and Import Statements
JavaFX Scriptjava
package org.myname.myapp;package org.myname.myapp;
import java.lang.System;import java.lang.System;
Class Definition
JavaFX Scriptjava
//declare a class public class Foo extends CustomNode { // attributes and functions go here }//declare a class public class Foo extends CustomNode { // fields and methods go here }
//declare an attribute public class Foo extends CustomNode { public attribute bar:Number = 1.0; }//declare a field public class Foo extends CustomNode { public double bar = 1.0; }
//declare a function public class Foo extends CustomNode { public function baz(a:Number, b:String):Number { return a*2; } }//declare a method public class Foo extends CustomNode { public double baz(double a, String b) { return a*2; } }
Audio and Video
JavaFX Scriptjava
// Create a video player import javafx.scene.media.*; MediaView { mediaPlayer: MediaPlayer { media: Media { source: "file://myvideo.mov" } } }N/A
// Create an audio player import javafx.scene.media.*; MediaView { mediaPlayer: MediaPlayer { media: Media { source: "file://myvideo.mp3" } } }N/A