Saturday, June 6, 2009

JavaFX FTW?

JavaFX is yet-another-scripting-language. It promises that you can quickly create Java applets and applications with fancy visual effects by writing simple little scripts instead of miserably complex Java code. Does it deliver?

I was sic'ed onto JavaFX at my day job, charged with whipping up some cool new UI mockups. I quickly became fascinated. You can accomplish a lot with a little, using JavaFX. But that's not enough; I need to find out whether it can create industrial-strength applications, and if so, whether the code is readable and maintainable, and whether the UI it produces runs quickly and is responsive to the user.

I won't go into my day-job's proprietary stuff here. But (just in order to learn, you understand) one of the first things I did was start building a virtual gameboard. It went so well and so quickly that I've kept working on the project at home on my own time.

What is a virtual gameboard? I wanted a tool to quickly prototype a new game idea on the computer, without having to spend time and money assembling physical pieces. I wanted to be able to play the game (by myself, as I always do with new game ideas), change the rules and pieces, then play again, with rapid turn-around time on the changes. I also wanted play to go at least as quick as it would with real materials. I called the tool "Placer" because it will let me place things on a virtual gameboard. (A lame name, but it doesn't need a better one at the moment.)

Placer lets you specify an image file for the gameboard, which it displays in a window. A tool-palette area is automatically provided to the left of the board. In the palette are templates: If you see a red cube in the palette, for example, you can click-and-drag on it. It will produce a copy of itself (another red cube) that gets dragged away under your mouse. You drop the copy (called a "token") anywhere you like on the board. The template stays put, in the palette, and can be used again and again to make more tokens. A template is therefore a sort of virtual, limitless pile of its particular kind of token.

Placer is configured from a text file, in which you can specify a wide variety of templates for the palette: squares and rectangles, circles and ovals, cubes, disks, and more. Each one can be given a color, so you can have (say) four templates for the cube supply of four different players: one template each in red, yellow, green, and blue.

Tokens on the board can be moved around as much as you like. They can also be rotated, and each has a little popup menu that lets you delete the token or get a little information about it.

There are a couple of fancier kinds of tokens. One is the Bank, which displays an amount of money. Click on it to make a withdrawal: a small text box appears, you type in the amount to withdraw, and the Bank creates a new Cash token under your mouse. The Cash token displays the amount of money that you withdrew, and you can drag the Cash off to wherever you want it to go. The Bank stays behind, now displaying the balance that remains after your withdrawal. If a game uses money, you can create a master bank for the game, and then give each player a bank of their own. When you drop a Cash token onto a Bank, the Cash disappears and its value is added to the Bank's balance, making it easy to transfer money among the players. (The image shows a $13 bill ready to be dropped onto the Red player's bank, which currently contains $42.)

So has it been all kittens and rainbows? Nope, it hasn't. As is always the case with Java or anything Java-related, layout (that is, getting everything to be in the right place) is a major pain. JavaFX features coordinate-system manipulations that you must use to get anything significant done; no doubt they are very powerful and carefully thought out, but it gets very difficult to keep it all straight in your head (and your code) when you're writing something complex. Event handling is a bit tricky, and seems not always to work as advertised: sometimes my tokens simply ignore clicks and drags, with no clue as to why. (I suspect the garbage collector, at the moment.) And then sometimes JavaFX just throws a huge exception, stopping my app cold. It seems to be because I've coded something wrong, but I don't (yet) understand why I can play half a game of Hammer and Spike before it suddenly decides it doesn't like me.

Another issue is deployment. Suppose I finish up Placer, and want you to use it: how do I give it to you, and how do you install it? JavaFX isn't really designed for building stand-alone apps. You're supposed to access JavaFX programs via the Internet: as browser applets, or mobile device apps. It does provide a way to use WebStart files to deliver a desktop app, but these may have Java sandbox problems, which would be a killer for Placer: it has to be able to read your config file and board art.

But we'll see. I'm not done playing with it yet!


1 comment:

Nunya Bidness said...

Go Rick! Go Rick!

-Nando