« Really This Time - Part Three of Creating Tetris in Compiled JavaFX Script | Main | Creating Compiled JavaFX Script Applets - Method Two »

March 09, 2008

Creating Compiled JavaFX Applets - Method One (of Two)

Important Note:  As of the JavaFX SDK Preview, this article is technically outdated.  Please see the Using the Java Deployment Toolkit with JavaFX Applets article for an updated example of creating and deploying JavaFX Applets.

The JavaFX Script compiler team has provided two ways of creating compiled JavaFX Script applets.  Today's example program demonstrates one 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:

Bindtofunctionapplet1

This way of creating a JavaFX Applet is more "Java-like" in that it uses the approach of extending the javafx.ui.Applet class.  Notice that it overrides a couple of applet life-cycle functions to demonstrate that ability.  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.

/*
*  BindToFunctionApplet1.fx - A compiled JavaFX program that demonstrates
*                             one 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;

public class BindToFunctionApplet1 extends Applet {
  public function start(): Void {
    System.out.println("BindToFunctionApplet1.start()");
  }
  public function stop(): Void {
    System.out.println("BindToFunctionApplet1.stop()");
  }
}

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

BorderPanel {
  var cModel = CircleModel {}
  background: Color.WHITE
  center:
    Canvas {
      content: [
        Circle {
          cx: 240
          cy: 250
          radius: bind cModel.diameter * 2
          stroke: Color.PURPLE
          strokeWidth: 1
          fill: Color.CYAN
        },
        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 BindToFunctionAppletPage1.html) that runs the applet:

<html>
  <body>
    <applet code="BindToFunctionApplet1"
            width=480 height=560
     archive="javafxrt.jar, Scenario.jar">
    </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 BindToFunctionApplet1.fx

To execute this compiled JavaFX Script applet, either open the BindToFunctionAppletPage1.html file in a browser, or use appletviewer as shown below:

appletviewer BindToFunctionAppletPage1.html

Enjoy, and please post a comment if you have any questions!  The next post will cover the other way of creating compiled JavaFX Script applets.

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

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54f133d69883400e5502dd4378833

Listed below are links to weblogs that reference Creating Compiled JavaFX Applets - Method One (of Two):

Comments

Hi everyone..this is a great blog.

Im also having the same problem with the other post about 'ClassNotFoundException: javafx.ui.Applet' doing some javafx applet. Hope you can recommend/give a better example in creating javafx applet.thank you :)

David,

My next post is going to be about creating JavaFX applets with the Technology Preview Release of the JavaFX SDK which is different that the technique shown in this post. Please watch for it and let me know if you have any questions.

Hello! Great blog, congrats!
I just have a little problem. I'm getting a ClassNotFoundException for javafx.ui.Applet from the appletviewer and from the firefox java console everytime I try to run an Applet. I can run any other javafx app, but applets seem to have a problem.
I can compile it with no problem and I'm putting the javafx*.jar, and Scenario.jar in the correct directory and specifying them in the applet tag in the html file.
I have the JRE 6 update 6.

Hope I'm not annoying you.

Many Thanks

David

This is my Favorit Blog. Greatfull Report. Best Regards Autoversicherung

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

My Photo

Upcoming Speaking Engagements:


  • Stephen Chin and Jim Weaver speaking about JavaFX Platform

  • Speaking on JavaFX and Java at Øredev in Malmö, Sweden on 2-6 November, 2009

Upcoming JavaFX Training:


  • Developing Secure, Rich Internet Applications Hosted on a Variety of Clients Using JavaFX Technology

Enter your email address:

Delivered by FeedBurner

Available now as early access eBook


  • Click book image above to obtain eBook

Twitter Updates

    follow me on Twitter

    Affiliations:

    DZone Links:


    July 2009

    Sun Mon Tue Wed Thu Fri Sat
          1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31  

    Disclaimer:

    • By reading this site, you are agreeing that under no circumstances will Veriana Networks, Inc. or its affiliates be responsible for (1) any information contained on or omitted from the site, (2) any person's reliance on any such information, whether or not the information is correct, current or complete, (3) the consequences of any action you or any other person takes or fails to take, whether or not based on information provided by or as a result of the use of the sites.