JavaFX and Java Interoperability

Posted: under JavaFX Technology.
Tags: , , , ,

[ 2009/06/22 Update: My latest article discussed how to use
Pure Java Code to Call JavaFX Class]

From the JavaFX blog, an article discussed the possiblity of invoking methods of JavaFX classes from Java. JavaFX can call Java code without any problem, however, the reverse is not supported by JavaFX. Doing some googling shows that programmers are trying all kinds of hacks to invoke a JavaFX class method from Java. You can check out an interesting article on reverse engineering of JavaFX classes. Even the example on JavaFX blog provided a hack to work around this.

So do we have the need of such kind of interaction between Java and JavaFX? I say it is a “YES”. If Java and JavaFX can be used interchangeably(when possible), this could give more life to JavaFX in the long run. Just consider the MVC design pattern, we can write an application by using Java and JavaFX together. The “M” and “C” part can be implemented in Java while the “V” can be done by JavaFX. It would be very interesting to see this.

Right now, there are a few “standard” ways to call JavaFX from Java:

1) Using the ScriptEngineManager class. From Geertjan Wielenga’s article, we can do it in this way:

package calc; 

import java.io.InputStreamReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException; 

public class CalculatorLauncher { 

public static void main(String[] args) {
 try {
 ScriptEngineManager manager=new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByExtension("fx");
 InputStreamReader reader = new InputStreamReader
 (CalculatorLauncher.class.getResourceAsStream
 ("Calculator.fx"));
 engine.eval(reader);
   } catch (ScriptException ex) {
  }
 }
}

However, this is just like System.exec(”calc”) as pointed out by cronseaux. I agree with him. A even simpler way is to use System.exec(”javafx Calculator.fx”) to complete the above code. So this is not a good solution.

2) Manage to use java reflection to call JavaFX class methods. This one should work because JavaFX classes are compiled into Java classes and byte code. However, the complexity makes it almost unusable and code written in this way has no readability.

3) A third approach is to define an interface in Java and implement it in JavaFX. For example,

public interface JavaInterface
{ ... }

In MyJavaFXClass.fx, do something like:

public class MyJavaFXClass extends JavaInterface
{ ... }

In you java code, just invoke the JavaFX object directly using the interface. This approach can solve most of the interoperation issues. Just that an interface is needed for every JavaFX class which is going to be called from Java. Though it is very cumbersome, at least it is the best workaround I can find so far.

Since this is the first release of JavaFX, I don’t criticize much on this given the strong powerful features of JavaFX. I do hope the future releases of JavaFX can improve on this.

[ 2009/06/22 Update: Please refer to my latest article for discussion on: Pure Java Code to Call JavaFX Class ]

Comments (2) Dec 26 2008

My First JavaFX Demo Game: PAC-MAN

Posted: under JavaFX Games.
Tags: , ,

I spent some time to implement the classic game “PAC MAN”. It demos many features of the JavaFX language. Right now, it is a “simplified” PACMAN Game. I am working on the code and hopefully to complete the PACMAN game soon.  Source code is not released yet because I plan to do so when I finish the whole game. A blog of writing this game will be available soon. Stay tuned.

Usage:
Arrow keys to move and control to pac man to eat all beans inside the maze. The big beans are magic beans which allow the pac man to eat ghosts.

JavaFX Features Demostrated:

  • Bindings
  • Animations
  • Effects
  • Transforms
  • Multiple inheritant
  • Java classes integration
  • Declarative statements
  • Sequences, how to map 2D arrays into a 1D Sequence
  • Handling keyboard events

JRE 1.5+ required, JRE1.6 U10 is better because it allows draggable applet to run outside the browser. It will take some time for first time launching JavaFX application …

screenshot


My JavaFX code is compatible with the newly released JavaFX 1.0. JavaFX is for Windows and Mac for now.

Comments (3) Dec 07 2008

JavaFX SDK 1.0 Launched

Posted: under JavaFX News.
Tags: , ,

Today, the JavaFX 1.0 SDK is officially released. It is really exciting! The JavaFX platform opens a huge market for developers and UI designers to work together for a rich interface application(RIA). Because Java technology is already on more than 10M devices(PDA, computers, cellphones, etc), this new scripting language is expected to be widely accepted by most Java developers.

One of the most innovative features, I think, should be the eye-opening Drag-to-install trick. This is support by the JDK 1.6U10. Applets can be dragged directly onto a desktop and run in a seperate window. This bring us the simplicity of installation of internet application.

Other useful and powerful features attractive to me include Netbeans IDE, video support, production suite. I downloaded and try the IDE, it is very simple to build your first “hello world” JavaFX program.

Refer to A JavaFX Demo Game

Comments (0) Dec 04 2008

JavaFX Preview SDK

Posted: under JavaFX News.
Tags: ,

Yesterday, the JavaFX Preview SDK is available for download. This is very exciting news. Since its debute on JavaOne 2007, many enthusiasts cheered of this preview release. The formal release is schedule to be out on Dec 2, 2008. We can’t wait to get it.

Though JavaFX is a technology based on Java, it totally changes the world of “Your Dad’s Java”. Many GUI related things can be done easily on JavaFX, such as graphic, video, audio, and anything for RIA. People are excited because JavaFX provides a very simple way to implement these elements in an RIA’s GUI.

JavaFX also links the world between art designer and computer programmer. Traditionally, these two kinds of folks find them hard to work with each other. With the birth of JavaFX, we finally have an approach to combine two things easily.

With a few lines of code, JavaFX can render sophiscated GUIs that used to be done by Java with 10 times of source code. JavaFX has many build-in features of doing animation, graphical effects that are seen photoshop.

I will download and test drive the preview SDK soon.

Comments (0) Aug 01 2008