JavaFX, Constrain check - Next Part !
Sunday Aug 10, 2008
As we discussed in the last blog, about easing and constraint. Now we have seen the ball is moving inside the box but not completely. And this is not possible as well because the mouseMove code has been written on the box and mouse can move anywhere in the box. So, the basic funda comes first is to make one virtual box bigger than target box and the mouseMove code should be written on the outer virtual box rather than the box inside which we want to move our ball. Now the real constraint part, that will be written inside the onMouseMove code:
onMouseMoved: function( e: MouseEvent ):Void {
mouseX = e.getX();
mouseY = e.getY();
if(mouseX < rect.x + circleRadius ) {
mouseX = rect.x + circleRadius
};
if(mouseX > rect.x + rect.width - circleRadius ) {
mouseX = rect.x + rect.width - circleRadius
};
if(mouseY < rect.y + circleRadius ) {
mouseY = rect.y + circleRadius
};
if( mouseY > rect.y + rect.height - circleRadius ) {
mouseY = rect.y + rect.height - circleRadius
};
}
Here, rect is the target box inside which we want the ball to move. So, if mouse position is going out of the boundary, we are pushing it inside the boundary. I am still damn sure, some checking may be missing. Now, this mouseMove will go into the outer bigger circle. Here is the final code:
package constraintcheck;
import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.geometry.Circle;
import javafx.scene.paint.Color;
import javafx.input.MouseEvent;
import javafx.scene.transform.Translate;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import java.lang.Math;
import javafx.scene.geometry.Rectangle;
var mouseX : Number = 100;
var mouseY : Number = 100;
var circleX : Number = 300;
var circleY : Number = 300;
var t : Number = 100;
var easing : Number = 0.05;
var circleRadius : Number = 50;
var timeline = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames : [
KeyFrame {
time : 16ms
action : function() {
if(
Math.abs(mouseX - circleX ) > 0.1 ) {
circleX = circleX + (mouseX - circleX ) * easing;
}
if( Math.abs(mouseY - circleY ) > 0.1 ) {
circleY = circleY + ( mouseY - circleY ) * easing;
}
}
}
]
}
Frame {
title: "Constrain Check"
width: 700
height: 700
closeAction: function() { java.lang.System.exit( 0 );
}
visible: true
var rect = Rectangle {
x: 100, y: 100
width: 400, height: 400
fill: Color.RED
}
stage: Stage {
content: [
Rectangle {
x: 0, y: 0
width: 500, height: 500
fill: Color.BLACK
onMouseMoved: function( e: MouseEvent ):Void {
mouseX = e.getX();
mouseY = e.getY();
if(mouseX < rect.x + circleRadius ) {
mouseX = rect.x + circleRadius
};
if(mouseX > rect.x + rect.width - circleRadius ) {
mouseX = rect.x + rect.width - circleRadius
};
if(mouseY < rect.y + circleRadius ) {
mouseY = rect.y + circleRadius
};
if( mouseY > rect.y + rect.height - circleRadius ) {
mouseY = rect.y + rect.height - circleRadius
};
}
},rect,
Circle {
centerX: bind circleX, centerY: bind circleY
radius: circleRadius
fill: Color.GRAY
}
]
fill: Color.BLACK
}
}
timeline.start();
It combines both the code, easing part and the constaint part :-). Virual box has been made by filling the rectangle same color as of frame. Now, you can play around with the coordinate value and check its working fine or not. Still some condition need to check like the outer box size should be bigger. Now, look at the code of Constraint.fx written in Netbeans 6.1 example :-P.















Hi,
My name is Varun Nischal and I'm the NetBean...
Hi Varun. Sure ! It will be my pleasure to do this...