Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Jump into JavaFX, Part 1: JavaFX Preview SDK

Experience JavaFX with NetBeans 6.1 and Project Nile

  • Print
  • Feedback

Page 3 of 6

On a Windows platform, NetBeans defaults to storing its projects in separate directories under the 'C:\Documents and Settings\user name\My Documents\NetBeansProjects' folder.

Figure 8. On a Windows platform, NetBeans defaults to storing its projects in separate directories under the C:\Documents and Settings\user name\My Documents\NetBeansProjects folder. (Click to enlarge.)

Figure 8 reveals HelloJavaFX as the project's name -- it's traditional to introduce a new technology via some sort of "hello world" program (or script in JavaFX-speak). It also reveals Main as the default name (.fx is the extension) of the script's file, and other default settings. After clicking the Finish button, you'll discover the skeletal source code shown in Figure 9.

The workspace assigns a separate tab to each source file.

Figure 9. The workspace assigns a separate tab to each source file. (Click to enlarge.)

Although the "hello world" script could output its Hello, JavaFX! message to the standard output (which is the workspace's Output window), this option is boring and doesn't reveal what JavaFX is all about. So, instead we'll have this script rotate the message while changing its opacity over a gradient-rendered background. Before we look at the script's output, examine Listing 1.

Listing 1. Main.fx

/*
 * Main.fx
 *
 */

package hellojavafx;

/**
 * @author Jeff Friesen
 */

import java.lang.System;

import javafx.animation.Interpolator;
import javafx.animation.Timeline;

import javafx.application.Frame;
import javafx.application.Stage;

import javafx.scene.Font;

import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;

import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;

class Model
{
    attribute text: String;
    attribute opacity: Number;
    attribute rotAngle: Number
}

var model = Model
{
    text: "Hello, JavaFX!"
}

Frame
{
    title: bind model.text

    width: 300
    height: 300

    var stageRef: Stage
    stage: stageRef = Stage
    {
        fill: LinearGradient
        {
            startX: 0.0
            startY: 0.0
            endX: 0.0
            endY: 1.0
            stops:
            [
                Stop { offset: 0.0 color: Color.BLACK },
                Stop { offset: 1.0 color: Color.BLUEVIOLET }
            ]
        }

        var textRef: Text
        content:
        [
            textRef = Text
            {
                content: bind model.text
                x: bind (stageRef.width-textRef.getWidth ())/2
                y: bind (stageRef.height-textRef.getHeight ())/2
                textOrigin: TextOrigin.TOP

                rotate: bind model.rotAngle
                anchorX: bind textRef.x+textRef.getWidth ()/2
                anchorY: bind textRef.y+textRef.getHeight ()/2

                font: Font
                {
                    name: "Arial"
                    size: 30
                }
                fill: Color.YELLOW
                stroke: Color.ORANGE

                opacity: bind model.opacity
            }
        ]
    }

    visible: true

    // If a function isn't assigned to closeAction, the script automatically
    // terminates. If a function is assigned to this attribute, it must include
    // System.exit() to terminate the script.

    closeAction: function ()
    {
        System.exit (0)
    }
}

var timeline1 = Timeline
{
     autoReverse: true
     repeatCount: Timeline.INDEFINITE

     var begin = at (0s)
     {
         model.opacity => 0.0
     }

     var end = at (4s)
     {
         model.opacity => 1.0 tween Interpolator.LINEAR
     }

     keyFrames: [begin, end]
}
timeline1.start ();

var timeline2 = Timeline
{
     repeatCount: Timeline.INDEFINITE

     var begin = at (0s)
     {
         model.rotAngle => 0.0
     }

     var end = at (5s)
     {
         model.rotAngle => 360.0 tween Interpolator.LINEAR
     }

     keyFrames: [begin, end]
}
timeline2.start ();

//model.text = "I"

If you're new to JavaFX Script, much of Listing 1 will probably look alien to you. This is true even though you should already be familiar with import statements, the single-line comment style, class declaration via the class keyword, and brace character ({}) delimiters -- all being borrowed from Java. What you see in Listing 1 will make more sense after you've explored the JavaFX Script language and associated APIs in the second and third parts of this series.

  • Print
  • Feedback

Resources

More from JavaWorld