Monday, August 17, 2009

JavaFX Script Introduction

JavaFX script is a declarative language. In this you tell the environment what you want. Than the environment takes care of building that. For example, you just tell that you need a circle of certain radius than you leave it to the environment to handle the output.

JavaFX script is quite powerful in the sense that it provides all the paradigms of a high level language and being closely tied to Java, can reuse all the Java API's.

Let's look into a simple JavaFX script and understand the basics. Let's write a simple script which does a division. Let's name that file as 

Divide.fx

//def defines the variable whose value remain constant
//as assigned in the begining
def numerator = 50;
def dinomenator = 0;

//var also define variable but they can be assigned newer values
var result;

//function can be called before the definition
divide();

//Definition of function
function divide(){
     result = numerator / dinomenator;
     print("{numerator} / {dinomenator} = {result}");
}

var and def are two ways to define the variables. Note that we do not tell the data type for var and def. This is inferred automatically and is also known as type inference.

Now let's introduce other function where the values are not hard coded but passed as an argument

//def defines the variable whose value remain constant
//as assigned in the begining
def numerator = 50;
def dinomenator = 0;

//var also define variable but they can be assigned newer values
var result;

//resultDouble will hold the the new double division calculation
var resultDouble;

//function can be called before the definition
//Calling the function with double values
divide(150.05,125.03);

//Overloaded definition of function with arguments
function divide(arg1:Double, arg2:Double){
     resultDouble = arg1 / arg2;
     print("{arg1} / {arg2} = {resultDouble}");
}

//Definition of function
function divide(){
     result = numerator / dinomenator;
     print("{numerator} / {dinomenator} = {result}");
}

The output of this script is

150.0500030517578 / 125.02999877929688 = 1.200112009251686

So the precision needs to be handled.

Return Value

The function can return a value also. Let's modify our argument function so that it returns a value

var resultDouble;

//The value is captured in a return value and displayed.
resultDouble = divide(150.05,125.03);
print("The value of division is  = {resultDouble}");

//returns the result
function divide(arg1:Double, arg2:Double) 
double{ resultDouble = arg1 / arg2; return resultDouble; }

Command Line arguments

We can provide command line arguments also

//run is like the main function which is invoked by default by the runtime.
//If we do not provide run as in previous example the compiler generates run by itself
function run(args : String[]) {
     //Convert Strings to Double
     //We are direclty using the Java API's which bring tremendous 
     //richenss to JavaFX scripting environment
     def numerator = java.lang.Double.parseDouble(args0);
     def denominator = java.lang.Double.parseDouble(args1);

     resultDouble = divide(numerator,denominator);
     print("The value of division is  = {resultDouble}");
}

//Overloaded definition of function with arguments
function divide(arg1:Double, arg2:Double) 
double{ resultDouble = arg1 / arg2; return resultDouble; }

The code has to be run by providing arguments

javafx Divide 150.2 134.3

Exceptions

In case of exceptional conditions, the program throws exceptions like any normal java class. For example, change the value of denominator to 0.

function divide(){
     result = numerator / 0;
     print("{numerator} / {dinomenator} = {result}");
}

It will throw an exception

java.lang.ArithmeticException: / by zero
at Divide.divide(Divide.fx:37)
at Divide.javafx$run$(Divide.fx:25)

More on JavaFX

No comments:

Post a Comment