Sunday, December 07, 2008

Migrating from preview SDK to 1.0 of JavaFX

I'm developing a course that includes the JavaFX technology. That part was written a few months ago, based on the JavaFX preview SDK. As the JavaFX 1.0 has been released, I convert the programs from the JavaFX preview to 1.0. The following is a summary of the changes in JavaFX that I notice during the conversion.
  • To define constant values, use the def keyword (instead of the readonly keyword in the preview). E.g.,
    def PI = 3.14;
  • To write an application (with Swing frames), use the javafx.stage.Stage and the javafx.scene.Scene classes as follows. There is no javafx.ext.swing.SwingFrame class or javafx.application package.
    Stage {
      scene: Scene {
        content: ...
      }
    }
    Programs written in this way can also be executed as applets without modification.
  • Swing components in the javafx.ext.swing package are prefixed by Swing. For example, the Swing label is javafx.ext.swing.SwingLabel (instead of javafx.ext.swing.Label in the preview).
  • To layout GUI components, use the classes in the javafx.scene.layout package, such as the VBox and HBox classes. There is no panel class in the javafx.ext.swing package (such as FlowPanel in the preview).
  • The preferred size of a Swing component in javafx.ext.swing package cannot be modified when the it is created. (It could be in the preview.)
  • In class definition, attributes (instance variables) are declared using the var keyword (instead of attribute in the preview). E.g.,
    class Person {
      var name: String;
    }
  • To declare a function that overrides a superclass's function, use the override keyword (no need in the preview):
    class Person {
      override function toString() : String { "..." }
    }
  • There is no menu support. (The Menu and MenuItem classes in the javafx.ext.swing package exist in the preview but not in 1.0.)
  • The shape classes, such as Rectangle and Circle, are in the javafx.scene.shape package (instead of the javafx.scene.geometry package).
  • To start, or play, a Timeline, call the Timeline's play() method (instead of the start() method in the preview.)
  • Shapes, in the javafx.scene.shape package, are not transparent by default. Set a shape's color attribute to Color.TRANSPARENT to make it transparent.
  • The + operator cannot be applied to strings, e.g., 10 + " cents" is not valid. Use string interpolation instead. E.g.,
    int n = 10;
    var s = "{n} cents";
  • There are built-in functions print() and println(). There is no need to use java.lang.System.out.println() and java.lang.System.out.print() now.
  • There is a built-in function FX.exit(). There is no need to use java.lang.System.exit() now.

Monday, February 04, 2008

Classes and objects

In every first lesson of object-oriented programming, the concepts of classes and objects are introduced. Typically,

  • an object represents an entity in real life that has states and behaviours, and
  • a class is a blueprint/template/design of a certain type of similar objects.
While these explanations are good and precise, some, if not many, new learners still cannot get the idea and their difference after reading them and the related examples. As majority of learners have procedural programming background, here I try to illustrate the concepts of classes and objects with comparison to functions in procedural programming.

When writing a function, we are defining what will happen when the function is used (invoked). For example, let's define a function sum:

int sum(int a, int b) { int r; r = a + b; return r; }

This definition does nothing more than allowing the system to look up the method's execution when it is used elsewhere. When this function is invoked

int x = sum(100, 200);
the system uses the definition of sum and executes the code in the definition.

In a similar way, a class is the definition of an object. It defines what will happen when the object is used. While using a function is mostly obvious - invoking it, using an object can be more complex - creating it, accessing its data fields, invoking its methods. Here I write a class Circle (as definition of Circle objects):

class Circle { int radius; // data field Circle(int r) { // constructor radius = r; } double getArea() { // method return radius * radius * 3.1416; } }

When Circle objects are used in some other code, the system will look into the class and execute the proper code in the class. For example, with

Circle c = new Circle(10);
the constructor of class Circle will execute and a Circle object is created. With
c.radius = 9;
the data field radius of the newly created Circle will be changed. With
c.getRadius()
the code inside the method getRadius of the Circle will be executed.