DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Creating Compiled JavaFX Script Applets - Method Two

Creating Compiled JavaFX Script Applets - Method Two

James Weaver user avatar by
James Weaver
·
Mar. 10, 08 · Java Zone · Interview
Like (0)
Save
Tweet
8.71K Views

Join the DZone community and get the full member experience.

Join For Free

in the previous post , i showed you one of the two ways that the javafx script compiler team has provided for creating compiled javafx script applets.  today's example program demonstrates the second of these ways, by converting the binding to a function example into a compiled javafx script applet.   here's a screenshot of the javafx script applet, running in firefox 3:

bindtofunctionapplet2

this method of creating a compiled javafx script applet is more "javafx-like" than the method that i showed you in the previous post .  using this approach, we:

  • declaratively express the javafx applet using the applet object as the top level (see bindtofunctionapplet2.fx below).
  • assign javafx.ui.applet to the code attribute of the applet element in the html file (see bindtofunctionappletpage2.html below).
  • specify an appletclass parameter with the name of the class in which the applet is expressed (in this case bindtofunctionapplet2 , see the html file below).

also, this approach requires that the applet be signed, so in this post i'm going to walk you through the process of creating a jar file that contains the applet classes, and signing each of the jar files.

compare the following code with the binding to a function example, the major difference being the absence of the frame object. it isn't required since the applet provides its own ui container.

/*
* bindtofunctionapplet2.fx - a compiled javafx program that demonstrates
* a "javafx-like" way of creating compiled javafx applets.
* it also demonstrates binding to a function.
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a javafx script example.
*/

import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.math;
import java.lang.system;

class circlemodel {
attribute diameter:number;

function getarea():number {
math.pi * math.pow(diameter / 2, 2);
}
}

applet {
var cmodel = circlemodel {}
content:
borderpanel {
center:
canvas {
content: [
circle {
cx: 240
cy: 250
radius: bind cmodel.diameter * 2
stroke: color.purple
strokewidth: 1
fill: color.magenta
},
text {
font:
font {
face: fontface.sansserif
style: fontstyle.bold
size: 24
}
x: 20
y: 10
stroke: color.red
fill: color.red
content: bind "diameter: {cmodel.diameter}"
},
text {
font:
font {
face: fontface.sansserif
style: fontstyle.bold
size: 24
}
x: 240
y: 10
stroke: color.red
fill: color.red
content: bind "area: {%3.2f cmodel.getarea()}"
}
]
}
bottom:
slider {
min: 0
max: 100
border:
titledborder {
title: "diameter:"
}
value: bind cmodel.diameter with inverse
minortickspacing: 5
majortickspacing: 10
paintticks: true
paintlabels: true
labels: [
sliderlabel {
value: 0
label:
simplelabel {
text: "0"
}
},
sliderlabel {
value: 50
label:
simplelabel {
text: "50"
}
},
sliderlabel {
value: 100
label:
simplelabel {
text: "100"
}
}
]
}
}
}

supplying an html file to run the applet

here is the html file (that i've named bindtofunctionappletpage2.html ) that runs the applet:

<html>
<body>
<applet code="javafx.ui.applet" width=480 height=560
archive="javafxrt.jar,scenario.jar,bindtofunctionapplet2.jar">
<param name="appletclass" value="bindtofunctionapplet2">
</applet>
</body>
</html>

you'll need to put the jar files (shown in the archive tag above) in the same directory as the html file. you can get these files from the compiler download in the dist/lib directory.  to compile this applet, i used the -target parameter so that it will run within browsers that have jre 5 or later:

javafxc -target 1.5 bindtofunctionapplet2.fx

as i mentioned previously, you'll need to create a jar file that contains the class files for this applet.  to do this, i used the following command at my operating system prompt:

jar cvf bindtofunctionapplet2.jar bindtofunctionapplet2*.class

you'll also need to sign the jar file that contains the applet classes, as well as the javafxrt.jar and scenario.jar files.  to do this, you'll need a signature key, which you can create with the following command:

keytool -genkey -alias jfx -dname "cn=john doe, o=jfx inc." (continued on next line)
  -validity 9999 -keystore jfx.keystore -keypass keypassword -storepass storepassword

use the following commands to sign the jar files:

jarsigner -keystore jfx.keystore -verbose -keypass keypassword (continued on next line) 
 
-storepass storepassword bindtofunctionapplet2.jar jfx
jarsigner -keystore jfx.keystore -verbose -keypass keypassword (continued on next line)
 
-storepass storepassword javafxrt.jar jfx
jarsigner -keystore jfx.keystore -verbose -keypass keypassword (continued on next line)
 
-storepass storepassword scenario.jar jfx

to execute this compiled javafx script applet, either open the bindtofunctionappletpage2.html file in a browser, or use appletviewer as shown below:

appletviewer bindtofunctionappletpage2.html

enjoy, and please post a comment if you have any questions!

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

JavaFX Script JavaFX

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Outsourcing, a Strong Business, and Management Approaches
  • Selenium vs. Protractor: What's the Difference?
  • Servlets Listeners Introduction and Examples
  • Troubleshooting Memory Leaks With Heap Profilers

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo