Sunday, September 12, 2010

JavaFX - Basics by Example



Update 1: This article was done when JavaFX 1.3 version was available and the "JavaFX Script" existed as a separate language for it. From JavaFX version 2.0 this language is now deprecated in favor of Java.


Continue with the Basics by Example; today's version of the post written in JavaFX Script Enjoy!

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html


Greetings Program - Verbose
// JavaFX Basics
package FxGreetProgram;

import java.lang.System;
import javafx.*;
import javafx.date.DateTime;
import java.text.SimpleDateFormat;

public class Greet {
    // Fields or Attributes
    //(initialize instead of using parameterless constructor)
    var message: String = "";
    var name: String = "";
    var loopMessage: Integer = 0;
    // Getters and Setters. No Properties built-in syntax available
    public function getMessage():String {
        return this.message;
    };
    public function setMessage(value:String):Void {
        this.message = this.Capitalize(value);
    };
    public function getName():String {
        return this.name;
    };
    public function setName(value:String):Void {
        this.name = this.Capitalize(value);
    };
    public function getLoopMessage():Integer {
        return this.loopMessage;
    };
    public function setLoopMessage(value:Integer):Void {
        this.loopMessage = value;
    };
    // insted of Overloaded Constructor, we use init and/or post-init to initialize
    init {        
        this.message = this.Capitalize(this.message);
        this.name = this.Capitalize(this.name);
        this.loopMessage = this.loopMessage;
    };
    // Method 1
    function Capitalize(val:String):String {
        // "if-then-else" statement
        if (val.length() >= 1) {
            return "{val.substring(0,1).toUpperCase()}{val.substring(1, val.length())}";
        }    
        else  {
            return "";
        }
    };
    // Method 2
    public function Salute():Void {
        // "for" statement
        for (i:Integer in [0..this.loopMessage]) {
            println("{this.message} {this.name}!");
        }
    };
    // Overloaded Method 2.1
    public function Salute(message:String, name:String, loopMessage:Integer):Void {
        // "while" statement
        var i:Integer = 0;
        while(i < loopMessage) {
            println("{this.Capitalize(message)} {this.Capitalize(name)}!");
            i++;
        }
    };
    // Overloaded Method 2.2    
    public function Salute(name:String):Void {
        // "switch/case" statement is not supported
        // so I'm using if then else if...
        //var dtNow:Calendar = Calendar.getInstance();
        var dtNow:DateTime = DateTime{};
        def hh = Long.parseLong(new SimpleDateFormat("HH").format(dtNow.instant));

        if (hh == 6 or hh == 7 or hh == 8 or hh == 9
                or hh == 10 or hh == 11) {
            this.message = "good morning,";
        }
        else if (hh == 12 or hh == 13 or hh == 14 or hh == 15
                or hh == 16 or hh == 17) {
            this.message = "good afternoon,";
        }
        else if (hh == 18 or hh == 19 or hh == 20
                or hh == 21 or hh == 22) {
            this.message = "good evening,";
        }
        else if (hh == 23 or hh == 0 or hh == 1 or hh == 2
                or hh == 3 or hh == 4 or hh == 5) {
            this.message = "good night,";
        }
        else {
            this.message = "huh?";
        }    
        println("{this.Capitalize(this.message)} {this.Capitalize(name)}!");
    };

};

// Console Program
public function run():Integer {
    // Define variable object of type Greet
    var g:Greet;
    // Instantiate Greet. Call Init
    g = Greet {};
    // Call Setters
    g.setMessage("hello");
    g.setName("world");
    g.setLoopMessage(5);
    // Call Method 2
    g.Salute();
    // Overloaded Method 2.1 and Getters
    g.Salute(g.getMessage(), "javaFX", g.getLoopMessage());
    // Overloaded Method 2.2
    g.Salute("carlos");

    // Stop and exit
    println("Press any key to exit...");
    System.in.read();
};

Greetings Program - Minimal
// JavaFX Basics
import java.lang.System;
import javafx.*;
import javafx.date.DateTime;
import java.text.SimpleDateFormat;

class Greet {
    // Fields or Attributes
    //(initialize instead of using parameterless constructor)
    var message = "";
    var name = "";
    var loopMessage = 0;
    // Getters and Setters. No Properties built-in syntax available
    function getMessage() {
        return message;
    }
    function setMessage(value:String) {
        message = Capitalize(value);
    }
    function getName() {
        return name;
    }
    function setName(value:String) {
        name = Capitalize(value);
    }
    function getLoopMessage() {
        return loopMessage;
    }
    function setLoopMessage(value:Integer) {
        loopMessage = value;
    }
    // insted of Overloaded Constructor, we use init and/or post-init to initialize
    init {
        message = Capitalize(message);
        name = Capitalize(name);
        loopMessage = loopMessage;
    }
    // Method 1
    function Capitalize(val:String):String {
        // "if-then-else" statement
        if (val.length() >= 1) {
            return "{val.substring(0,1).toUpperCase()}{val.substring(1, val.length())}";
        }
        else  {
            return "";
        }
    }
    // Method 2
    function Salute() {
        // "for" statement
        for (i:Integer in [0..loopMessage]) {
            println("{message} {name}!");
        }
    }
    // Overloaded Method 2.1
    function Salute(message:String, name:String, loopMessage:Integer) {
        // "while" statement
        var i:Integer = 0;
        while(i < loopMessage) {
            println("{Capitalize(message)} {Capitalize(name)}!");
            i++;
        }
    }
    // Overloaded Method 2.2
    function Salute(name:String) {
        // "switch/case" statement is not supported
        // so I'm using if then else if...
        //var dtNow:Calendar = Calendar.getInstance();
        var dtNow:DateTime = DateTime{};
        def hh = Long.parseLong(new SimpleDateFormat("HH").format(dtNow.instant));

        if (hh == 6 or hh == 7 or hh == 8 or hh == 9
                or hh == 10 or hh == 11) {
            message = "good morning,";
        }
        else if (hh == 12 or hh == 13 or hh == 14 or hh == 15
                or hh == 16 or hh == 17) {
            message = "good afternoon,";
        }
        else if (hh == 18 or hh == 19 or hh == 20
                or hh == 21 or hh == 22) {
            message = "good evening,";
        }
        else if (hh == 23 or hh == 0 or hh == 1 or hh == 2
                or hh == 3 or hh == 4 or hh == 5) {
            message = "good night,";
        }
        else {
            message = "huh?";
        }
        println("{Capitalize(message)} {Capitalize(name)}!");
    }
}

// Console Program
function run() {
    // Define variable object of type Greet and Instantiate Greet. Call Init
    var g = Greet {};
    // Call Setters
    g.setMessage("hello");
    g.setName("world");
    g.setLoopMessage(5);
    // Call Method 2
    g.Salute();
    // Overloaded Method 2.1 and Getters
    g.Salute(g.getMessage(), "javaFX", g.getLoopMessage());
    // Overloaded Method 2.2
    g.Salute("carlos");

    // Stop and exit
    println("Press any key to exit...");
    System.in.read();
}


And the Output is:


















No Private Access Modifier?
JavaFX Access Modifiers: http://download.oracle.com/javafx/1.3/tutorials/core/modifiers/
// Console Program
function run() {
    // Define variable object of type Greet and Instantiate Greet. Call Init
    var g = Greet {};
    // Call Setters
    g.setMessage("hello");
    g.setName("world");
    g.setLoopMessage(5);
    // using attributes and fields because they are non-private
    g.message = "hello";
    g.name = "world";
    g.loopMessage = 5;
    // of course, by doing this you dont get the Capitalize code within the setter...
    // unless you use the Capitalize method because it is also non-private!
    // encapsulation in JavaFX is done via package access modifiers, and, to understand it, 
    // you might want to read about it here below:
    // http://download.oracle.com/javafx/1.3/tutorials/core/modifiers/
    // to know more about JavaFX access modifiers
    g.message = g.Capitalize("hello");
    g.name = g.Capitalize("world");
    g.loopMessage = 5;
    // Call Method 2
    g.Salute();

};

No comments:

Post a Comment