Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 10 ■ User InterfaCe DesIgn InteraCtIvIty: event hanDlIng anD ImagIng effeCts

An Effect object can also be associated with a Scene Graph Node (rather than an Image object) by
setting an attribute called Node.effect, that is, the effect attribute for the Node class (or object created from a
Node subclass).
Some effects, such as ColorAdjust, will change the color characteristics (hue, lumination, and
saturation) for the source pixels, while others such as Blend will combine multiple images together
algorithmically (via Porter-Duff ).
The DisplacementMap and PerspectiveTransform special effects classes will warp, or move, the pixels
of the source image around in 2D space to simulate 3D space, commonly called “2.5D” or “isometric” spatial
optical effects.
All JavaFX special effects have at least one input defined. Additionally, this input can be set to
another Effect object, allowing developers to chain the Effect objects together. This allows developers
to combine the Effect results, allowing compound or hybrid special effects to be created. This input can
also be left “unspecified,” in which case the effect will apply its algorithm to the graphical rendering (pixel
representation or rendering result) of the Node object that it has been attached to using a .setEffect() method
call or back onto the Image object that has been provided.
It’s important to note that special effects processing is a conditional feature. The ConditionalFeature.
EFFECT enum class and constant will define a set of conditional (supported) special effects features. These
features may not be available on all operating system or on all embedded platforms, although “modern-day”
consumer electronics devices can usually support effects processing as well as i3D rendering using their
hardware GPU graphics-processing abilities.
If your pro Java games application wanted to poll the hardware platform to ascertain whether any
particular effect feature is available, you may query effects support using the Platform.isSupported()
method call. If you use any conditional feature on a platform that does not support it, it will not cause an
exception. In general, the conditional feature will simply be ignored so that you don’t have to code any
specific error-trapping or error-handling Java code.
Next, let’s take a look at how you would implement one or two of these special effects in the UI design
and add drop shadows to the TextFlow object so the text that it displays is made more readable using
increased contrast. After that, we will take a look at the way that you can shift your digital image color around
the visible color spectrum.


Creating Special Effects: Add a createSpecialEffects() Method


Let’s follow our trend of organizing your Java code and create a method for setting up all of your
special effects called .createSpecialEffects(). Have NetBeans 9 create an empty private void
createSpecialEffects() {...} infrastructure by adding a line of code to call it in the start() method, after
the createTextAssets() method call, as shown highlighted in Figure 10-9. The logic here is that we will first
load images, then define effects, and then create text.

Free download pdf