Friday, March 12, 2010
Garbage First prsentation - G1
http://developers.sun.com/events/techdays/presentations/locations-2009/hyderabad/td_hyd_garbagcollector_aroskar_choudhary.pdf
This time also we are talking on G1. Join us at tech days at Hyderabad.
If you are interested in knowing more about Garbage First or any Garbage collector algorithms, please let me know here.
JDK6u12 - Mixing heavy and lightweight component
So, if you are a Swing Developer, you have heard many stories where someone messed up Lightweight component with Heavyweight component. In one line " A heavyweight component
is one that is associated with its own native screen resource (commonly
known as a peer). A lightweight component is one that "borrows" the
screen resource of an ancestor (which means it has no native resource
of its own -- so it's "lighter")." AWT is all heavyweight, Swing is all lightweight except top level ones like JFrame, JWindow...
Now many times you have heard "Don't mix lightweight and heavyweight". What will happen ? Alright, here is a small code :
import javax.swing.*;
import java.awt.*;
public class Test extends JPanel {
public Test() {
JComboBox jc = new JComboBox();
JButton btn1 = new JButton("Button1");
Button btn2 = new Button("Button2");
Button btn3 = new Button("Button3");
jc.addItem("France");
jc.addItem("Germany");
jc.addItem("Italy");
jc.addItem("Japan");
add(jc);
add(btn1);
add(btn2);
add(btn3);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Test());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Here what you see the output:
Now, this is what it happen what you mix lightweight and heavyweight. No way, you can bring the drop down items on top of Button2 !!
All Past :) , JDK6 update 12 and JDK7 build 19, the output will be like this:
New JDK release fixed all these problem of mixing lightweight component and heavyweight component. So, don't worry, keep messing :).
For more detail, please see this : http://java.sun.com/developer/technicalArticles/GUI/mixing_components/index.html
For more: Please join us at Tech Days at Hyderabad on 24-25th of March.
LiveConnect Docs for JDK6
JDK6 has done lot of changes in LiveConnect. LiveConnect is a feature in the browser for communication between Java Applet and JavaScript. With the new Plugin2, most of the work has been left on browser to do. Initially it was Java which do a good amount of work. So, now the Java Plug-in will operate like any other scriptable Plug-in.
This is one of the great document written : http://java.sun.com/javase/6/webnotes/6u10/plugin2/liveconnect/
If you want to see some code help, visit: http://java2s.com/Code/Java/JDK-6/Script-Engines.htm
Have Fun !!Monday, August 24, 2009
Hyperlink in JavaFX
It's a long time being blogging. Actually not done anything new from long time :). Here is one simple concept which some guys asked me. We have provided hyperlink API in JavaFX 1.2 but some of us struggled to open a URL using hyperlink API.
Hmm, 2 ways to do it actually.
No1 : Use the Desktop API of JDK6. It's simple to use. One example is here.
So, very basic code will go like this :
package sample2;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.*;
import java.net.*;
Stage {
title: "HyperLink to URL"
width: 240
height: 320
scene: Scene {
content: [
Hyperlink{
translateY: 160
translateX: 40
width: 150
text: bind "Visit javafx Samples! "
action: function():Void{
java.awt.Desktop.getDesktop().browse(new URI("http://javafx.com/samples"));
}
}
]
}
}
So, 2 things for running this code. First,Desktop API has been added in JDK6, so this code won't run on JDK5. Second, Add rt.jar(rt.jar of JDK6) file in the Libraries if you are using Netbeans
No2 : For only JavaFX code, we can use AppletStageExtension like this :
package sample1;import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.stage.AppletStageExtension;
Stage {
title: "Hyperlink to URL"
width: 250
height: 80
scene: Scene {
content: [
Hyperlink {
text: "JavaFX Samples !"
action: function() {
AppletStageExtension.showDocument("http://javafx.com/samples");
}
}
]
}
}
In this case, you cant send hyperlink from Desktop Application, but it will work fine for applet or Browser application. So, best is to use this and then use our normal funda : if {__PROFILE__}" != "browser") --> use the Desktop API code. What you say :).
Please let me know if there is any issue in the code ! Or also if there is any better way to do this.
Java Debugging basics
1. Application is crashing : Most miserable one. Get your log file, try to analysis log. How to write log file, use Java Logger API. Java Logger had been introduced in JDK 1.4.2. The most awesome feature of Logger API is that you can use it in production without much overhead. The overhead is controlled by something called level in API. Level goes from FINEST to SEVERE. You can refer to O'Relly Book "Java, In a NutShell". I guess, it covers Logging API into a great detail.
Lot many things to know: Standalone application is crashing or web application. Its crashing with -client or -server option, appletviewer or browser, plugin or plugin2.
2. Application Hang: Most prominent reason for Hang is thread related. I don't know too many language but Java handles thread in most graceful way. What we can do when a Java Process or Application Hang:
- Hmm, get stack trace at Java and native level.
- Get to know current thread conditions and their status.
- Try to get core dump. Sometime, application will refuse to give you.
- Get to know machine detail. Almost all OS, use different thread model. Not only that in Solaris, 8 and 9 use different models. Sometime, it narrow down the problem, if you are luck :).
How to get all these information. Easy actually. Take help of Java Debugger, jdb(which is a part of JDK). In jdb, you can run the command like threads, thread, dump and many more. Take help of Windbg, if its a windows machine for native level. I find it useful and painful.
3. Your application is drinking memory: There is some memory leak. Best is to use jhat(part of JDK). First take a heap dump by using jmap, jconsole hprof. Pass that heap dump to jhat and it will bring a server and
dump all the information. Analyze where most of the memory is going. Writing to track the place from where memory is going. What is GC response on it. Change some arguments of GC and then give a try.
There are lot of JDK tools that help in analyzing JVM, threads, memory, process. See the list : http://java.sun.com/javase/6/docs/technotes/tools/
Garbage collector play important role in all of them, so always get to know what is best for what:- http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html (Even you know less about GC, you can't resist yourself finishing this page, this is so interesting). In terms of GC, everything is improving day be day and we have new GC, Garbage First aka G1 ready. With Vikram, I had one presentation on G1 in Sun tech days - http://developers.sun.com/events/techdays/presentations/locations-2009/hyderabad/td_hyd_garbagcollector_aroskar_choudhary.pdf.
If you have any wired debugging story related to Java, please share :). Also, visit Visual VM which is meant to integrate all the command line tools of JDK.
Tuesday, December 30, 2008
Z-Order is supported in JavaFX !
While writing some of the samples in which we have to play with images, we sometimes has to manage the depth of the images. Like for the Carousel example, every image has a depth. In that example, actually images are not overlapping with each other, so we never need to write the Z-Order concept. But if someone want to write a Carousel or some application in which Images are residing over other images, we need to set the Z-order of Images. Z-Order in literal term means depth-ness of images. JavaFX gracefully provide API's to set the Z-order of images. With a simple call, you can set the images toFront or toBack features.
In this example, I have taken 3 images and try to set the depth-ness of images on the event of Buttons.

First Image on Top Second Image on Top
Third Image on Top
Here is the code to set the Z-Order :
package zorder;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.input.MouseEvent;
import javafx.ext.swing.SwingButton;
var im1 = ImageView {
x: 100
y: 100
image: Image {
url: "{__DIR__}im1.PNG"
}
opacity: 0.8
};
var im2 = ImageView {
x: 130
y: 130
image: Image {
url: "{__DIR__}im2.PNG"
}
opacity: 0.8
};
var im3 = ImageView {
x: 160
y: 160
image: Image {
url: "{__DIR__}im3.PNG"
}
opacity: 0.8
};
var gp = Group {
content:[
im1, im2,im3
]
}
Stage {
title: "Application title"
width: 400
height: 400
scene: Scene {
fill: Color.BLACK
content: [
gp,
SwingButton {
translateX: 10
translateY: 10
text: "Image 1"
action: function() {
im1.toFront();
}
}
SwingButton {
translateX: 90
translateY: 10
text: "Image 2"
action: function() {
im2.toFront();
}
}
SwingButton {
translateX: 170
translateY: 10
text: "Image 3"
action: function() {
im3.toFront();
}
}
]
}
}
From next blog, I will use applet or JNLP in place of images, as suggested by Dmitry in last blog. Pictures make it bulky and uneasy to load. But I was getting some problem in deploying the application on Sun blog which will be rectified soon.
Monday, December 22, 2008
JavaFX production Suite - How it work
Here is the little discussion on Designer + Developer workflow in JavaFX. Powered with Project Nile, we can export data from PhotoShop or Illustrator. Actually the complete production suite is awesome and provide developer and designer to work in parallel. Here how it is :
So, Developer can work on the business logic and till that time designer can design the actually content for developer. Finally it will merge in a great style.
Basic Requirement to do :
1. JavaFX Production Suite : Download from the start section of javafx.com.
2. For Designer : Any tool, either PhotoShop CS3 or Illustrator CS3. Officially CS3 is the supported platform but it works for CS4 as well.
3. For Developer : Java FX SDK: Download from the start section of javafx.com
Now, I am going ahead with PhotoShop. Copy the plugin from JavaFX production suite to PhotoShop. Run the PhotoShop, in export it will give you a save option in JavaFX, which basically saves the file in fxz format(a new format, why Sun need a new format, there is a lot of discussion and Jeet has pointed some reason in his blog).
Alright, so work started :
I was watching the batman movie, so decide to take his awesome car, which is here :

In photoshop, I have changed the hue and exported all in fxz format.
Then I made a Netbeans Project, Copy the fxz file into the project space. We can now click on fxz file, we can see the preview and code as well. Right now, if we put some of the complex features of PhotoShop, I am afaird to say JavaFX will not catch those changes.
So, my fxz file(JavaFX.fxz) looks like this :
/*
* Generated by JavaFX plugin for Adobe Photoshop.
* Created on Fri Dec 19 19:17:33 2008
*/
//@version 1.0
Group {
clip: Rectangle { x:0 y:0 width:576 height:432 }
content: [
ImageView {
opacity: 1.0
x: 0
y: 0
image: Image {
url: "{__DIR__}Background.png"
},
},
]
}
Actually in my case there was nothing, so it generated a simple code :).
Now, I have made another file, calling it CarRotate.fx :
package psfx;
import java.lang.*;
import javafx.fxd.FXDLoader;
import javafx.scene.*;
import javafx.scene.input.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.*;
var group = Group {
content: []
};
var fxdContent = FXDLoader.load("{__DIR__}JavaFX.fxz"); // loads the content
insert fxdContent into group.content; // inserts the fxd content into the group
Stage {
title: "JavaFX Invaders"
resizable: true
width: 700
height: 700
onClose: function() {
System.exit (0);
}
scene: Scene {
content: [
group
Rectangle {
x: 330,
y: 500
width: 60,
height: 30
fill: Color.GRAY
onMouseClicked: function( e: MouseEvent ):Void {
fxdContent.rotate = 90;
}
}
]
}
};
Some part of the code is point of interest :
var group = Group {
content: []
};
var fxdContent = FXDLoader.load("{__DIR__}JavaFX.fxz"); // loads the content
insert fxdContent into group.content; // inserts the fxd content into the group
Here I have loaded the .fxz file into var fxdContent which is a node and node means things are in our hand. I have simply written a rotate equation on a button click which is working nicely.
We can see the rotated car and hue which is the asset of PhotoShop in Green color. Huh, finally its done. Sorry, for posting bad example.
JavaFX - Developer and Designer work !
One more example of JavaFX production Suite. Though the complete thing can be done in Photoshop alone but I am just giving an example. I have made a house in Photoshop, which is not very good :(, but fair enough. And I animated the star effect in JavaFX.
So, here is my home in photoshop :

Actually this is funny, I was following a tutorial to make house and in temptation, I made shadow as well, but there is no meaning of shadow in night :). Now, I filled star sparking effect in this from JavaFX.

Filling star effect need same which we have written for sparkling glasses. Just some changes here and there. In the last blog we have already discussed how to import work from Photoshop.
Here are the things to download:2. Code (Main.fx, Star.fx)
Lot many things can be done. But I don't know Photoshop.
Wednesday, December 10, 2008
Physics Motion - Spring In JavaFX

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

Regret to say, ball will not look like a real 3D ball in FX Mobile because of Bug ID: RT-2205, which basically speaks that Mobile Runtime don't understand Radial Gradient, hoping this will be fixed soon.
Here are the code files :
1. Main file.
2. Spring file.
3. SpringEquation file.
Enjoy FX'ing !
Tuesday, December 09, 2008
3D E'FX's in JavaFX
Me and Vikram was looking today some of the cool flash examples and I have seen the button effect at some place, when you press the button it really goes like inside and coming out. But that was an effect achieved by the images(two different images, one unpressed button and one pressed button) and then we thought to simulate this effect by code. Somehow we are able to do that in FX, here is the final outcome:


What we have tried to do is pressing one button will put the other in unpressed mode and vice-versa. This has been achieved by some of the cool API's of JavaFX. And we have used the DistantLight effect of JavaFX which gives a lighting effect in its awesome way. Actually this can be more cooler but I left that for developer to modify it according to their need :). But this is a modular code and can be used in any of the button place.
Here is the simple code for the same(again code is not written in the most optimized way but in the best way for understanding) :
package lighteff;
import javafx.scene.effect.light.DistantLight;
import javafx.scene.effect.Lighting;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
var factor = 5;
var scale = 1.0;
var factor1 = 10;
var scale1 = 0.85;
Stage {
title: "Control Panel"
width: 290
height: 180
style: StageStyle.UNDECORATED
scene: Scene {
fill: Color.BLACK
content: [
Group {
effect: Lighting {
light: DistantLight {
azimuth: 90
elevation: 60
}
surfaceScale: bind factor
}
content: [
Circle {
centerX: 100,
centerY: 100
radius: 40
fill: Color.RED
onMousePressed: function( e: MouseEvent ):Void {
scale = 0.85;
factor = 10;
scale1 = 1.0;
factor1 = 5;
}
},
Text {
fill: Color.WHITE
scaleX: bind scale
scaleY: bind scale
font: Font {
size: 24
}
x: 71,
y: 105
content: "Press"
}
]
},
Group {
effect: Lighting {
light: DistantLight {
azimuth: 90
elevation: 60
}
surfaceScale: bind factor1
}
content: [
Circle {
centerX: 200
centerY: 100
radius: 40
fill: Color.BLUE
onMousePressed: function( e: MouseEvent ):Void {
scale1 = 0.85;
factor1 = 10;
scale = 1.0;
factor = 5;
}
},
Text {
fill: Color.WHITE
scaleX: bind scale1
scaleY: bind scale1
font: Font {
size: 24
}
x: 171,
y: 105
content: "Press"
}
]
}
]
}
}
Monday, December 01, 2008
Java Plugin2 - Docs !
With Plug-in 2(Java 6u10), a whole new experience comes into Java Plug-in. I have mentioned the new features of Plugin2 in some of my presentations of 6u10.
For detail, please visit this link : http://java.sun.com/javase/6/webnotes/6u10/plugin2/index.html
Switching between old/new plugin:
Java Control Panel - Advanced - Java Plug-in - Enable the next gen plug-in.
(Check this link : http://www.java.com/en/download/help/new_plugin.xml)
By default, it will take new plugin, but untick it for old plugin.
Scene to Scene in JavaFX
Any middle or big application demands to change one window to other at some point of time. A window type of thing in JavaFX is represented by Scene and its each to switch between scene or to run multiple scenes.
Here is a small application in which clicking on image will put you in another window, written "Hello World"
package sample6;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
var s_new:Scene;
var s = Scene {
content: [
Text {
font: Font {
size: 24
}
x: 10,
y: 30
content: "HelloWorld"
}
]
};
var s1 = Scene {
content: [
ImageView {
image: Image {
url: "{__DIR__}im2.PNG"
}
onMouseClicked: function( e: MouseEvent ):Void {
s_new = s;
}
}
]
};
s_new = s1;
Stage {
title: "Application title"
width: 250
height: 280
scene: bind s_new
}
So, its simple, on mouse click, I have bind a scene variable with a new scene. That's it !
Moving image from MouseDrag
So, we got a discussion here. Last week we(me, Subrata and Vikram, both my office colleagues) are discussing about dragging an image with mouse pointer in JavaFX.
So, this was the first code. Point is to drag an image from the same place where we first hit the mouse, like it happens when we drag a folder :
package sample5;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import java.lang.System;
var x: Number;
var y: Number;
var im = Image {
url: "{__DIR__}im2.PNG"
};
var temp1:Number = 0;
var temp2: Number = 0;
var count: Integer = 1;
Stage {
title: "Application title"
width: 250
height: 280
scene: Scene {
content: [
ImageView {
x: bind x - temp1
y: bind y - temp2
image: Image {
url: "{__DIR__}im2.PNG"
}
onMouseDragged: function( e: MouseEvent ):Void {
x = e.x;
y = e.y;
if(count <= 1) {
temp1 = e.x;
temp2 = e.y;
}
count++;
}
}
]
}
}
You can see those patches of counts and flags which makes the code so unstable. And a bug, when you leave the mouse once, it cant grip the image from your mouse point again.
Subrata has written a cleaner code which works correct and here it is :
package mousedrag;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
/**
* @author Subrata Nath
*/
var imgX : Number = 20;
var imgY : Number = 20;
var startX : Number;
var startY : Number ;
var distX : Number;
var distY : Number ;
Stage {
title: "Mouse smooth drag"
width: 250
height: 280
scene: Scene {
content: [
ImageView {
x : bind imgX
y : bind imgY
image: Image {url: "{__DIR__}Mail.png"
}
onMousePressed: function( e: MouseEvent ):Void {
startX = e.x;
startY = e.y;
// Calculate the distance of the mouse point from the image top-left corner
// which will always come out as positive value
distX = startX - imgX;
distY = startY - imgY;
}
onMouseDragged: function( e: MouseEvent ):Void {
// Find out the new image postion by subtracting the distance part from the mouse point.
imgX = e.x - distX;
imgY = e.y - distY;
}
}
]
}
}
Sparkling Glass
What to achieve: A sparkling glass(beer glasses are clean and shiny before beer get served) like this.

So, our aim is to make those sparkling effect on glass. Here is the code in JavaFX(things are little hard coded but better for understanding):
package sample;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.input.MouseEvent;
import javafx.scene.transform.Rotate;
import javafx.scene.shape.Polygon;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;
import javafx.scene.shape.Circle;
import javafx.scene.Group;
/**
* @author Vaibhav
*/
var r = 0.0;
var t = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames: [
KeyFrame {
time: 3s
canSkip: true
values: [
r => 360.0 tween Interpolator.EASEBOTH
]
}
]
}
t.play();
var op = 1.0;
var t1 = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames: [
KeyFrame {
time: 3s
canSkip: true
values: [
op => 0.0 tween Interpolator.EASEBOTH
]
}
]
}
t1.play();
Stage {
title: "Sparkling on Glass"
width: 250
height: 480
scene: Scene {
fill: Color.BLACK
content: [
ImageView {
image: Image {
url: "{__DIR__}wineglass.png"
}
}
Polygon {
rotate: bind r;
translateX: 130
translateY: 100
scaleX: 0.5
scaleY: 0.5
points: [ 0,0, 2,-50, 4,0, 54,2,4,4,2,54,0,4,-50,2]
fill: Color.WHITE
opacity: bind op
},
Polygon {
rotate: 45;
scaleX: 0.25
scaleY: 0.25
translateX: 130
translateY: 100
points: [ 0,0, 2,-50, 4,0, 54,2,4,4,2,54,0,4,-50,2]
fill: Color.WHITE
opacity: bind 1 - op
},
Polygon {
rotate: bind r;
translateX: 50
translateY: 50
scaleX: 0.5
scaleY: 0.5
points: [ 0,0, 2,-50, 4,0, 54,2,4,4,2,54,0,4,-50,2]
fill: Color.WHITE
opacity: bind op
},
Polygon {
rotate: 45;
scaleX: 0.25
scaleY: 0.25
translateX: 50
translateY: 50
points: [ 0,0, 2,-50, 4,0, 54,2,4,4,2,54,0,4,-50,2]
fill: Color.WHITE
opacity: bind 1 - op
},
Polygon {
rotate: bind r;
translateX: 30
translateY: 120
scaleX: 0.5
scaleY: 0.5
points: [ 0,0, 2,-50, 4,0, 54,2,4,4,2,54,0,4,-50,2]
fill: Color.WHITE
opacity: bind op
},
Polygon {
rotate: 45;
scaleX: 0.25
scaleY: 0.25
translateX: 30
translateY: 120
points: [ 0,0, 2,-50, 4,0, 54,2,4,4,2,54,0,4,-50,2]
fill: Color.WHITE
opacity: bind 1 - op
},
]
}
}
Same animation in flash is here : http://www.entheosweb.com/Flash/sparkling_effect.asp
Java Market Trend
Today I got a mail from Carl. He works in a company which provides Marketing stats. Here are some of the exciting marketing stats of Java :
1. Median Salary :
| Generated By: |
|
2. Jobs Trend line
| Compare Market Statistics | Generated By: |
3. Salary Histogram :
| Compare Market Statistics | Generated By: |
Now, see the market increase in Java Gaming.And there is a reason behind it, people start realizing that Java is fast and a suitable language for gaming. It happened for quite a long time developed used to think that Java is a slow language and not meant for game.
| Compare Market Statistics | Generated By: |
Banking sector, this is quite obvious because of the market fall down, else it was little above than the last year performance.
| Compare Market Statistics | Generated By: |
Defense: This is quite an interesting market. It speaks about the robustness and security of Java. Defense finds it the best language for their purpose.
| Compare Market Statistics | Generated By: |
For more visit this link : Java Market Stats
Saturday, October 18, 2008
JavaFX – Getting Started
Alright, here is the blog for the beginner of JavaFX. We generally say it “getting started”
So, right now I suggest you to work with Netbeans 6.1. There is a good amount of code change going to happen soon in JavaFX but never mind. Here we go:
- Start Netbeans 6.1. Go into Tools -> Plugin section. Go into Available Plugins, search for JavaFX in the search box. You may see more than this, but whatever comes on the name of JavaFX, install all (I already have it in installed section).
- After installation, go into File -> New Project Section. You can see JavaFX now in project section:
- Click on Next, It will ask for Project Name. Enter a project name “JavaFXApplication” (this is what I have written) and click finish. You will get something like this:
- But the most important thing is what you get on right most side with a caption “Palette” (FX Builder). Here how it looks:
- In place of “place your code here”, we need to write our code. So, let’s start with “Drag and Drop” work. First we will make a circle. So, drag a Frame into the code space and then drag a circle in the content section of code.
- Before running the code, you can see the preview of output, by clicking on the preview button.
- This is how the preview will come:
- After you convince with the output, go into the Run Section. Run the application or press F6.
In the next blog, we can discuss on how to use FX builder to make more wise applications like running a ball, making 3D balls, image gallery or something else, depending on the demand :).
Please let me know if there is/are some problem(s) in installation or running the application.If you are not a netbeans user, the you have two option :
- Wise one, download netbeans and start working :).
- Unwise one, we can also work only with javafx preview developer by putting the code in bin folder compiling with javafxc and running with javafx. But then you will not be able to use lot of netbeans features.
Sunday, September 14, 2008
JavaFX Presentation
Today we had a good BOJUG meet at Thoughtworks. I have presented my small JavaFX slides. Followed to that, Sathish has given a presentation on Bean Binding and Bean Validation. He talked about JSR 295 + JSR 303. Quite a handsome presentation. After this, Sriram has taken the presentation on Tomcat internal and it seems he really went into internal. He talked about how to write own ClassLoader in tomcat + how to provide a webapps on fly, many more. It was a nice presentation without presentation slides :-).
Here is my presentation. Please feel free to comment.Filling eyes effect in JavaFX

package eyes;
import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.geometry.Arc;
import javafx.scene.geometry.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.*;
import javafx.scene.effect.*;
var y: Number = 150;
Frame {
title: "Golden Eyes"
width: 500
height: 500
closeAction: function() { java.lang.System.exit( 0 );
}
visible: true
stage: Stage {
fill: Color.GRAY;
content: [
Circle {
centerX: 160, centerY: y
radius: 23
fill: LinearGradient {
startX: 0.0
startY: 0.0
endX: 1.0
endY: 0.0
proportional: true
stops: [
Stop { offset: 0.0 color: Color.GOLD },
Stop { offset: 1.0 color: Color.BLACK }
]
}
opacity: 0.9
},
Circle {
centerX: 160, centerY: y
radius: 10
fill: Color.BLACK
},
Circle {
centerX: 166, centerY: y - 5
radius: 5
fill: Color.WHITE;
effect : GaussianBlur {
radius: 6
}
},
Circle {
centerX: 250, centerY: y
radius: 23
fill: LinearGradient {
startX: 0.0
startY: 0.0
endX: 1.0
endY: 0.0
proportional: true
stops: [
Stop { offset: 0.0 color: Color.BLACK },
Stop { offset: 1.0 color: Color.GOLD }
]
}
opacity: 0.2
},
Circle {
centerX: 250, centerY: y
radius: 10
fill: Color.BLACK
},
Circle {
centerX: 244, centerY: y - 5
radius: 5
fill: Color.WHITE;
effect : GaussianBlur {
radius: 6
}
},
Circle {
centerX: 200, centerY: 180
radius: 100
fill: Color.SIENNA
opacity: 0.1
},
Polyline {
stroke:Color.BLACK
points: [
200,160,
220.0,220.0,
180.0,220.0
]
},
Path {
fill: LinearGradient {
startX: 0.0
startY: 0.0
endX: 1.0
endY: 0.0
proportional: true
stops: [
Stop { offset: 0.0 color: Color.BLACK },
Stop { offset: 1.0 color: Color.RED }
]
}
elements: [
MoveTo { x: 160 y: 240 },
ArcTo { x: 250 y: 240 radiusX: 100 radiusY: 100},
]
},
]
}
}
Tracing the ball path in Java FX !

package tracemotion;
import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.application.Frame;
import javafx.application.Stage;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.geometry.*;
import javafx.scene.effect.*;
import javafx.scene.paint.*;
import java.lang.System;
import java.lang.Math;
var t : Number = 0.0;
Frame {
var input : TracingBall = TracingBall {};
stage : Stage {
fill: Color.BLACK;
content : bind [
input
]
}
visible : true
title : "Tracing Ball"
width : 600
height : 600
closeAction : function() { java.lang.System.exit( 0 ); }
}
class TracingBall extends CustomNode {
attribute tracingballs : Circle[];
attribute length : Integer = 600;
attribute timer : Timeline = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames :
KeyFrame {
time : 100ms
action : function() {
update();
t = t+0.3;
}
}
}
public function update() : Void {
for( i in [0..length - 30] ) {
tracingballs[i].centerX = tracingballs[i+30].centerX;
tracingballs[i].centerY = tracingballs[i+30].centerY;
tracingballs[i].radius = tracingballs[i+30].radius;
tracingballs[i].opacity=0.4;
}
tracingballs[length] = Circle {
centerX : bind(100 + t * 30),
centerY : (300 + Math.cos(t) * 100),
radius : 30,
fill : bind LinearGradient {
proportional: true
stops: [
Stop { offset: 0.0 color: Color.RED },
Stop { offset: 1.0 color: Color.GAINSBORO },
]
},
opacity : 1.0
};
}
public function create(): Node {
return Group {
content : bind[tracingballs]
};
}
init {
for( i in [0..length] ) {
insert Circle { fill : bind LinearGradient {
proportional: true
stops: [
Stop { offset: 0.0 color: Color.RED },
Stop { offset: 1.0 color: Color.GAINSBORO },
]
},
} into tracingballs;
}
timer.start();
}
}
Some lines in code are filling those fancy colors in ball(some lines are hard coded as well).I am a real bad guy in filling nice catchy colors(this color looks like a sun and a moon combination). Any comments/improvements are welcome !
2+2=4 and so 2*2
Last week, our office celebrates OpenSource Mela. In code war, we got a programming contest in which the problem statement is :
5 * 4 / 2 - 5 + 2 = 7 (evaluated from left to right). So, input format is:
5 4 2 5 2 7
and output is to put into a valid expression format(all possible format). So, if its 2 2 4, then 2+2=4 and 2*2=4 is possible. I have written some code for this, which goes here :
package problemstatement1;import java.util.List;
public class Main {
String input = "2 0 2";
String output = new String(); // 5 * 4 / 2 - 5 + 2 = 7
int resultVal = 0;
String operatorseq = new String();
int result = 0;
int totalcounter = 0;
boolean flag = true;
// converting input into easy format
String[] inputtoken = input.split(" ");
int[] numberseq = new int[inputtoken.length - 1];
int totaloperator = numberseq.length - 1;
public void isValid() {
if (inputtoken.length < 3) {
System.out.println("Usages ... Input Parameter should be three or more ");
System.exit(0);
}
}
public void processInput() {
try {
resultVal = Integer.parseInt(inputtoken[inputtoken.length - 1]);
} catch(NumberFormatException e) {
System.out.println("Parsing error" + " " + e);
System.exit(0);
}
for (int i = 0; i < numberseq.length; i++) {
try {
numberseq[i] = Integer.parseInt(inputtoken[i]);
} catch(NumberFormatException e) {
System.out.println("Parsing error" + " " + e);
System.exit(0);
}
}
}
public void showInput() {
for (int i = 0; i < numberseq.length; i++) {
// System.out.println(numberseq[i]);
}
}
public void getPermutation() {
GenerateOperators gen = new GenerateOperators("+-*/", totaloperator);
List
System.out.println("Possible Solutions");
for (int i = 0; i < v.size(); i++) {
operatorseq = v.get(i);
manupulate();
}
}
public void manupulate() {
result = 0;
for (int i = 0; i < operatorseq.length(); i++) {
if (i == 0) {
if ((operatorseq.charAt(i)) == '+') {
result = result + (numberseq[i] + numberseq[i + 1]);
}
if ((operatorseq.charAt(i)) == '-') {
result = result + (numberseq[i] - numberseq[i + 1]);
}
if ((operatorseq.charAt(i)) == '*') {
result = result + (numberseq[i] * numberseq[i + 1]);
}
if ((operatorseq.charAt(i)) == '/') {
try {
result = result + (numberseq[i] / numberseq[i + 1]);
} catch(Exception e) {
flag = false;
// don't do anything
}
}
} else {
if ((operatorseq.charAt(i)) == '+') {
result = result + numberseq[i + 1];
}
if ((operatorseq.charAt(i)) == '-') {
result = result - numberseq[i + 1];
}
if ((operatorseq.charAt(i)) == '*') {
result = result * numberseq[i + 1];
}
if ((operatorseq.charAt(i)) == '/') {
try {
result = result / numberseq[i + 1];
} catch(Exception e) {
flag = false;
// don't do anything
}
}
}
}
if (result == resultVal && flag == true) {
totalcounter++;
System.out.println("");
for (int i = 0; i < numberseq.length - 1; i++) {
System.out.print(numberseq[i] + "" + operatorseq.charAt(i));
}
System.out.print(numberseq[numberseq.length - 1]);
System.out.print("= " + result);
}
}
public void count() {
if(totalcounter == 0 ) {
System.out.println("NO EXPRESSION POSSIBLE");
System.exit(0);
}
else {
System.out.println("");
System.out.println("Total Possible Solution :" + totalcounter);
}
}
public static void main(String[] args) {
Main main = new Main();
main.isValid();
main.processInput();
main.showInput();
main.getPermutation();
main.count();
}
}
and one more file which is :
package problemstatement1;
import java.util.List;
import java.util.ArrayList;
public class GenerateOperators {
private String a;
private int n;
public GenerateOperators(String a, int n) {
this.a = a;
this.n = n;
}
public List
int l = a.length();
int permutations = (int) Math.pow(l, n);
char[][] storeCombination = new char[permutations][n];
for (int x = 0; x < n; x++) {
int temp = (int) Math.pow(l, x);
for (int p1 = 0; p1 < permutations;) {
for (int al = 0; al < l; al++) {
for (int p2 = 0; p2 < temp; p2++) {
storeCombination[p1][x] = a.charAt(al);
p1++;
}
}
}
}
List
for (char[] permutation : storeCombination) {
result.add(new String(permutation));
}
return result;
}
public static void main(String[] args) {
GenerateOperators gen = new GenerateOperators("AAAMMBR", 7);
List
for (int i=0;i
System.out.println(s1);
}
}
}
Feel free to use this code :-).
