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:
<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
NOT WORKING IN FIREFOX WITH JRE 1.6.0_11(Windows Vista)
Here is the error i got in the console
Java Plug-in 1.6.0_11
Using JRE version 1.6.0_11 Java HotSpot(TM) Client VM
User home directory = C:\Users\Lenin
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to
----------------------------------------------------
load: class javafx.application.Applet.class not found.
java.lang.ClassNotFoundException: javafx.application.Applet.class
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: javafx.application.Applet.class
Posted by: Lenin | December 19, 2008 at 12:25 AM
Can i implement this javafx applet using NetBeans 6.0
thanks for the hint.
Posted by: wahyu | November 17, 2008 at 09:59 PM
The deployment process does not work in Opera (9.6 preview build). I have Java 1.5.0_04 installed on Windows XP, and it happily starts the applet, only to recognize that it does not know the class format (which is compiled for 1.6, i guess).
Posted by: Eabin | September 25, 2008 at 12:23 PM
Jim,
Still not starting. Not even getting anything in the console. Is there any way I can trace what's (not) happening?
Beginning to feel it's this client not chrome specific :/
Posted by: Erik | September 23, 2008 at 04:06 AM