JavaFX App Deployment

September 19, 2008

Using the Java Deployment Toolkit with JavaFX Applets

First, let me apologize for resurrecting the very humble JavaFX program shown below, but I want to keep this example very succinct.  This will enable you to use it as "starter code" for JavaFX applet deployment.  Note: To see more functional JavaFX programs, please see articles in the JFX Custom Nodes category.

BindToFunctionApplet_SDK_Preview

Note: Thanks to reader "mbien" (see comments) for pointing our that the colors of the original applet in this post were hideous (my words).  I then consulted graphics designer Mark Dingman of Malden Labs who gave me a graphical mock-up from which I created the above applet.  Here's the code for this applet, updated for the JavaFX SDK preview:

/*
 *  BindToFunctionApplet.fx - A compiled JavaFX program that demonstrates
 *                            how to create JavaFX applets.
 *                            It also demonstrates binding to a function.
 *
 *  Developed 2008 by Jim Weaver (development) and Mark Dingman (graphic design)
 *  to serve as a JavaFX Script example.
 */
package com.javafxpert.bind_to_function;

import javafx.application.*;
import javafx.ext.swing.*;
import javafx.scene.*;
import javafx.scene.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.scene.transform.*;
import java.lang.Math;

class CircleModel {
  attribute diameter:Integer;
  
  bound function getArea():Number {
    Math.PI * Math.pow(diameter / 2, 2);
  }
}

Application {
  var cModel = CircleModel {};
  var componentViewRef:ComponentView;
  var stageRef:Stage;
  stage: 
    stageRef = Stage {
      var labelFont = Font {
        name: "Sans Serif"
        style: FontStyle.PLAIN
        size: 32
      }
      fill:
        LinearGradient {
          startX: 0.0
          startY: 0.0
          endX: 0.0
          endY: 1.0
          stops: [
            Stop { 
              offset: 0.0 
              color: Color.rgb(0, 168, 255) 
            },
            Stop { 
              offset: 1.0 
              color: Color.rgb(0, 65, 103) 
            }
          ]
        }
      content: [
        Circle {
          centerX: 250
          centerY: 250
          radius: bind cModel.diameter / 2
          fill:
            LinearGradient {
              startX: 0.0
              startY: 0.0
              endX: 0.0
              endY: 1.0
              stops: [
                Stop { 
                  offset: 0.0 
                  color: Color.rgb(74, 74, 74) 
                },
                Stop { 
                  offset: 1.0 
                  color: Color.rgb(9, 9, 9) 
                }
              ]
            }
        },
        Text {
          font: labelFont
          x: 30
          y: 70
          fill: Color.BLACK
          content: bind "Diameter: {cModel.diameter}"
        },
        Text {
          font: labelFont
          x: 260
          y: 70
          fill: Color.BLACK
          content: bind "Area: {%3.2f cModel.getArea()}"
        },
        componentViewRef = ComponentView {
          transform: bind 
            Translate.translate(40, stageRef.height - 30 -
                                   componentViewRef.getHeight())
          component:
            Slider {
              minimum: 0
              maximum: 400
              preferredSize: bind [stageRef.width - 80, 20]
              value: bind cModel.diameter with inverse
            }
        }
      ]
    }
}

Why Use the Java Deployment Toolkit for Java Applets?

According to Sun's Java Deployment Toolkit overview page, "Desktop clients have a wide variety of Java Platforms installed, from the Microsft VM to Sun's latest Java SE 6 updates. They run various operating systems from Sun, Microsoft, Apple, Red Hat, and others, and are connected to the internet at a wide range of connection speeds. How are content providers to deliver Java content to all of these clients with the best possible user experience?

Various sources have published JavaScript techniques for detecting and deploying the Java Platform for use by Java Plug-In applets and Java Web Start applications. These scripts generally have serious limitations and fail to support the varied combinations of browser, OS, and configuration options found on today's clients.

The Java Deployment Toolkit allows developers to easily deploy applets and applications to a large variety of clients with JavaScripts. It also provides advice on using some of the most powerful features available in Java Web Start and Java Plug-In, and an outline of the differences between these two deployment vehicles.
"

In a nutshell, the Java Deployment Toolkit is a JavaScript library maintained by Sun and always available at runtime by your HTML code.  This library has several methods that perform tasks such as sensing Java-related infrastructure and installing the JRE on client machines.  We'll use one of these methods, namely runApplet, to run a JavaFX applet with a specified minimum JRE version.  Here's the HTML and JavaScript code I'm using to deploy today's example applet:

<html>
<script src="http://java.com/js/deployJava.js"></script>
<script>
  var attributes = {codebase:'http://jmentor.com/JFX/BindToFunctionApplet',
    code:'javafx.application.Applet.class',
    archive:'BindToFunctionApplet.jar, javafxrt.jar, Scenario.jar, javafxgui.jar, javafx-swing.jar',
    width:500, height:500, java_arguments:'-Djnlp.packEnabled=true'};
  var parameters = {"ApplicationClass":"com.javafxpert.bind_to_function.BindToFunctionApplet",
                    "draggable":"true"};
  var version = '1.6.0' ;
  deployJava.runApplet(attributes, parameters, version);
</script>
</html>


Notice that the above code enables dragging the applet onto the desktop, as well as using Pack200 formatted JAR files, if the client machine has Java SE 6 update 10 installed.  Give the applet a whirl to see its deployment behavior on your machine.  By the way, according to the Java SE 6 Update 10 plug-in docs, "by default, the gesture to drag the applet out of the web browser is Alt + Left click + Drag."

Thanks,
Jim Weaver
JavaFXpert.com weblog

February 18, 2008

Hear Juicy Bits about J6u10 and JavaFX in Java Posse Interview

If you've got a few minutes for a silly conversation that has some revealing insights into Java SE 6 Update 10 (the Get Deployment Right release) and JavaFX, then listen to this Java Posse interview with Josh Marinacci and Rich Bair from Sun, starting at minute 42:00 in the podcast.  By the way, here are the mugshots of the Java Posse:

Javaposse_2

Right out of the gate (after an inevitable comment from someone about beer), Joshua made the rather insightful remark that "JavaFX is sort of a code word for reinventing client Java and fixing the sins of the past".  They go on to discuss some of the cool features of the new browser plug-in (according to the interview they "shot the old one in the head and buried it out back"), and about J6u10 in general.

For more background and info on J6u10, see my Java Kernel post.  Enjoy the podcast, but please don't say I didn't warn you that it contains mucho sillyness :-)

JavaFXpert BootCamp Registration Now Open

Javafxpertbootcamp300x250bannerindy

I will be offering a 2.5 day JavaFX Script day "boot camp" on Wednesday, April 9 through Friday, April 11, 2008 (ending at noon) in Indianapolis, Indiana. This course is designed to get you quickly up to speed in JavaFX Script application development. A primary reference for this course is my JavaFX Script book, but the course has its own syllabus which includes material covered in the book as well as up to the minute developments in compiled JavaFX Script. Registration is now open, and I am accepting 12 students in this pilot class. The cost of this pilot class will be 900 USD, and additional people from the same company will be 600 USD. You'll need to bring your laptop computer with the latest versions of the JavaFX Script downloads (which I'll specify in more detail as the class date approaches). I'm looking forward to teaching this class and hope that you can attend!

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications

Immediate eBook (PDF) download available at the book's Apress site

 

January 16, 2008

Java Kernel: The Final Piece of the JRE Deployment Puzzle is in Place!

As mentioned in previous posts, and in Weiqi Gao's Game Changing Move post, the purpose of Java SE 6 Update N is to solve the JRE deployment and performance issues that have hindered the adoption of Java rich clients.  This is great news for JavaFX Script as it is dependent upon the JRE (Java Runtime Environment) being present on the client machine.  Java Kernel, which was the remaining feature of Java SE 6 Update N to be implemented, is now in place.  This feature allows the JRE to be segmented into small bundles, each of which downloads and installs quickly.  The first bundles provides just enough of the JRE to run a simple "Hello World" program.  Java Kernel will then proactively download and install the remaining bundles.  The major benefit of this approach is that the user doesn't have to wait for a large JRE download before running a Java or JavaFX Script program, because the bundles that are needed by the program are downloaded in the background as the program is starting up.  Note: When I tried this, 35 bundles were downloaded, ranging in size from 52 KB to 2610 KB.  Here is the message box that appeared when I invoked a JavaFX Script application via Java Web Start just after the Java Kernel had been installed:

Javakerneladdionalcomponentsneede_2

Please also note that even if a Java or JavaFX program is not invoked, the Java Kernel will continue installing bundles until the entire JRE is installed.

For more information, see the Java Kernel FAQ.  Also, you can obtain the latest release of Java SE 6 Update N from this page. Of course, if you download and install Java SE 6 Runtime Update N build 10, you won't see the Java Kernel in action, because its purpose is to download the JRE.  If you want to observe the Java Kernel in action, just download the Java SE 6 Runtime (JRE) Update N build 10 Kernel Installer from that page (after having removed Java SE 6 Update N if it was already on your machine).  The FAQ referenced above explains how to tell when the Java Kernel has not yet finished downloading the complete JRE.

So here's the really neat part for compiled JavaFX Script: The plan is to make bundles out of the JavaFX platform jar files, as well as to leverage the automatic update facility of Java SE 6 Update N to keep the JavaFX jar files current.  As Chris Oliver puts it, "we have to be competitive with the Flash experience".

Here are a couple of other interesting JavaFX Script-related things to check out:

Compiled JavaFX Script and Java SE 6 Update N are quickly coming together to the point where we'll soon have fast deploying and executing JavaFX Script RIAs (rich internet applications) that are simple, elegant, and leverage the power of Java.  The next few months are going to be exciting, and in my opinion, will revolutionize (and greatly simplify) the way we develop internet applications.

JavaFX Script Boot Camp Announcement

As a heads-up, I will be offering a JavaFX Script 2.5 day "boot camp" on Wednesday, April 9 through Friday, April 11, 2008 (ending at noon) in Indianapolis, Indiana.  This course is designed to get you quickly up to speed in JavaFX Script application development.  A primary reference for this course is my JavaFX Script book, but the course has its own syllabus which includes material covered in the book as well as up to the minute developments in compiled JavaFX Script. Registration will open soon, and for this pilot class I am accepting 12 students.  The cost of this pilot class will be 900 USD per student. Additional students from the same organization will be 600 USD.  You'll need to bring your laptop computer with the latest versions of the JavaFX Script downloads (which I'll specify in more detail as the class date approaches).  The prerequisite for the class will be the completion of a JavaFX Script programming assignment that I'll post soon to this weblog.  I'm looking forward to teaching this class and hope that you can attend!

More details to follow,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site