Monday Mar 16, 2009

Play with Space - JavaFX

Spring season is about to come and one can feel the coldness in air. Last week, I went to my home and that week was quite good for my sample writing. Able to finish some of my pending job. 

Back to JavaFX Samples, last week I saw a nice sample written in flash about the ball motion in 3-D space. I try to implement that idea in JavaFX. Code is little dirty, so I will post it later. 

This is how it looks (animation is important) :

To watch the animation, please launch this JNLP.

I hope, you will enjoy it ! 

1. There are 4 polygon, which covers the space, looks like a cricket net practice place :).

2. Launch button will give motion to ball, since the motion is not restored, pressing it again will not work.

3. var scale = z0 / (z0 + z); Scaling with proper value will provide the Z-Camera.

4. Gaussian Blur for shadow, which translate in 2-D and with same timeline.


Change in Blog : Adding source code :

1. Main File

2. Ball Throw

 This will allow you to relaunch the animation.

Friday Dec 26, 2008

Colliding Balls + JavaFX

Here is an example of one more physics system - Colliding Balls. This is one of the most common examples we find everywhere. I have written this on transparent window. And so, if we increase the width and height of the stage + width, height variable in MotionBall.fx, it will give us a feeling like FlyingSaucer which keep on running all the way on Desktop. Here it looks on top of my code :D.

So, the netbeans window on back and application is running on top of it. Actually dragged out of the browser and running. Just for adding information, we can run an application from netbeans on browser by Properties -> Run -> Run in Browser. Remeber to set the size of applet correct else the application display will not full.

Initially position in this application has been generated randomly + Damping motion has been applied. So, in sometime all the balls will settle down on base. These all parameters can be adjusted in the file. Ball Effect has been generated as usual by Gradient Effect. Some variable may be unused because of future plan :D.

Here goes the code:

MotionBall.fx :

package collidingballs; import java.lang.\*; import javafx.scene.CustomNode; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.paint.\*; import javafx.scene.shape.\*; var spring : Number = 0.05; var gravity : Number = 0.05; var width : Number = 240; var height: Number = 320; var start: Number = 0; public class MotionBall extends CustomNode {     public var x: Number;     public var y: Number;     public var velocityX: Number;     public var velocityY: Number;     public var mass: Number;     public var radius: Number;     public var color: Color;     public function collision( balls: MotionBall[]) {         for(ball in balls) {             var dx : Number = ball.x - x;             var dy : Number = ball.y - y;             var distance : Number = Math.sqrt( dx \* dx + dy \* dy );             var minDist : Number = ball.radius + radius;             if( distance < minDist ) {                 var angle : Number = Math.atan2( dy, dx );                 var tx : Number = x + Math.cos( angle ) \* minDist;                 var ty : Number = y + Math.sin( angle ) \* minDist;                 var ax : Number = ( tx - ball.x ) \* spring;                 var ay : Number = ( ty - ball.y ) \* spring;                 velocityX -= ax;                 velocityY -= ay;                 ball.velocityX += ax;                 ball.velocityY += ay;             }         }     }     public function move(balls: MotionBall[]) {         velocityY += gravity;         x += velocityX;         y += velocityY;         if( x + radius >= width ) {             x = width + start - radius;             velocityX \*= - 0.9;         }         if( x - radius <= start ) {             x = start + radius;             velocityX \*= -0.9;         }         if( y + radius >= height ) {             y = height - radius;             velocityY \*= -0.9;         }         if( y - radius <= 40 ) {             y = 40 + radius;             velocityY \*= -0.9;         }     }     public override function create(): Node {         return Group {             content: [                 Circle {                     centerX: bind x,                     centerY: bind y,                     radius: bind radius                     fill: bind RadialGradient {                         centerX: 0.25                         centerY: 0.25                         proportional: true                         stops: [                             Stop {                                 offset: 0.0                                 color: color },                             Stop {                                 offset: 1.0                                 color: Color.BLACK },                         ]                     }                 }             ]         };     }

Main.fx :

package collidingballs; import collidingballs.MotionBall; import java.util.Random; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.scene.paint.Color; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.stage.StageStyle;

var balls: MotionBall[]; var rnd : Random = new Random(); for( i in [1..5] ) {     insert MotionBall {         x: rnd.nextInt( 240 ),         y: rnd.nextInt( 320 ),         radius: 30         color: Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255))     } into balls; } // timeline for colliding balls var t_collide = Timeline {     repeatCount: Timeline.INDEFINITE     keyFrames: [         KeyFrame {             time: 30ms             action: function() {                 for(ball in balls) {                     ball.collision(balls);                     ball.move(balls);                 }             }         }     ] }; Stage {     title: "Collding Balls"     style: StageStyle.TRANSPARENT     width: 240     height: 320     scene: Scene {         fill: Color.TRANSPARENT         content: [             balls         ]     } }

Thursday Dec 18, 2008

Initial Look of Physics App.

So, finally I decided to add all the motion(physics) application under one tree. Still I am not able to write too much of them but we will do it soon. Here are some which I merged into one and it looks like : 

Soon, the further work will be done. We will remove application like Carousel,Spring because its already in our repository. This application will also depicts 2 scene mode, how to remove content and add content dynamically.

Sorry for not posting the code, its in the mid and in a bad shape :).

Wednesday Dec 10, 2008

Spring Motion - Physics in JavaFX

3 weeks back, we were thinking of some cool application to make. I am a guy who has seen very less outside world, so coming up with some great idea is always tough for me. So, deciding that, I went back to my tenth class physics book and saw some of the cool physics motion. Its one of the tough subject and always screw me in exam. Searching some of the easy equation, I though to make one spring motion. Meantime, I though there is some spring motion residing in our repository. Actually one of the Josh applications do it in awesome way, but still we were missing the actual feel of Spring motion because of the gig-gag and spiral stuff attached to the wall and spring is going up and down in it, with a complete view of awesomeness :). This is what finally we achieve from this blog :

 I can still bet this can be 3 times much better than what you are seeing here. So, little of good news here that this sample can be executed on mobile

Here are the code files :

1. Main file.

2. Spring file.

3. SpringEquation file.

Enjoy FX'ing !


Vaibhav Hi, I am Vaibhav Choudhary working in Oracle with JDK team. This blog is all about simple concept of Java, JVM and JavaFX.


« July 2016