Showing posts with label javafx. Show all posts
Showing posts with label javafx. Show all posts

Tuesday, July 21, 2009

JavaFX - Operations on Strings

Use any quote you like

Ok, so let's start with Strings. You can use both single and double quote to define simple String. Both works:
var a = 'Single';
var b = "Double";
Also you can use another quote in String as part of this String. I.e. if you use single quote to declare String, you can put double quote inside, and if you use double one, you can put single one inside.
var a = 'This is String "with double quote" inside';
var b = "This is String 'with single quote' inside";
Both are valid Strings. However, if you need to use both types in your String, you have to escape it:
var a = 'Here we have "double quotes" and \'single quotes\' in one String';
var b = "And 'here' \"too!\""
Expressions in String

In JavaFX you can also embed expressions in Strings. To do it, put expression in curly brackets:
println("2 + 3 = {2+3}");
This code prints: 2 + 3 = 5 By the way, you can see new println function here. It is in javafx.lang.Builtins, which is included automatically in all JavaFX scripts.

As expression, you can use also variables, function calls, operations on variables (like in example above) etc.

Concatenation

You don't need any operator to concatenate Strings. If you split String over few lines, you don't have to add "+" at the end of each line.
var one = "This" 'is'
"one"
'String';
println(one);
And it outputs: ThisisoneString

Formatting

You can quite easy format Strings in JavaFX. It is based on java.util.Formatter class.

println("{%x 12}");
println("{%c 120}");
var d = Date{};    //Here new java.util.Date is created
println("{%tH d}:{%tM d}:{%tS d}")
And it prints:
c x 23:12:18 First there is 12 in hex, then there is character, which code in Unicode is 120, and then it is Date object, first we get hour from it, then minute, and then second.

Localization

Localization in JFX is realized by properties files. It's name is connected with your script (file with your JFX code) by name convention. If your script is House.fx, then your localization file is House_en.fxproperties and it has to be somewhere in the classpath. fxproperties file looks like this:
"dog"="pies"
"cat"="kot"
And then, to use it in script:
println(##"dog");
println(##[cat]"Cat");
If there is fxproperties found, it is used. If not found, default is used. Default is either properties key (like "dog" in above example) or another word (like "Cat", not "cat" which is only fxproperties key).

Tuesday, July 14, 2009

JavaFX - Defining simple variables.

Ok, so I thought to write JavaFX tutorial. Hope it's going to be useful for somebody. Welcome to part one, about language basics, defining variables and values.

First, you should know what variables/value types are available. There are all the familiar ones (String, Long, Double etc.), but no primitives (long, double etc.). Well, in JavaFX everything is an object. Really. There is also one new type: Duration. It is especially useful in animations, but we'll get to it later.

So there are variables and values. Values are a little like final constants in Java, but only a little. Value of value ( ;) ) can change, but you can't change it explicitly. It can be changed by binding - we'll talk about it in some later part.

So how do you define variables and values? Quite simple:
var a:Float = 5;
Defines variable a of type Float. We can also do something like this:
var a = 5;
Where is variable's type? Well, you don't need to specify it in such case. Type is inferred automatically by JavaFX Script compiler from value assigned to this variable. Isn't it obvious that it is Integer? It could be also Float, yes, and if you want it to be Float, please specify it like in first example.

What about "almost-like-java-constants" values? You define it like that:
def a:Float = 5;
So now there is value defined, and you can't change it. a = 6 will not compile.

Ok, let's look at this new Duration type. Few examples:
var s = 5s;
var m = 2m;
var h = 4h;
s is 5 seconds, m is 2 minutes and h is 4 hours. Quite simple, isn't it? Also arithmetics like adding, multiplying etc works here:
var s = 5s * 4;
var m = 2m + 3m;
var h = (2h + 3m) * 4;
Ok, that's all in this part. In the next one I'll write something about String operations and sequences. Stay tuned!

Saturday, May 23, 2009

Be careful binding to functions in JavaFX

In JavaFX (I'll call it JFX from now) there is possibility to bind variables. You can bind variable to another variable:
var src = 4;
def dst = bind src;
println(dst);
src = 42;
println(dst);
it'll print:
4
42
Maybe you noticed I used var for one variable and def for another. It could be as well var in both cases. Bind makes dst is updated every time it is read. Here we bound variable to another variable, but we can bind it to any expression:
var factor = 3;
var multiplier = 3;
def dst = bind getValue(multiplier);
println(dst);
multiplier = 5;
println(dst);

function getValue(p:Integer) {
 p * factor;
}
What it gives us?
9
15
Still everything is quite clear, isn't it? First value of dsc is 3*3, which is 9, than multiplier is changed and we get 5 * 3, which is 15. Let's now change factor, not multiplier:
var factor = 3;
var multiplier = 3;
def dst = bind getValue(multiplier);
println(dst);
factor = 5;
println(dst);

function getValue(p:Integer) {
 p * factor;
}
And we get:
9
9
Now this is wrong! First we want 3*3 which is 9, so it is ok, but then we change factor and expect 3*5, which is 15, but we still get 9. What's up? Well, dsc is bound to function. JFX remembers our function was called last time with parameter 3, and returned 9. Second time (second read of dst) it is also called with parameter 3, so JFX doesn't call it again, but just gives us cached value. But there is something we can do about it, and it is the bound keyword:
var factor = 3;
var multiplier = 3;
def dst = bind getValue(multiplier);
println(dst);
factor = 5;
println(dst);

bound function getValue(p:Integer) {
 p * factor;
}
And now it prints:
9
15
Just as expected. bound keyword makes JFX analyse the function and checks which variables can affect return value. In this case it can recoginze return value is dependent not only on input parameter, but also on factor variable, so when variable changes, JFX calls the function again even if the parameter doesn't change. Now consider a little different code:
var factor = 3;
var multiplier = 3;
def dst = bind getValue(multiplier);
println(dst);
Thread.currentThread().sleep(5);
multiplier = 5;
println(dst);
Thread.currentThread().sleep(5);
println(dst);

function getValue(p:Integer) {
 System.currentTimeMillis();
}
Note that function is not bound again. sleep(5) is there to make sure two calls won't happen in the same millisecond. So it can give us:
1243112189488
1243112189496
1243112189496
(of course you'll get different values if running it at different time) First is number of milliseconds, second is again proper number of milliseconds, because function parameter value has changed and JFX called function again, and then second value is repeated, because parameter value didn't change and function return value is taken from cache. But what happens when we'll use bound? Let's see:
var factor = 3;
var multiplier = 3;
def dst = bind getValue(multiplier);
println(dst);
Thread.currentThread().sleep(5);
multiplier = 5;
println(dst);
Thread.currentThread().sleep(5);
println(dst);

bound function getValue(p:Integer) {
 System.currentTimeMillis();
}
And we get:
1243112415228
1243112415228
1243112415228
What? All three values are the same? So adding bound even made things worse! How is it possible? Well, bound makes JFX not only can recognize which variables affect return values, but also which function parameters doesn't affect it. So now JFX is aware that p doesn't affect return value of the function and it calls the function only on the first time. Then it takes value from cache, regardless parameter has changed or not. Conclusion: Be careful when using functions for binding variables. Be extremely careful when using functions dealing with current time (or better just don't do it ;)).

Friday, May 22, 2009

JavaFX for linux

There is no official release of JavaFX for linux. Yet you can use it thanks to Silveira Neto. Here is his blog entry with plugins for Netbeans.

All you need to do is download the plugins, extract and install in Netbeans 6.5.x (doesn't work on 6.7 beta). You also need java 1.6 update 13.