Jan 18

Java SE 6u18 Performance Improvements

Posted: under JavaFX.
Tags: , , , , January 18th, 2010

Unlike the u17, the u18 update is not released for security, instead, it introduces many improvements on performance.

Faster Jar File Creation If you application need to create a lot of jar files, you will see about 20 percent improvement.

Hotspot VM Enhancement This includes some hardware-aware features, garbage collection improvements, class loading optimization and compile time optimization on code generation. The improved G1 garbage collector is still not yet ready for production, unless you have business support contract with Sun.

Application Startup Time Improvements If you are using JavaFX, you will notice about 20 percent improvement when launching an FX application. Web start and applet application can now download jar files concurrently.

Other improvements include runtime UI application performance, zip file reading and deployment updates. For more info, please check out here.

Other links:

JavaFX Plug-in for Eclipse 3.5.x
javaFX used in Winter Olympics 2010
Review on Essential JavaFX
JavaFX How-tos

Australian Citizenship Test
Free British citizenship Test
US Citizenship Application

Java SE U18的性能提高

Comments (0)

Jul 29

JavaFX API for Java?

Posted: under JavaFX.
Tags: , , , July 29th, 2009

Last week, in a poll many java developers said they did not want to use JavaFX in their applications. A reason is probably that the APIs of JavaFX is not yet stable. It is true that JavaFX language itself is still evolving. If we search on the internet and we often get obsolete examples which no longer run in the latest JavaFX version. So “pay attention to the publishing date” is a useful tip when you are looking for documents on JavaFX.

The second reason that developers stay away from JavaFX is the lack of convenient way to integrate existing Java applications with JavaFX. Undoubtedly, the strength of JavaFX is that it is based on Java. It gets compiled into bytecode, runs on a JVM and it can call methods of java classes. While compared with Flex or Silverlight, JavaFX has the advantage of n million of Java developers and a huge amount of Java applications. This is great and it gives a robust start to JavaFX. Unfortunately, there is no official way(except for reflection) for Java to fully utilize JavaFX features directly. This becomes a substantial barrier for people to adopt JavaFX.

JavaFX’s attraction to Java developers is the functionalities that are not available in Java SE, such as animation, media processing, charting. However, the price may be too high for developers to convert their existing Java code into JavaFX only to leverage the new features. There are many Java applications out there which cannot be easily changed, esp. for enterprise applications. If we have an easy way to call JavaFX functions from Java, the adoption of JavaFX in the Java developer community will be a lot faster and wider.

I think an ideal architecture of JavaFX API may look like this:

I am not sure technically how difficult it is to come up with a JavaFX API for Java, but I think it should be given some consideration for future JavaFX releases. Since JavaFX is still in its early stage of development, I hope we can see something like this sooner rather than later.

Related Articles:

How to Display JavaFX Scene in a Swing Window

Using JavaFX Features in Java

Interoperability between JavaFX & Java

How to Show a SWING Menu in JavaFX

Convert Swing to JavaFX

Comments (0)

Jun 20

Pure Java Code to Call JavaFX Class

Posted: under JavaFX, JavaFX Coding.
Tags: , , June 20th, 2009


In my previous post Interoperability between JavaFX and Java, I discussed three possible approaches to invoke JavaFX features from the Java side. These approaches were:

1. The ScriptEngineManager class. It is based on JSR-223, the java scripting API, which allows a java program to call a script(such as JavaFX Script, javascript).
2. The JavaFX reflection API. It can probably call any classes in JavaFX.
3. The JavaFX class implements a Java interface so that a Java program can invoke the JavaFX class via the interface. The interface acts as a bridge between the two sides.

The third one seems the most elegant to call JavaFX from Java. However, there is a drawback: the program should start from the JavaFX side. The reason is that it is simpler to use JavaFX code to instantiate the JavaFX classes which can be passed to Java code. Nevertheless, in some scenario, it would be better to start the program from the java side. For example, if you want to add in some JavaFX features to an existing large java application, it is better to have java code as the entry point. To solve this issue, I am combining the essence of Approach 2 and 3 to create the below example.

Let’s say we want to invoke the latest charting functions of JavaFX 1.2 from the java code. We will first use the JavaFX reflection API to instantiate the JavaFX class. We then use it via its java interface. So we define a Java interface first.

/*
 * JavaInterface.java
 *
 * @author Henry Zhang      http://www.javafxgame.com
 */
package javatest;
public interface JavaInterface {
  public void addData(String name, float data);
  public void showChart();
}

The next step is to create a JavaFX class MyChart to implements this interface:

/*
 * MyChart.fx
 *
 * @author Henry Zhang     http://www.javafxgame.com
 */
package javatest;

import javafx.scene.chart.PieChart;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.chart.PieChart3D;

public class MyChart extends JavaInterface {
  var chartData :  PieChart.Data[] = [];

  public override function addData( l:String, v: Number):Void {
    var labelString = l;

    var data =  PieChart.Data {
      label : l
      value : v
      action: function() {
        println("{labelString} clicked!");
      }
     } ;

    insert data into chartData;
  }

  public override function showChart() : Void {
    var chart =
      PieChart3D {
        data : chartData
        pieThickness: 25
        pieLabelFont: Font{ size: 9 };
        pieToLabelLineOneLength: 10
        pieToLabelLineTwoLength : 20
        pieLabelVisible: true
        pieValueVisible: true
        translateY: -50
     };

    Stage {
      title: "PieChart Window"
      width: 520
      height: 300
      scene: Scene {
        content: [
          Text {
            font : Font {
                    size : 16
                   }
            x: 200
            y: 20
            content: "Pie Chart"
          },
          chart
        ]
      }
    }
  }
}

The last thing is to write the java main class JavaTest.

/*
 * JavaTest.java
 * @author Henry Zhang    http://www.javafxgame.com
 */
package javatest;

import javafx.reflect.FXClassType;
import javafx.reflect.FXLocal;
import javafx.reflect.FXLocal.Context;
import javafx.reflect.FXLocal.ObjectValue;

public class JavaTest {
  public static void main(String args[]) {
    Context context = FXLocal.getContext();
    FXClassType instance = context.findClass("javatest.MyChart");
    ObjectValue obj = (ObjectValue)instance.newInstance();

    JavaInterface ji = (JavaInterface)obj.asObject();

    String [] labels = {"January", "Febuary", "March", "April"};
    int [] values = { 18, 20, 25, 37 };

    for ( int i=0; i < values.length; i++ ) {
      ji.addData(labels[i], values[i]);
    }

    ji.showChart();
  }
}

In the above code, there are three lines for instantiating a JavaFX class via reflection:

    Context context = FXLocal.getContext();
    FXClassType instance = context.findClass("javatest.MyChart");
    ObjectValue obj = (ObjectValue)instance.newInstance();

The next line is to convert the JavaFX instance into a java interface so that it can be used by Java code:

    JavaInterface ji = (JavaInterface)obj.asObject();

If you are using NetBeans IDE, you can set javatest.JavaTest as the main class in your project properties(so that it can be the entry point of your program). Build this project you will get a javatest.jar. Running this program produces the below screenshot:


Java PieChart via JavaFX

To run it from the command line, use the below command:

   javafx -jar javatest.jar

Actually, you could do it in the purest java style by including all the JavaFX runtime stuffs, the command would look like this:

 java -Djava.library.path="<path to javafx sdk lib>"
     -classpath "<all javafx sdk jars>" -jar javatest.jar

Since there are many jar files used by the JavaFX, this purest java approach turns out to be very troublesome. I would rather use the javafx command, which is a wrapper of the above java command.

Please leave comments if you have any questions.

This article is cross-posted at Calling JavaFX Classes from Pure Java Code. The Chinese translation can be found at http://www.javafxblogs.com.

JavaFX Used in Winter Olympic Games 2010
Review on Essential JavaFX
US Citizenship Practice Test

Comments (7)

Jun 18

Pac-Man Widget for JavaFX 1.2

Posted: under JavaFX, JavaFX Coding, Javafx Games.
Tags: , , , , , June 18th, 2009

To fully take advantage of the power of JavaFX 1.2, Stephen Chin had just released the WidgetFX 1.2 API beta version. There is even a widget contest running until the end of July.

Just before the JavaOne 2009, Jim Weaver asked me to write a widget for my JavaFX Pac-man game. The WidgetFX API was quite simple to use, so I finished it pretty soon. The game widget later got demo-ed on Jim and Steve’s JavaOne sessions. There was a small problem of the pac-man widget: it run relatively slow due to the performance issue of JavaFX 1.1. Since JavaFX 1.2 and WidgetFX 1.2 are ready now, I am modifying the code to see the improvement on performance.

First, the code of the Pac-Man game needs to be modified a little bit for JavaFX 1.2 . Since multi-inheritance is gone, we need to use mixin classes now. You can refer to my articles on insideRIA.com for details of the code. Changes for JavaFX 1.2 were given on comments of Article 4 by Patrick Webster. I also added in a pausing key(”P” button) handling for the game. I compiled the game into a pacman.jar file.

The next step is to write the widget. Actually, the code is quite simple if you do not have stuffs like configuartion etc. Let’s take a look at the code below:

/*
 * PacManWidget.fx
 * http://www.javafxgame.com
 */

package pacmanwidget;

import org.widgetfx.Widget;

/**
 * @author Henry Zhang
 */

def defaultWidth = 528.0;
def defaultHeight = 576.0;

def maze =  pacman.Maze {};

var widget:Widget = Widget {
    width: defaultWidth
    height: defaultHeight
    aspectRatio: defaultWidth / defaultHeight
    resizable: false
    content: maze          

    onDock: function():Void {
       maze.pauseGame();
    }
}

return widget;

In the standalone game, an instance of the Maze class was put into the content variable of a Stage. Now, instead of putting it into a Stage, we add it into a Widget. To do this, we can just set the content variable of a Widget instance. Other attributes of the Widget class are quite straightforward, mostly for resizing purposes. The next thing is to write a onDock() function to pause the game when the widget gets docked. The game can be resumed after pressing the “p” button when it is undocked.

The last thing is to deploy it on a web server. We need a JNLP file. Be sure to write the jnlp file of the JavaFX 1.2 style. Netbeans can generate the JNLP file which we can modify for deployment. I listed below part of my jnlp file. Besides the widget code PacManWidget.jar, there are supporting jar files( pacman.jar and WidgetFX-API.jar) under the /lib folder as well. Notice that there is a bug in the generated JNLP file by NetBeans 6.5.1: the <update> tag is missing a slash(/) at the end of the tag.


 . . . . . .
 <resources>
     <j2se version="1.5+" />
     <extension name="JavaFX Runtime"
        href="http://dl.javafx.com/1.2/javafx-rt.jnlp"/>
     <jar href="PacManWidget.jar" main="true"/>
     <jar href="lib/pacman.jar"/>
     <jar href="lib/WidgetFX-API.jar"/>
 </resources>
 <application-desc main-class="com.sun.javafx.runtime.main.Main">
    <argument>MainJavaFXScript=pacmanwidget.PacManWidget</argument>
 </application-desc>
 <update check="background" />
 . . . . . .


Now, you can click on the below button to start the Pac-man widget for JavaFX 1.2. Enjoy!


Pac-Man Widget 1.2



Pac-Man Game Widget for WidgetFX 1.2

Comments (0)

Dec 25

Interoperability between JavaFX and Java

Posted: under JavaFX, JavaFX Coding.
Tags: , , , December 25th, 2008

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

From the official 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 really have the need of such kind of interaction between Java and JavaFX? I’d 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.

[Update: Please refer to my latest article for discussion on

Pure Java Code to Call JavaFX Class]

Diecast Cars NASCAR Collectables

Online US Citizenship Practice Test

Comments (2)

Aug 01

JavaFX Preview SDK

Posted: under JavaFX.
Tags: , , , , August 1st, 2008

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)