X

Vaibhav's Blog Space

  • Java
    August 15, 2008

Insertion trigger in JavaFX

Guest Author

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.


Join the discussion

Comments ( 6 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha