Monday, July 6, 2009

Review: JavaFX in Action, by Simon Morris

JavaFX in Action JavaFX (JFX) is a new technology. There are not many books about it yet. Then the more happy I am I can read one of them - "JavaFX in Action" by Simon Morris from Manning Publications.

At the beginning of the book there is explanation of what JavaFX platform is, and it's language JavaFX Script. And why it is great at creating GUIs. The next step is not language basics as one could expect, but first small application in JavaFX. You don't fully understand how it works and what all this words, numbers and braces mean, but you can run it and say "WOW!" :) It is there to make an impression, and it serves this purpose well. It's like an appetizer, making you even more hungry for knowledge and more eager to learn this new great language. I like it, because it makes further reading "taste" better. It also shows what is told in first chapter is true - in JavaFX you can create fancy graphical effects easily.

Chapters two and three are about language itself, variables declaration, data types (new one: duration), language structure - all the fun begins. After learning basic stuff about language, we start creating "real" applications. Nice looking, fancy and colorful small applications (JavaFX is for GUI, isn't it?). What I didn't like here is a bit waterfall, not iterative process. All applications are created class by class, and then at the end run. Many times I wanted to see how single component looks like before finishing whole application. I could do it by writing small app by myself, but I'd prefer to see it in book.

The language used in the book is very light, even funny. It is pleasure to read, still everything is explained clearly and understandably. One of difficult topics are transformations. Such constructs are not common and I didn't see them in any other language (at least not done this way). For me it was quite difficult topic, yet everything was well explained. I understood transformations after playing a little with the code. Another thing was new data type "duration" - no problems with understanding here too (but this one was much easier).

I like it when in book you can see effect of running application. So you don't need to code or even copy code from sources attached to the book and run it. In this book there always was output included after source code, or there were pictures if output was not on console. You still can copy code to your favorite IDE and see it running, but you don't have to.

The book I read was based on JFX 1.1, while there is 1.2 available now. However it was MEAP version, and I'm sure final version is going to be based on 1.2 or even newer JFX (depending when it is going to be released). Anyway author has a lot of knowledge and he predicted (or just knew about it) many things that were missing in JFX 1.1 but are available in 1.2.

Appendixes are also great. There is one for people who don't have anything installed yet, just opened the book and want to code. I had everything installed before, but if you don't, don't worry, the book will tell you what to do step by step. I often missed such appendix in other books, especially when I was less experienced developer. There is even appendix for people not familiar with java, so you can see what static typing vs dynamic typing is all about, what are packages etc.

Reading this book was a pleasure. I learned a lot, and by the best and most effective way - by having fun :)

Friday, June 12, 2009

JAVArsowia 2009

Next edition of JAVArsovia conference is getting closer :) Schedule looks amazing! Four tracks, and everything for free! What a pity I (probably) can't go there this year :( I would love to see presentations on testing and TDD (quite many presentations related to testing this year), Apache Wicket, Android, JVM virtualisation...

I know some speakers, not all of them, but the ones I know are great, so I assume the others are the same :) Also I wouldn't miss evening meeting, drinking beer, talking with fellow geeks and meeting new ones.

Well, I won't be there, but YOU still can :) I am pretty sure you won't regret it :)

Tuesday, June 9, 2009

Encoding movies (in ubuntu)

Example command to encode movies in ubuntu.
mencoder input-movie.avi -o output-movie.avi -oac mp3lame -mc 0 -ovc x264 -x264encopts bitrate=45000
You need to have mencoder installed. mp3lame is audio codec, x264 is video codec. To see other possible codecs type
mencoder -ovc help -ova help 
-mc 0 is for not messing with audio synchronization. Without it mencoder tries to synchronize audio with video, and it can break the synchronization. So if in source movie audio was synchronized well include this option. After -x264encopts there are x264 codec options. Other codecs have other options.

Mencoder howto site was helpful while looking for all this information.

Sunday, May 31, 2009

JavaFX is statically typed. Scala is statically typed.

Sometimes people are suprised when they read or hear that Scala and JavaFX are statically typed languages. Probably it is because in this languages you can declare variables without explicitly telling the type, like this:
//It looks exactly the same in JavaFX and Scala
var a = "string";
No type is given, yet compiler knows a is String (at compile time). How does it know? Well, we just assigned value of type String to it, didn't we? When declaring some variable and immediately assigning value to it, you don't have to add type to the declaration.

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.

Sunday, May 17, 2009

Key promoter for Eclipse

Did you ever miss key promoter in Eclipse? I did. Fortunately I found one: Mousefeed

Whenever you do some action with your mouse, and there is keyboard shortcut for this action, the shortcut is displayed: