Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

Animated JavaFX Meets the NetBeans Platform

01.27.2009
| 25860 views |
  • submit to reddit

I was talking to a NetBeans Platform customer last night, about Josh Marinacci's instructions for integrating JavaFX into Java applications. "Where," we wondered, "would doing that make sense?" Then we remembered that one of the new requirements for his application is that the Welcome Screen should be animated. Some flashy stuff should appear as soon as the user starts up the application, rather than boringly static links to documentation and so on. That's a great use case for JavaFX, we thought.

So, with renewed purpose, I worked through Josh's instructions. First, I prototyped using a standard JFrame:

And then I ported that solution to a TopComponent (which is a JPanel that integrates with the NetBeans Platform):

The window above appears in undocked state by default, at which point the swishy JavaFX stuff will unleash itself. Right now it's not much (x changes and color changes within a timeline), but that's why it's a prototype. One can imagine how cool that Welcome Screen could become. However, from the following instructions anyone out there should be able to create some impressive JavaFX effects integrated into Java, whether in a standard Swing container or on the NetBeans Platform.

First, I created two JavaFX classes in a JavaFX application in NetBeans IDE. The first class defines my Scene, while the second defines my Timeline:

package demo;

import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;

public var myColor = Color.RED;
public var myX = 20;

public class DemoScene extends Scene {
    init {
        content = [
            Rectangle {
                x: bind myX
                y: 20
                width: 200
                height: 200
                fill: bind myColor
            },
        ];
        //This references my Timeline extension class:
        DemoTimeline{}.play()
    }
}

public function run(args: String[]) {
    DemoScene {}
} 

And here is the definition of my Timeline class, which is called from my Scene class (above):

package demo;

import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;

var scene = DemoScene{}

public class DemoTimeline extends Timeline {
    init {
        autoReverse = true;
        repeatCount = Timeline.INDEFINITE;
        keyFrames = [
            KeyFrame {
                time: 1s
                canSkip: true
                values: [
                    scene.myColor => Color.GREEN
                    scene.myX => 200
                ]
            }
        ]
    }
}

That's all that's needed on the JavaFX side.

Next, in the first prototype, i.e., standard Swing, I created a new Java application, put the JavaFX libs on its classpath, together with the lib created from the above two JavaFX classes. Then I created a JFrame and added a JScrollPane. I also added the "JXScene" class from Josh's blog (referenced at the start of this article).That's what enables JavaFX to integrate with Java. (The sooner the JXScene class or something like it is introduced into the official JavaFX libs the better! Where can I go to vote for that?) The code below uses the JXScene class, putting it into the JScrollPane (called "mainPanel").

JXScene scene = new JXScene(); // create a new JXScene
scene.setScript("demo.DemoScene"); // the name of your main JavaFX class
mainPanel.setViewportView(scene); // add the scene to your Swing component

And that's all. For the integration with the NetBeans Platform, create three modules (one for the JavaFX libs, one for the JavaFX application above, and one containing the TopComponent that provides the Welcome Screen). Then do the same as for the JFrame, i.e., add the JXScene to the JScrollPane and Bob's your auntie. You've now integrated JavaFX with the NetBeans Platform and the sky's the limit. And, last but not least, this means that JavaFX can also be integrated as a new section in the NetBeans Platform Certified Training!

 

AttachmentSize
fig-1-jfx-anim.png5.98 KB
fig-2-jfx-anim.png8.5 KB
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Mariusz Fcfcfc replied on Wed, 2009/03/11 - 3:51am

But how can we use coustom swing component in javafx script? Is it possible?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.