Recent Updates Page 2 Toggle Comment Threads | Keyboard Shortcuts

  • mxshrestha 10:06 pm on February 22, 2011 Permalink | Reply
    Tags: , group in javaFx, JavaFx group   

    Using Group in JavaFx 

    Using Group in JavaFx:
    Group in JavaFx can be used to put multiple nodes together in order and then later use it as one. Any effect applied to the group will be applied to all the children nodes.
    For example:

     var bgRect = Rectangle
     {
       width: 20,
       height: 20,
       arcHeight: 5;
       arcWidth: 5;
       fill: Color.CYAN
     }
    
     var pageTxt = Text
     {
       font: Font
       {
          size: 15
       }
    
     content: pageString;
    
     }
    
     var pageNoGrp = Group
     {
       content:
       [
         Stack
         {
            content:
             [
               bgRect,
               pageTxt
             ]
         }
       ]
     }
    

    The variable pageNoGrp is a group that contains a stack of bgRect node and pageTxt node.  It will have a background rectangle of specified dimensions and the pageTxt is stacked on top of the rectangle.

    The output is looks like:

    Here the pagetext zero(0) is stacked on top of rectange.

     

     

     
  • mxshrestha 1:09 am on February 16, 2011 Permalink | Reply
    Tags: container in javafx, , , javafx.scene.layout.container   

    Creating a node using javafx.scene.layout.Container 

    Containers can be extended to create a custom node. Extending a container helps manage and position multiple nodes. The subclass extending the Container must override the function doLayout() of the class Container. Containers provide methods to position and specify the sizes of the node. The subclass extending the container should also override the getPrefWidth and getPrefHeight methods to make sure the layout is resized correctly. Functions provided by Container class to handle position and sizes of the node are:

    • getManaged(content:Node[]):Node[]
    • resizeNode(node:Node, width:Number, height:Number):Boolean
    • positionNode(node:Node, x:Number, y:Number):Void
    • layoutNode(node:Node, x:Number, y:Number, width:Number, height:Number):Boolean

    A simple example of creating a wall of thumbnail extending container class is:

    
    import javafx.scene.layout.Container;
    
    public class ThumbNailContainer extends Container
    {
    package var imageList: String[];
    package var thumbNails: ThumbNails[]; //ThumbNails is a class that
                                             //creates a thumbnail of the image.
    
        package function generateThumbs():Void
        {
    
            thumbNails = for (images in imageList)
                        {
                           ThumbNails
                           {
                              imgUrl:images;
                           }
                        }
        }
    
    //doLayout() function overridden to create own custom layout.
     override function doLayout():Void
     {
        var x = 0;
        var y = 0;
        var temp = 0;
    
        generateThumbs();
    
        for (images in thumbNails)
        {
    
           setNodeWidth(images, 100);//sets the width of the node.
                                    //In this case the thumbnails are the node.
           setNodeHeight(images, 100);//sets the height of the node.
                                     //In this case the thumbnails are the node
          if (x < 600)
          {
              positionNode(images, x += temp, y); //positions the node
              temp = 110;
          }
          else
          {
             x = 0;
             temp = 0;
             y += 110;
          }
     }
    
     content = thumbNails;//sets the content as the sequeces of thumbnails
     }
    
    
    

    More info: http://download.oracle.com/docs/cd/E17802_01/javafx/javafx/1.3/docs/api/javafx.scene.layout/javafx.scene.layout.Container.html

     

     

     
  • mxshrestha 5:02 pm on February 8, 2011 Permalink | Reply
    Tags: bind, bind keyword, , javaFX bind, javafx on replace, on replace, on replace clause, on replace trigger   

    Data binding using bind and on replace in JavaFx 

    Data binding using bind and on replace:
    bind:
    bind is a keyword used to bind a value of a variable to some other expression.
    For example:

    var a = 64;
    var b = bind {a/8};
    

    The value of variable b in the above expression is bound to the value of variable a.
    In the case above the value of b is 8.

    var a = 64;
    var b = bind {a/8};
    a = 64 * 8;
    println("value of b  is :{b}"};
    

    When the above code is executed the value of variable b will be printed as 64. So, the value of variable b changes accordingly every time the value of variable a changes.

    on replace:
    on replace is usually used when certain actions need to be taken once the value of the variable is changed.
    For example:

    var a = 64;
    var b = bind {a/8} on replace { if (a > 64) a = 10};
    a = 64 * 8;
    println("value of b  is :{b}"};
    

    The above block of code replaces the value of variable a from 64*8 to 64 once the value of b is printed from the last line. When the second line is executed the value of variable a remains unchanged because variable a has the value of 64. However when the last line is executed the value of variable at that moment is 64 * 8 which is greater than 64. So, when variable b gets the value 64 the value of variable a is also changed to 10.

     
  • mxshrestha 4:39 pm on February 8, 2011 Permalink | Reply
    Tags: delete expression, duration type, insert expression, , javaFx duration type, javaFX sequence type, javaFX types, Sequence type   

    Duration Types and Sequence Types in JavaFx 

    Two interesting data types introduced in JavaFX are duration type and sequence type.

    Duration Types:

    Duration types are used in JavaFX to specify the time duration. The time units can be specified in ms, s, m, or h.  For example:

    
    var timeDuration = 10s;
    
    

    The variable timeDuration is assigned 10 seconds. The duration type variables can be very useful when animating an object.

    Sequence Types:
    Sequence types in JavaFX  is a data type that holds a sequence of object values. The interesting thing about sequence types is they can be specified as ranges and also have insert and delete features like that of a list in java.
    A simple sequence construction would be:

    var numbers = [1,2,3,4,5,6,7,8,9,10];
    

    The same assignment can also be performed as:

    var numbers = [1..10];
    

    Keep in mind that the sequence can hold any objects.

    var stringsExample = ['this','is','an','example'];
    

    we can also specify datatypes:

    var numbers:Number[] = [1..10]
    

    This assignment is the same as the above assignments for numbers variable except in this we specify that the variable numbers carry the Number data type only. Specifying data types are usually useful when sequence of different objects have to be created.
    insert expression can be  used with sequence to add an object to the sequence. For example:

    insert 11 into numbers;
    

    This statement will add the number 11 to the end of the sequence. So, the range of sequence now is from 1 to 11. We can also specify where we want to add the object.

    insert 0 before numbers[0];
    

    This statement adds the number 0 before 1 in the sequence so the range of sequence now is 0 to 10.

    insert ['a', 'JavaFx'] after stringExample[2];
    

    This statement adds the sequence [‘a’,’JavaFx’] after ‘an'(index 2 of stringExample) in the sequence stringsExample. so the resulting sequence will be:

    stringsExample = ['this', 'is', 'an', 'a', 'JavaFx', 'example']
    
    

    Similarly delete operation can be performed from a sequence.

     

    delete 'a' from stringsExample;
    

    This statement will delete the value ‘a’ from the sequence stringsExample. If there are multiple ‘a’ in the sequence then all occurrence will be deleted.

    delete stringExamples[0..2];
    
    

    This statement will delete the values in the sequence from index 0 to index 2. So the resulting stringsExample sequence have values [‘a’, ‘JavaFx’, ‘example’];

     

    </p>
    delete numbers[0];
    

    This statement will delete the value in index 0 of the sequence numbers.

    delete numbers;
    

    This statement will delete all the values of the sequence numbers. So, the sequence number is empty sequence after this operation.

     
  • mxshrestha 8:01 pm on January 31, 2011 Permalink | Reply
    Tags: , JavaFX access modifiers, JavaFX Block Expression, JavaFX class, JavaFX functions, JavaFX objects, JavaFX scripts   

    Java FX class, function, block expression and object 

    Creating a Class:
    Creating a class in JavaFX is similar to creating a class in Java.  The class declaration should start with an access modifier followed by keyword ‘class‘ and the desired classname. If no access modifier is specified JavaFX assumes the access modifier to be default access i.e. the class cannot be accessed from other FX scripts and can only be accessed in the same script where it is declared. It is similar to private access modifier in Java. Also, class members can be only  var, def, function, init, or postinit.

    More information about access modifier. (http://download.oracle.com/javafx/1.3/tutorials/core/modifiers/)

    Let’s start with an example:

    public class Name
    {
    var title: String;
    var firstName: String;
    var middleInitial:  String;
    var lastName: String;
    var YOB: Integer;
    
    public var age =
    {
    var currentYear = 2011;
    var currentAge = currentYear  - YOB; //Last expression
    }
    
    public var fullName =  "{firstName}  {middleInitial}  {lastName}";
    
    public function getTitle(): String
    {
    return this.title;
    }
    
    public function setTitle(title:String):Void
    {
    this.title = title;
    }
    }
    
    //Instantiating an object of class Name and assigning it to variable name1.
    var name1 = Name
    {
    title:  "Mr";
    firstName: "Henry";
    middleInitial: "G";
    lastName: "Thomas";
    YOB: 1984;
    }
    
    

    In the above example, the public access modifier makes the class visible and accessible to other script files. The name of the class is ‘Name’. The class contains four default access string variables title, firstName, middleInitial, and lastName and one default access Integer YOB. Since no access modifier is specified for these variables, they have default access as their access modifier. So, these variables are accessible within the class ‘Name’ only.  Suppose you create an object of the class ‘Name’ in some other script then these variables cannot be accessed from that script.

    Block Expression:
    A block expression starts  and ends with curly braces (starts with ‘{‘ and ends with ‘}’). It contains a list of declarations and expressions. The block expression returns the value of the last expression or is a Void type if no expression exists within the block. The variable ‘age’ is assigned a block expression.  The last expression within the block is currentAge which is returned and then is assigned to variable ‘age’. So the value of age is whatever the value of currentAge is calculated from the expression. Notice that the variable ‘age’ has a public access modifier. So, this variable can be accessed wherever object of the class is instantiated. Also the variable fullName is assigned a string of block expressions. {firstName} returns the value of class variable firstName, {middleInitial} returns the value of class variable middleInitial and {lastName} returns the value of class variable lastName. Keep in mind that ‘var’ declarations and ‘def’ declarations  are expressions and hence the value is returned by the block expression.

    Declaring Function:
    Function declarations start with access modifier followed by the keyword function and then the function name. The return type is followed by a colon:‘ after the function name. getTitle() and setTitle() are two functions. getTitle()  has a public access modifier and has no function parameters and returns the type String.  setTitle() also has a public access modifier with a string parameter ‘title’ and has a return type of Void i.e. nothing is returned. Multiple function parameters can be specified by separating each with a comma ‘,’.
    ex:
    public function test (arg1:String, arg2:Integer):Integer
    {
    …..some expression
    }
    This function has two parameters arg1 which is a string and arg2 which is an integer and the return type of the function is  integer.

    Instantiating an Object:
    The variable name1 is assigned an object of the class Name.  The class members are initialized within the curly braces. So name1 object of class name has “Mr” as value for title, “Henry” as value for firstName, “G” as value for middle initial, “Thomas” as value for lastName, and 1984 as value for YOB.
    name1.getTitle() will return string value  “Mr” and name1.setTitle(“Jr”) will set the value of title to “Jr” in name1. Keep in mind that the object is instantiated in the same FXscript file and hence the default access members(title, firstName, middleInitial, lastName) are directly accessible. If the object was instantiated in some other FXscript then the default access modifiers would not be accessible and only the declarations with public access modifiers would be accessible. For example the variable ‘title’ would not be accessible but the  variable ‘fullName’ would be.

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel