Java Link

See which of your colleagues or former colleagues are already on Java Link: Check out the Contact Finder
News »Browse Articles » Animated JavaFX meets the NetBeans platform
0
Vote Vote

Animated JavaFX meets the NetBeans platform

Views 9 Views    Comments 0 Comments    Share Share    Posted 27-01-2009  

JPanel that integrates with the NetBeans Platform

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:
view plaincopy to clipboardprint?

1. package demo;
2.
3. import javafx.scene.Scene;
4. import javafx.scene.shape.Rectangle;
5. import javafx.scene.paint.Color;
6.
7. public var myColor = Color.RED;
8. public var myX = 20;
9.
10. public class DemoScene extends Scene {
11. init {
12. content = [
13. Rectangle {
14. x: bind myX
15. y: 20
16. width: 200
17. height: 200
18. fill: bind myColor
19. },
20. ];
21. //This references my Timeline extension class:
22. DemoTimeline{}.play()
23. }
24. }
25.
26. public function run(args: String[]) {
27. DemoScene {}
28. }

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):
view plaincopy to clipboardprint?

1. package demo;
2.
3. import javafx.animation.Timeline;
4. import javafx.animation.KeyFrame;
5. import javafx.scene.paint.Color;
6. import javafx.animation.Timeline;
7. import javafx.animation.KeyFrame;
8. import javafx.animation.Interpolator;
9.
10. var scene = DemoScene{}
11.
12. public class DemoTimeline extends Timeline {
13. init {
14. autoReverse = true;
15. repeatCount = Timeline.INDEFINITE;
16. keyFrames = [
17. KeyFrame {
18. time: 1s
19. canSkip: true
20. values: [
21. scene.myColor => Color.GREEN
22. scene.myX => 200
23. ]
24. }
25. ]
26. }
27. }

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").
view plaincopy to clipboardprint?

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

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!

Source:
http://java.dzone.com/news/animated-javafx-meets-netbeans
0
Vote  Vote
Enter your comment:
No Comments For This News

Search News

What's the News?

Post a link to something interesting from another site, or submit your own original writing for the Java community to read.

Most Popular News

Most Recent User Submitted News