Friday Aug 15, 2008

Insertion trigger in JavaFX

This weekend I have written some physics equation in JavaFX like motion under gravity and collision motion. In all  complex code, we have to write a class of MotionBall, which consist attribute like X position, Y position, Velocity, Mass and Color of Ball like:

public class MotionBall extends CustomNode {

    public attribute x: Number;
    public attribute y: Number;
    public attribute velocityX: Number;
    public attribute velocityY: Number;
    public attribute mass: Number;
    public attribute radius: Number;
    public attribute color: Color;
 
    public function create(): Node {
        return Circle {
            centerX : bind x, centerY : bind y, radius : bind radius
            fill : bind color
 
        };
    }
} 

After this, the important part is to fill these data outside the class. Say, I want to make 5 instance of MotionBall. And I want to give Center X, Center Y, radius and color. Here in this code, I want to put the Balls at the random position with random radius with random color :-). Here goes the insertion trigger in JavaFX:

package insertion;

import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.geometry.Circle;
import javafx.scene.paint.Color;
import java.util.Random;


var radius: Number;
var balls: MotionBall[];

var rnd : Random = new Random();

 for( i in [1..5] ) {
                insert MotionBall {
                    x : rnd.nextInt( 170), y :  rnd.nextInt( 170 ), radius : rnd.nextInt( 10 ) + 20
                    color : Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255))
                    , opacity : 0.9
                    } into balls;
                }    
Frame {
    title: "Insertion Example"
    width: 250
    height: 250
    closeAction: function() { java.lang.System.exit( 0 ); 
    }
    visible: true

    stage: Stage {
        fill: Color.BLACK
        content: 
            bind balls
 
    }
}
 
public class MotionBall extends CustomNode {

    public attribute x: Number;
    public attribute y: Number;
    public attribute velocityX: Number;
    public attribute velocityY: Number;
    public attribute mass: Number;
    public attribute radius: Number;
    public attribute color: Color;
 
    public function create(): Node {
        return Circle {
            centerX : bind x, centerY : bind y, radius : bind radius
            fill : bind color
 
        };
    }
}

This is how output looks like : jar file.

I have written some attribute for further use, so please ignore the use.


About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today