 |
Chris Campbell's Blog
Chris Campbell is an engineer on the Java 2D Team at Sun Microsystems, working on OpenGL hardware acceleration and imaging related issues. He will be out the door as soon as his rock and roll career takes off. Anyone that knows Chris understands that he is full of contradictions and that his music career will never come to fruition. Therefore, you can trust that he will be working hard for years to come to make Java the perfect platform for rich graphical applications and games.
Effects in JavaFX: Chaining
Posted by campbell on June 11, 2009 at 02:04 AM | Permalink
| Comments (1)
Picking up where I left off six (cough, cough) months ago, in my series on the effects framework in JavaFX...
In JavaFX, you can chain effects together, contrary to a couple blog entries I've come across recently. This feature may not be entirely obvious from the API documentation, so let's look at chaining in a bit more detail here.
Just like Nodes in the JavaFX scene graph, Effects can be linked together in a tree-like structure. Most Effect subclasses expose one or more "input" variables that allow you to chain Effect instances together. By default, the input variable is set to null, indicating that the rendered contents of the Node to which the Effect is attached will be used as the input to the effect. For example, in the following code snippet, since we do not set the BoxBlur.input variable, the blur effect will be applied to the Text node content:
Text {
effect: BoxBlur {}
content: "Hello!"
...
}
If we instead explicitly set the BoxBlur.input variable to, say, a SepiaTone instance, the sepia effect will first be applied to the node content, and the result of that operation will then be blurred:
Text {
effect: BoxBlur {
input: SepiaTone {}
}
content: "Hello!"
...
}
The following is a simple applet that demonstrates what a Text node looks like when one or more effects are chained to it:
(The source bundle for this demo is available here.)
Note that in some situations, the order of the chained effects can be quite significant. One such scenario is in "cover flow" style components, where each node has a Reflection effect and is positioned in (faux) 3D using a PerspectiveTransform. In this case, it is important to apply the reflection effect first, and then transform the result last. If you try it the other way around, the result will be goofily [goofily, really?] non-realistic. The applet above demonstrates the difference in behavior of the two orderings.
Unfortunately, due to a minor technical limitation, we have not yet exposed an input variable on the DropShadow and InnerShadow classes as of the 1.2 release. This will certainly be resolved in an upcoming release. In the meantime, you can achieve the same result (albeit with a bit more code) by using nested Groups. For example, we can apply a Lighting effect to a Text, and then wrap that in a Group that has a DropShadow applied to it:
Group {
effect: DropShadow {
radius: 14
offsetX: 4
offsetY: 4
}
content: Text {
effect: Lighting {
light: DistantLight { azimuth: -135 }
surfaceScale: 5
}
}
}
In the next few installments, I'll look at individual effects such as Lighting and Reflection in more detail. (Let's hope this time I get to it sooner than six months from now!)
In my ears: Charles Mingus, The Black Saint And The Sinner Lady
In my eyes: Ricky Gervais and Karl Pilkington, The World of Karl Pilkington
 |
 |
June 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 |
|
|
|
|
Search this blog:
Categories
Community
Community: Java Games
Community: JavaDesktop
Deployment
Games
J2SE
JavaOne
Open Source
Performance
Porting
Swing
Tools
Web Services and XML
Archives
June 2009
January 2009
February 2008
April 2007
March 2007
February 2007
January 2007
October 2006
July 2006
April 2006
February 2006
January 2006
September 2005
July 2005
March 2005
November 2004
September 2004
March 2004
September 2003
July 2003
June 2003
Recent Entries
Effects in JavaFX: Chaining
Effects in JavaFX: Quality
Effects in JavaFX: The Basics
Articles
The Perils of Image.getScaledInstance()
Lots of developers use the simple Image.getScaledInstance() that's been around since Java 1.1, apparently not realizing that Java 2D provides better-looking, more performant, and more flexible options. Chris Campbell checks in with where image scaling is in Java SE 6 and what we might see in JDK 7. Apr. 3, 2007 Behind the Graphics2D: The OpenGL-based Pipeline
This document describes the current state of the OpenGL-based pipeline as of J2SE 5.0. Nov. 12, 2004
All articles by Chris Campbell »

|