Mind Your Language, Silly!

Easter holidays are over, back to my old crazy Java 3D and 2D experiments and the fun idea I had for the JavaFX coding challenge! :-D ... Which... I of course cannot submit, since I'm an employee... :-| Oh well. All I can say is that the idea hinges on a highly advanced bleeding-edge stickfigure generator. I'll post it here if my app ever amounts to anything without the pressure of a deadline. :-P

What I like about JavaFX is how fast I learned the syntax and how much I get from a few lines of code. I looked at the sample projects included in the IDE and browsed the language overview (all tutorials) and basically whipped up my first app. I jotted down some revelations I had on the way, hope they are useful to you:

Java SyntaxJavaFX Syntax
The Java constructor needs explicit arguments, in order...
public class Pizza {
  public Pizza( int radius ){ ... }
}
... while the JavaFX constructor implicitly accepts all public variables, in any order.
public class Pizza {
  public var radius;
  public function create(){ ... } 
}
You made this variable public because you want it to be set, no need to repeat yourself -- nice.
Creating a new Java object...
Pizza p = new Pizza(16);
... versus creating a new JavaFX object.
var p = Pizza { radius:16 };
The order of arguments doesn't matter thanks to the labels. The argument type (here, integer) is inferred from the context (here, 16).
String concatenation operator in Java...
println("The pizza is "+radius\*2+"cm across.");
... versus evaluation braces in JavaFX strings:
println("The pizza is {radius\*2}cm across.");
You can import and use the classical packages such as java.io.\*...
BufferedReader in = new BufferedReader(
  new FileReader("bla.txt")
);
... but note the syntax change:
var in:BufferedReader = new BufferedReader(
  new FileReader("bla.txt")
);
Creating an JFrame application window in Java...
...
JFrame frame = new JFrame("Hello World");
JLabel text = new JLabel("Hello?");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(200,200));
frame.getContentPane().add(text, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
...
... versus a JavaFX application window:
Stage {
  title: "Hello World"
  width: 200 height: 200
  scene: Scene {
    content: [ Text { content:"Hello?" } ]
  }
}
That's it!

As you see above you still use standard Java packages for certain things (such as reading and writing files) that JavaFX has no need to reinvent since it focuses on GUI code. The NetBeans GUI Builder however does not support JavaFX yet, which is a pity. In the meantime you align GUI elements using javafx.scene.layout.VBox and and HBox and preview the results in the IDE.

When your app grows you will want to move individual javafx.scene.Nodes into classes of their own. Here's a very common pattern how to create a custom Node: The details are now defined inside the custom class and don't clutter the Stage. The return value can also be a javafx.scene.Group of Nodes.

File: Main.fx

import javafx.scene.\*;
import javafx.stage.Stage;

Stage {
    title: "Pizza Window"
    width: 200 height: 200
    scene: Scene {
      content: [
        Pizza { radius:16 } // create a new object
      ]
    }
}

File: Pizza.fx

import javafx.scene.\*;
import javafx.scene.effect.\*;
import javafx.scene.shape.\*;

public class Pizza extends CustomNode {
  public var radius:Integer;
  override public function create():Node{
    return
      Circle {
        centerX:100 centerY:100 radius:radius 
        fill:javafx.scene.paint.Color.YELLOW
        effect: Lighting{}
      }
  }
}

You can quickly compile these two files on the command line using javafxc Main.fx and then run them using javafx Main -- Or create a NetBeans project.

Another cool thing in JavaFX is binding: You bind an input element (a slider or field) to a variable, and bind the variable to an argument. For instance, the slider is bound to the variable that is bound to the image size. When the user moves the slider, the image is scaled immediately, no explicit event handling necessary! Also have a look at sequences and for-loops, these expressions are fun to use. Whoever invented these features was obviously annoyed about the same things as I. ;-)

PS:

During my first tries to compile stuff I kept getting this annoying error and didn't know what it meant:

Pizza.fx:40: cannot find symbol
symbol  : constructor Pizza(int,boolean)
location: class Pizza
             def p = new Pizza( 16 );
Note: The following error is an internal error in the OpenJFX compiler (1.1.1).

Wait a second -- My constructor doesn't even use any Boolean arguments, where does this come from? Seems that out of a habit, I created the JavaFX object using Java's "new" syntax... After I changed the offending line to def p = Pizza { radius:16 }; the compiler is happy. It would be nice if the JavaFX compiler could identify this case and just say "You're talking Java again, silly!" -_-

Comments:

Post a Comment:
Comments are closed for this entry.
About

NetBeans IDE, Java SE and ME, 3D Games, Linux, Mac, Cocoa, Prague, Linguistics.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
News

No bookmarks in folder

Blogroll