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.
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:
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);
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."
0 comments:
Post a Comment