Saturday, May 3, 2008

JavaFX curly brace overdose

Have you seen code examples from JavaFX script? The language is very interesting. It contains many new features that may revolutionize the way people create user interfaces in Swing, such as triggers and binding. But one thing that always bothers me about the code examples that I have seen is the insane amount of curly braces that they have to use. Check out this example taken from James Weaver's Java FX blog:


/*
*  TetrisMain.fx - The main program for a compiled JavaFX Script Tetris game
*
*  Developed 2008 by James L. Weaver (jim.weaver at lat-inc.com)
*  to serve as a compiled JavaFX Script example.
*/
package tetris_ui;

import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.System;
import tetris_model.*;

Frame {
  var model = TetrisModel {}
  var canvas:Canvas
  width: 350
  height: 500
  title: "TetrisJFX"
  background: Color.LIGHTGREY
  content:
    StackPanel {
      content: [
        Canvas {
          content: [
            ImageView {
              image:
                Image {
                  url: "{__DIR__}images/background.jpg"
                }
            },
            Text {
              transform: bind [
                Translate.translate(30, 350),
                Rotate.rotate(290, 0, 0),
              ]
              content: "Game Over"
              fill: Color.WHITE
              opacity: bind if (model.timeline.running) 0
                            else 1
              font:
                Font {
                  face: FontFace.SANSSERIF
                  size: 60
                  style: FontStyle.BOLD
                }
            }
          ]
        },
        BorderPanel {
          center:
            FlowPanel {
              alignment: Alignment.LEADING
              vgap: 10
              hgap: 10
              content: [
                Canvas {
                  content:
                    TetrisPlayingField {
                      model: model
                    }
                }
              ]
            }
          right:
            GridPanel {
            var sansSerif24 =
              Font {
                face: FontFace.SANSSERIF
                size: 24
                style: FontStyle.BOLD
              }
              rows: 2
              columns: 1
              cells: [
                BorderPanel {
                  top:
                    FlowPanel {
                      vgap: 10
                      alignment: Alignment.LEADING
                      content:
                        Label {
                          foreground: Color.LIGHTBLUE
                          text: "NEXT:"
                          font: sansSerif24
                        }
                    }
                  center: 
                    Canvas {
                      content:
                        TetrisNextShapeNode {
                          model: model
                        }
                    }
                },
                BorderPanel {
                  top:
                    Label {
                      foreground: Color.LIGHTBLUE
                      text: "SCORE:"
                      font: sansSerif24
                    }
                  center:
                    FlowPanel {
                      alignment: Alignment.TRAILING
                      content:
                        Label {
                          foreground: Color.LIGHTBLUE
                          text: bind "{model.score}"
                          font: sansSerif24
                        }
                    }
                }
              ]
            }
          bottom:
            FlowPanel {
              alignment: Alignment.CENTER
              content: [
                Button {
                  text: bind
                    if (model.timeline.running)
                      "Stop"
                    else
                      "Play"
                  action:
                    function():Void {
                      if (model.timeline.running) {
                        model.stopGame();
                      }
                      else {
                        model.startGame();
                      }
                    }
                },
                Button {
                  text: "Rotate"
                  enabled: bind not model.stopDropping
                  action:
                    function():Void {
                      model.rotate90();
                    }
                },
                Button {
                  text: "Left"
                  enabled: bind not model.stopDropping
                  action:
                    function():Void {
                      model.moveLeft();
                    }
                },
                Button {
                  text: "Right"
                  enabled: bind not model.stopDropping
                  action:
                    function():Void {
                      model.moveRight();
                    }
                }
              ]
            }
        }
      ]
    }
  visible: true
  onClose:
    function():Void {
      System.exit(0);
    }
}


Wow... that code looks like a tower. So here goes a crazy idea: what if JavaFX Script used the same strategy as python: use the indentation to avoid the need of curly braces. Also, perhaps a different indentation scheme can help.

What do you think about the looks of JavaFX?

Thursday, May 1, 2008

Java 6: compile once, run nowhere?

Java's strength is supposed to be "compile once, run everywhere". This  claim is true in most of Java 1.4 and 5.0 applications. But what about Java 6? Is Java loosing it's most important property? 

Java used to be the prime choice for creating applications that can run in any platform. But if you want to take advantage of the latest additions in the Java 6.0 release of the platform, you will probably face a big disappointment at distribution time.  The main problem is that most users don't have the correct version of Java in their system. In a word, it is a big pain for most users to update the Java Runtime Environment. In the case of Windows, whenever you update Java, it will add a new version to the machine, so after a while, a user will find 4 or 5 different versions of the JRE in their systems. Each download is big and slow. I am not familiar with the case of linux, but for the distributions I have tried, Java 5 is installed by default. Finally, just this week Apple announced the availability of Java 6.0 for MacOS X Leopard with support for just 64 bit Intel processors. 

Are we stuck with Java 5.0 to be sure we can deliver an application that any user can use? I would love to hear your opinion.

Like this post? Digg it!