Probably I missed something. But this is the way we can add
indefinite progress indicator with any unpredictable events. Here I am
just calling a Web service to show yahoo maps for NY city.
Here how progress indicator looks like : ..........
A simple example how ListView works in JavaFX 1.2. I guess it need more
feature in next release. But it do basic stuff like providing scrollbar
if list is long, selected item actions.Something like this :
Here is the simple code, in which I am adding text ..........
It has been
used in lot of samples but if you are finding it tough to grab out the
code from sample to use. Here is a simple(simplest actually :) ) code
to use scrollBar. I have create array of rectangle and associated
scroll bar with it. Something like this ..........
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....
As already discussed, JavaFX 1.2 provide API set for Charts and Graphs. Though I decided to put my hand dirty in writing one 3D piechart from my own. With mine, you will get additional feature of explode in and out feature :). Well, action can be written with the existing chart API and I guess explode feature will also come soon.
Making 3D Pie chart is nothing but layering of 2D Pie Chart and here goes a small code :
Slice.fx
package piechart3d;
import java.lang.Math;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
/**
* @author Vaibhav Choudhary
*/
public class Slice extends CustomNode {
public var color: Color;
public var sAngle: Number = 0.0;
public var len: Number = 0.0;
public var xt = 0.0;
public var yt = 0.0;
function explodout():Boolean {
var t = Timeline {
repeatCount: 1
keyFrames: [
KeyFrame {
time: 0.25s
canSkip: true
values: [
xt =>
30 * Math.cos(2 * Math.PI * (sAngle + len / 2) / 360) tween Interpolator.EASEBOTH,
yt =>
-30 * Math.sin(2 * Math.PI * (sAngle + len / 2) / 360) tween Interpolator.EASEBOTH
]
}
]
}
t.play();
return true
}
function explodein():Boolean {
var t1 = Timeline {
repeatCount: 1
keyFrames: [
KeyFrame {
time: 0.25s
canSkip: true
values: [
xt => 0,
yt => 0
]
}
]
}
t1.play();
return true
}
public override function create(): Node {
return Group {
blocksMouse: true
translateX: bind xt
translateY: bind yt
onMouseClicked: function( e: MouseEvent ):Void {
if(xt == 0 and yt == 0) {
explodout();
}
else
explodein();
}
content: for(num in [0..25]) {[
Arc {
stroke: color
cache: true
fill: color
translateX: 0
translateY: (num + 1) * 1
centerX: 250
centerY: 250
radiusX: 150
radiusY: 60
startAngle: bind sAngle
length: bind len
type: ArcType.ROUND
}
Arc {
cache: true
fill: LinearGradient {
startX: 0.3
startY: 0.3
endX: 1.0
endY: 1.0
stops: [
Stop {
color: color
offset: 0.0
},
Stop {
color: Color.WHITE
offset: 1.0
},
]
}
centerX: 250
centerY: 250
radiusX: 150
radiusY: 60
startAngle: bind sAngle
length: bind len
type: ArcType.ROUND
},
]
}
};
}
}
Anything here can be made generic to any extend. There is a for loop of 0..25 in Slice.fx which speaks about the thickness of chart :) and some mathematics in timeline speaks about explode feature.
Now if you compare this with PieChart that comes in API, you will see this has some jerky corners + Color combination is not as soluble. How that has been made is secret :).
One of the most effective API's got introduced into the Marina(JavaFX 1.2) release are Control and Chart API's. As there are lot of questions on chart API, here is the small code for making some charts, it almost follow the same protocol with all chart API's.
We can do hell lot of things with it. I have just used the default and shown how action() can be written on slices.
Here goes the bubble chart, basic funda is same, insert the number array into BubbleChart.Data. Get all the Data into series and get that series into the Chart API. For more, please see the source Code.
More will be posted soon, or a jumbo one with all of it in one go ! You can download the latest SDK from here.
Just a small demo of JavaFX + Web Services. From JavaFX, its quite easy to work on Web Services like maps, search, pictures .. There are lot of Web services demo in the sample space.
Here I have contact list of person, which has name, email ID, phone no, address and thumbnail images. Everything has a different work but here we are willing to see the address work. On clicking contact list's, will show me his location in maps. Actually there are two ways to do that, fast way and good way. We can store the maps in Database and use it when required, other is we call the web service and take the map on demand from Yahoo or Google API, which will be slow. So, we will go with good way.
Considering only one contact.
Some fancy stuff, like clicking the + sign will open address option. Now, on clicking the address, we will call the yahoo maps API for showing the location of the person Joseph J.
Actually its a mobile application, and can be viewed in mobile as well(just that I am not putting image). So, in basic code of Map show, we did :
// in place of Sunnyvale and CA, it will be {city} and {state} which will be the variables and passed from main code.
var url = bind "http://local.yahooapis.com/MapsService/V1/mapImage?appid=GetApp;city=Sunnyvale&state=CA";
var p: PullParser; var h: HttpRequest; init { if (url.length() > 0) { h = HttpRequest { location: url onException: function(exception: Exception) { }
onInput: function(input) {
p = PullParser { input: input onEvent: function(event) {
if ((event.type == PullParser.END_ELEMENT)) { if (event.qname.name == "Result") { location = event.text; //this will give a URL for image } } } }; p.parse(); p.input.close(); .......
Lot of further code. Important of it is OnDone method where we have to write some code of cleaning or loading prev. data. I have also embed my long back code of navigation, which will help us into navigation in the map.
Ignore some minor mistakes, have a look at the jnlp:
Soon, I will post the whole application with code, but the basic idea is simple and can be doable.
Often we required some shapes which are not in the list of Basic Shapes available in JavaFX. I mean this is true with any language. A month back, I wanted to make a tube in JavaFX and Tube has very awesome cut at the top which can't be done with basic shapes. We can easily tackle these conditions by intersecting and subtracting the shapes. Have a look :
Now, this is very easy to make. 1 subtraction and that's it. This is just 2 ellipses and 1 Shape subtraction ! Find the code :
Even in this, we can play more and make it as real as it is desired. If you see my tube closely, you will find a green color on the top which gives a feeling that the inside color or liquid inside the tube is green in color. Rotating this tube with 90 and removing the lower base can be helped in the simulation of Pipes. With this only, I had written one small code, showing how light travel in pipes. I will post that code as well in few days.
Point is JavaFX has a strong support for basic shapes but if your desired shape doesn't resides in the list. Use ShapeIntersect and ShapeSubtract for acheiving the desired result.
If you are interest in seeing the liquid filling in this tube, please visit this article.
JavaFX being an easy language, one complex part is to write proper timeline for animation. Though its quite easy but as beginner I feel problem sometime. And sometime as a Java Developer, we start demanding those things which are generally done by the concept of multi-threading in Java. Remember, JavaFX is single threaded application.
So, this is what I generally follow. Say, If I have a class Ball, which has a circle and every ball has a timeline for its own. Now, if you want One ball move after another ball, I write a master timeline in Main file and there we write something like this :
Where there is an animation of 1s in t1.t.playFromStart(); So, the next timeline call goes at 1s, means finishing at first one. If you want some initial delay, you can write :
But, It is possible that you messed up after sometime. For that, you need to check the "javafx.animation.transition" package, one of the awesome packages in FX API's.
Though, example is everywhere in API Doc. I just show a small one, copied from the API' example itself. First car will fade, then move left and right, then rotate, then move again and then zoom.
This is combination of my old examples. But here we can easily see how to delete, insert items from window at runtime.
I have integrated 6 of the physics motion in one tree. Actually it makes things little complex, say we have colliding balls on one button and when I go somewhere and come back to colliding balls, it should colliding from begin. So, for some motion we need to restore initial position again.
Run the JNLP here :
There are lot of ways, you can use Lighting effects in JavaFX. All of them have its own use like Spot Light, Distant Light or Point Light. One small sample to show how Spot Light works.
Somewhere in someone blog, I read this "Complex thing should be doable and simple things should be simple" - this is what the power of a Language.
Many of us have seen lot of Samples in JavaFX and my favorites are those in which complex things are done quite easy, like PhotoFlip. http://www.javafx.com/samples/PhotoFlip/index.html. You can see how complex calculation goes for a perspective transform.
Using it in a simpler form, I tried to write Cascade transformed Frames, which looks something like this :
You can play with 2 buttons. Sorry for not making some flashy button, I simple used Swing Buttons.
By code is little buggy, so bear with it.
- Moving Mouse on any frame, will make it front. - Close button will close that frame. (It is only possible in non-perspective mode). - Top bar can be useful for dragging the frames(again good at non-perspective mode, in perspective mode, use the left most corner to drag it, you can figure out why is so ? :) ). - Text will be as clear as it was in original mode. - Sharing common reason in case of toFront() make the effect little flckry.
(One problem solved, thanks for José Miguel in comment section - Code changed)
Its all in around 100-150 lines of code. Feel free for suggestions. This can be used for multi-frame work like showing Car models, parts of engine.
Writing flexible code is always good. Though I myself write lot of hard coded stuff but nothing wrong in giving good lecture :D. Weeks back I planned to make flexible template in JavaFX, so that we all can use it by just writing one line of code and that is making an instance of that template in our main file. In general the code we write comes with OS frame like on Windows XP it will come with blue frame and close button, min/max buttons, but for good graphics its between to use own template :) and off course it will work on mobile too.
Here are some examples :
So, I have just created a close button on which we call FX.exit(), nothing else. If we can customize the close button, say hmm to save the data or to save the position of the application. Most of the time it happens, we want the application to open at same position where we dragged it last time.
Important thing to notice is the upper rectangle I mean the title bar adjust the length from its own. So, even the landspace(90 degree rotating the mobile) will give us the correct template form(title bar). I agree, it should be small in case of mobile, and even that can be manageable with code(I have not done in this example).
How to do this can be understandable in 3 basics steps :
1. Define scene as an instance and use that inside stage like this :
public var s = Scene { height: 200 width: 200 fill: Color.GRAY content: [ Text { font: Font { size: 20 } x: 10, y: 100 content: "Application content" } ] };
2. Everything will be effected with scene width and height. So, take this in a var. like :
public var width = bind s.width; public var height = bind s.height;
Now, every component, like rectangle, arc, circle will be properly bind with width and height. Changes width and height will change all the component in relative term. So, rectangle(border line) is :
So, height is hard coded here but it should vary according to the size of window. For small window like mobile screen we need to reduce it with some factor.
3. Since Main is used as many place, the compiler will confuse with the entry point, so write the main stage code in function run(). Like:
function run() { var s1 = Stage { title: "Flexible Themes" style: StageStyle.TRANSPARENT width: 240 height: 320 scene: s } }
Here are the files :
Main File (Please rename this file to Main.fx, else you will get some problem)
There can be lot to do with Themes, like adding min/max button, giving drag option which is default in OS Frames or save option as we already mentioned. Please let me know if I missed something :).
Thanks to Josh for making it possible for all screen.
Some of the way, we can make fancy text. There are lot of good way but these are few of them. If you really want to make it fancy then please do some R&D with these API's used in this code. Here is a small code:
Ah, finally I got JNLP working on my blog, thanks to Sergey and Vikram. I am posting some of my samples with JNLP as we can use as repository for JavaFX samples :). These all are old samples but just with JNLP, so that we can run and see the effect.
1. Spring Motion : We can create n no. of instance of Spring class. Detail is here. This example deals with Motion, Gradient and Physics Equations.
2. 3D Button Effect: This example is about PressButton and 3D shadow effect. Detail is here. Basic deals with Shadow Effect, Gradient, and Animation.
3. Glowing Stars in Sky: This example I have created with JavaFX Production Suite. So, we made a home in Photoshop and imported that in JavaFX and then star animation is written in JavaFX. Detail is here. This sample deals with JavaFX Production Suite, Animation, Timelines and Shapes.
4. Colliding Balls: This we have blogged some 4-5 days back. This is again a physics motion with a transparent window. Detail is here. Sample deals with Motion, Equation, Timelines and Gradient. Initial positions and colors are random, so can be wired at sometime + style: StageStyle.TRANSPARENT has been used, so we will not see any frame and so close button will be missing, please press Ctrl + F4 to close the application :). I guess, the good practice is to write esc. key event and call FX.exit().
5. Image Depth support in JavaFX: Image depth setting or in some language we call it Z-Ordering is supported in JavaFX too. Last to last blog is about that, so here is detail. This sample basically deals with toFront and toBack API of Node and Animation(nothing cool in terms of Animation :D).
6. Pendulum Motion with Gravity Controller: This is just the last blog. Detail is here. This sample deals with Motion, Gradient and Complex Equations, Binding Feature. I have changed the code little from the prev. blog. Now, it is transparent, so it will give us a better look :)
Feel free to share your experience. I hope all JNLP should work, if not please let me know. Some more I will add soon, actually all these are older samples, just tried to make repoistory, so that easy to find :).
Hi, I am Vaibhav. I am working in Java and JavaFX group. This blog is about writing simple code and application in Java and newly launched JavaFX. I am working in Bangalore, India.
It was a very nice idea! Just wanna say thank...