miércoles 31 de diciembre de 2008

JavaFX, Mario and me

I guess I am a decent programmer. At least, I've been able to contribute some code to DWR and/or IWebMvc. I could even say that I don't hate Javascript if that holds any value. But, anyway, I've always been bugged by a musing though...

The sad thing is I'm a proficient programmer of "enterprise" (read web) applications. I've always been afraid of not been a REAL coder. Say a Linux hacker or even better...a videogame programmer! You know the kind of people that deliver things like Call of Duty!

I've always blamed my lack of imagination (I'm no game designer) or creative skills (probably a 6 year old kid draws better than me). But with time I've come to conclude that it's just fear. If the graphics are the problem there are multitude of game sprites around the net. And you don't need to invent something new really, a classical shooter or platform game fits the bill perfectly to start.

So I decided that enough is enough and I had to try and make a video game (and become a Tier-1 developer). Well, as a matter of fact, I don't pretend to build a complete one (or some fancy 3d graphics...for now). Just a character that moves on screen and jumps to get coins will be enough to begin. Something like one of my all-time-favorites, Super Mario World. And I can even get the original sprites. Good!


Unfortunately, I'm proficient with Java, Groovy or Javascript but I'm not sure they're your best bet to code a Super Mario clone. I thought about Visual Studio Express C++ for a moment but, really, that was not feasible. C++ after Java seems like a trip to the past. What about something new and shiny? Something related to the Java world if possible? What about JavaFX? Said and done I was downloading the FX plugin for Netbeans and looking through some samples and tutorials.

My first impression was...why the hell do they call this Java??? Yes, be warned, the syntax and the structure is all new. But it packs some interesting features! The bind keyword was a nice surprise (it's like joining a pointer with its referenced value). How useful! The IDE support can improve though. It's still in it's infancy. I'm eagerly waiting for new versions. But watch for the preview mode, it detects changes on-the-fly without even saving the file and shows them on screen.

Soon I realized I needed to use the ImageView (to show images) and the Timeline object to process animations. Using the bind keyword profusely let's you modify a variable and cascade changes in multiple places. By the way, you don't have to worry about concepts like Double Buffering, the platform handles them for you. That's pretty good for newbies. So how does a view look like in the end: a Stage object with a Scene inside that packs a group of components

Stage {
   title: "Mario sprites"
   width: 250
   height: 120
   scene: Scene {
      content: [
   ]
}

Add some vars to control state:

var x = 115.0;
var state = 0;
var currentAnimation : Timeline;
def positions : Image[] = [
   Image {
      url: "{__DIR__}../resources/front.gif" },
   Image {
      url: "{__DIR__}../resources/spin.gif" },
   Image {
      url: "{__DIR__}../resources/left.gif" },
   Image {
      url: "{__DIR__}../resources/right.gif" }
];
var activePosition = bind positions[state];

And a component:

ImageView {
   translateX: bind x
   translateY: 10
   image: bind activePosition
}

The animation will be fired by a radio button as I yet haven't figured keyboard events. Notice that a change in state automatically changes the image and a change in the X axis magically repaints the image component:

SwingRadioButton {
   ...
   onMouseClicked: function (event: MouseEvent) {
      if (currentAnimation != null) currentAnimation.stop();
      state = 2;
      currentAnimation = Timeline {
      keyFrames: [
         KeyFrame {
            time: (x * 1s) / 50
            values: [
               x => 0.0 tween Interpolator.LINEAR,
               state => 0 tween Interpolator.DISCRETE,
               currentAnimation => null tween Interpolator.DISCRETE
            ]
         }
      ]
   }
   currentAnimation.play();
},

And here's the result, 100 lines of code and three hours of work later:

video

All in all, much better than I expected to achieve given that this has been my first try managing any kind of graphics or animations and I had zero experience with JavaFX. The technology is really great though it needs maturing. I'm looking forward to seeing JavaFX views in Spring applications for example. And I have yet to read some tutorials about server side integration. But it has been a refreshing start. Follow my advice and give it a try!

miércoles 24 de diciembre de 2008

Twitter

Finally I've succumbed and I've created a Twitter account (not precisesly an early adopter I guess!). Not that I have much to say (heck, I've never been a fan of social networks) but some people may be interested in knowing the most up to date progress of DWR or IWebMvc. If so, just look for xmaniac there.

And Happy Christmas all!

domingo 7 de diciembre de 2008

IWebMvc2 Beta 2 available

A new beta version (and the corresponding screencast) is available at the repository. This new release features a much cleaner codebase, some new widgets (AJAX login, color, inner CRUD, lighbox nano...), security enabled and lookup/edition mode. It should even work in IE7+!

Enjoy it!

jueves 4 de diciembre de 2008

JAWR

I've been wanting to talk about JAWR for sometime now. For those in the dark JAWR is an OSS project (lead by a fellow Spanish!) that focus on improving the performance of web applications just by serving JS and CSS files. Not impressed? Let's review some of the features the developer gets with minimal to no effort:
  • Debug and production modes
    Many projects end up using an Apache frontend to serve static files. Doing this correctly is far from trivial. A big chunk of the rest just end up serving files without any special tweaking. That imposes an extra load on the server, the network and the client and clearly does not scale very well. JAWR let's the developer completely forget about deployment issues by just modifying a property.
  • Compress, minify & cache for free
    Among the most interesting features JAWR offers is the automatic post-processing of archives. Without a line of code all the files are automatically shrunk improving performance orders of magnitude
  • Bundling
    People have always avoided splitting the JS code in too many files due to the extra HTTP requests required. JAWR automatically concats files in a bundle allowing clean sources with no penalties.
  • Framework integration
    Last version of JAWR added support for Spring MVC. Before was DWR and even before Facelets or Grails. Configuring DWR2 for best performance is difficult (and I know what I'm talking about), JAWR simplifies it so much that it seems cheap. The best news is DWR 3.1 (notice the dot) will probably pack some form or another of JAWR inside. This alone is a proof of the power of the tool
  • I18n in Javascript
    Including i18n messages in JS has always been a pain and no standard API existed so every team had to devise a custom solution. Here's a nice one perfectly integrated with the Java platform (and I expect improvements in this area)
I admit that in my case it was love at first sight and I can't help but recommend it as much as I can. IWebMvc uses it extensively and for a reason. I can't stress it enough, for every developer interested in web performance this tool is a must!

PS.- Sprites...next? One can only hope ;-)