Sunday 15 January 2012

A Taste of JavaFX Script

The best way to get the flavor of JavaFX Script is to see some examples! Don't worry if some of these don't make sense, the rest of this Reference will provide the details.
JavaFX Script programs are written as one or more scripts. A script consists of expressions and declarations, typically in a file. The following is an expression and thus a valid script:
4
While this is a valid script, it doesn't do anything (other than evaluate the number 4). To make the result appear on the console, we could print the result of the expression using the built-in function 'println':
println(4)
Which makes it irresistible to show the obligatory Hello World script:
println("Hello, World")
This script will, of course, print:
Hello, World
While it was certainly easy to write Hello World in JavaFX Script, this isn't really in the spirit of the language, since it isn't graphical. There is a separate tutorial on using the graphics capabilities of the JavaFX platform;
You can, of course, have more complex expressions:
println("Circumference is { 2*3.1415*7 } ")
This script will print:
Circumference is 43.981
Note the expression enclosed in braces. What is being printed is a string expression. A string representation of the expression in braces is evaluated at runtime. The value of the string expression is the whole string with that string representation replacing the expression in braces. That is { 2*3.1415*7 } is replaced in the string by 43.981 which is the runtime value of { 2*3.1415*7 }.
Better programming style would encourage us to use named variables instead of "magic numbers" in our calculation, which is easy to do:
def PI = 3.14159265; 
var r = 7; 
println("Circumference is { 2 * PI * r } ")
Here PI is defined to always be 3.14159265, while the variable r starts out as 7, but can be changed.
To improve our little script even more, we could also define a function to compute the circumference:
def PI = 3.14159265; 
var r = 7; 
function circumference(radius) { 2 * PI * radius }
println("Circumference is { circumference(r) } ")
The function circumference takes a Number radius as input, and returns the value of the expression 2 * PI * radius.
Note that JavaFX Script uses type inference to figure out the type of variables and function. Type inference means that the compiler figures out the types of variables and functions from context, rather than requiring us to name them explicitly. We are always free to name types explicitly if so desired. With explicit type declarations, this program would be written:
def PI : Number = 3.14159265;  
var r : Number = 7; 
function circumference(radius : Number) : Number {
   2 * PI * radius 
} 
println("Circumference is { circumference(r) }")
Like many other languages, including Java, C++, and Ruby, JavaFX Script allows you to use classes to represent a real or abstract entity. In this case, a point:
class Point { 
   var x : Integer; 
   var y : Integer; 
   function show() {
      println("Point {x}, {y}")
   } 
} 
This class defines an abstract data type for a two-dimensional point, with instance variables to hold its x and y coordinates. It also defines a function to display the state of the Point on the console.
We create an instance of a Point using an object literal, which lets us assign initial values to its instance variables like this:
var myPoint = Point { 
   x: 12
   y: 9
}
This will create a Point whose x and y values are 12 and 9, respectively.
Finally we can ask the point to print itself by calling its show() function (defined, above, in the Point class).
myPoint.show()
This will print:
Point 12, 9
For an example-based introduction to JavaFX, keep in touch with us, more on JavaFX Tutorial soon.

1 comment:

  1. I desired to thank you for this exceptional read!! I absolutely enjoyed every single small little bit of it. I’ve you bookmarked your internet site to look at out the new stuff you post.
    Web developer

    ReplyDelete