Home > Articles > Programming > Java

JavaFX Effects and Blending

  • Print
  • + Share This
In this chapter, you see how to use the classes in the javafx.scene.effects and javafx.scene.effects.lighting packages, which implement graphical effects that you can use to enhance the appearance of your application.
This chapter is from the book

This chapter is from the book

After discussing effects in general, the first part of this chapter describes 15 different effects that you can use to create blurs, shadows, warps, and various lighting effects. The second part describes the blending effect, which provides 19 different ways to combine two inputs, such as a node and another effect, to produce an output. The same 19 blending modes can also be applied to a group (and therefore also to a container) to control how the pixels for intersecting nodes are combined. The last part of this chapter looks at the ways in which you can light a scene by using the Lighting effect.

Effects are a feature of the desktop profile—they do not work on mobile devices—so the example source code for this chapter can all be found in the javafxeffects package in the JavaFX Book Desktop project. You can use the conditional feature API described in Chapter 12, "Platform API," to determine at runtime whether effects are available to your application.

Effects Overview

An effect is a graphical filter that accepts an input (and in some cases more than one input) and modifies it in some way to produce an output. The output is either rendered as part of a scene or becomes the input for another effect. The combination of a node and one or more effects is referred to here as an effects chain.

Effects Chains

Figure 20-1 shows two common effects chains. An effects chain contains, at minimum, one node and one effect.

Figure 20-1

Figure 20-1 Relationship between effects and nodes

In the first chain, at the top of the figure, a single effect is applied to a node, and the result is drawn onto the scene. The second chain contains two effects. In this case, the first effect is applied to the node, which results in an output image that becomes the input for the second effect. It is the output of the second effect that will be drawn onto the scene.

Effects and Nodes

When an effect is applied to a node, the output of the effects chain logically replaces the node itself on the screen. In general, an effect will change the bounds of a node. For example, adding a shadow to a node by using the DropShadow effect will typically make it wider and taller. The node's local and parent bounds are adjusted based on the result of the effects chain, but its layout bounds are not affected. When a node has both effects and transformations, the effect is applied before the transformations. This means, for example, that adding a shadow and then scaling up the node will also scale up the shadow.

An effect is linked to a node via its effect variable. 1 The code in Listing 20-1, which you will find in the file javafxeffects/Effects1.fx, shows how simple it is to add an effect to a node. In this case, a drop shadow is added by the three lines of code starting on line 19. Figure 20-2 shows the result.

Figure 20-2

Figure 20-2 A rectangle with a drop shadow effect

Listing 20-1. Adding an Effect to a Node

1      package javafxeffects;
3      import javafx.scene.effect.DropShadow;
4      import javafx.scene.paint.Color;
5      import javafx.scene.Scene;
6      import javafx.scene.shape.Rectangle;
7      import javafx.stage.Stage;
9      var rect: Rectangle;
10     Stage {
11         title: "Effects #1"
12         scene: Scene {
13             width: 150 height: 150
14             content: [
15                 rect = Rectangle {
16                     x: 10 y: 10
17                     width: 100 height: 100
18                     fill: Color.YELLOW
19                     effect: DropShadow {
20                         offsetX: 5 offsetY: 5
21                     }
22                 }
23             ]
24         }
25     }
26     println("Layout bounds: {rect.layoutBounds}");
27     println("Parent bounds: {rect.boundsInParent}");

The last two lines of Listing 20-1 print the layout bounds and parent bounds of the rectangle. Here's the result:

Layout bounds: BoundingBox [minX = 10.0, minY=10.0, maxX=110.0, maxY=110.0,
width=100.0, height=100.0]
Parent bounds: BoundingBox [minX = 6.0, minY=6.0, maxX=124.0,
maxY=124.0,width=118.0, height=118.0]

As you can see, the rectangle's layout bounds correspond to its specified width and height (because the layout bounds do not include the results of the effect), but width and height of the parent bounds have both increased from 100 to 118 because of the space occupied by the drop shadow.

Applying more than one effect is simply a matter of linking one effect to another. The following code (which you'll find in the file javafxeffects/Effects2.fx) adds a reflection to the drop shadow, giving the result shown in Figure 20-3:

Rectangle {
    x: 10
    y: 10
    width: 100
    height: 100
    fill: Color.YELLOW
    effect: Reflection {
   input: DropShadow {
   offsetX: 5 offsetY: 5
Figure 20-3

Figure 20-3 Applying two effects to the same rectangle

The linkage between the effects is made through the input variable of the reflection effect—the drop shadow is applied first, and the result of this becomes the input to the reflection effect. When no input is specified, the node itself is used as the input, as in the case of the drop shadow effect.

Not all effects have an input variable. Those that don't can only appear as the first (or only) entry in the effects chain. The DropShadow class is an example of this. 2 Other effects can have more than one input, such as the Blend effect that you'll see in the second part of this chapter.

As noted earlier, transformations are applied after any effects, so they apply to the effects, too. The following code, from the file javafxeffects/Effects3.fx, adds a rotation to the two effects that are applied to the Rectangle, as shown in Figure 20-4.

Rectangle {
    x: 10 y: 10
    width: 100 height: 100
    fill: Color.YELLOW
    rotate: -45
    effect: Reflection {
        input: DropShadow {
        offsetX: 5 offsetY: 5
Figure 20-4

Figure 20-4 Using effects and transformations together.

Effects and Groups

A particularly powerful feature of effects is that they can be applied to a group. An effect that is applied to a group operates on the group as a whole. This is particularly useful if you want to create an effect that is uniform across the scene, such as the direction of lighting.

The following code, from the file javafxeffects/Effects4.fx, applies a DropShadow effect to a group that contains a rectangle and a circle; as you can see in Figure 20-5, this gives both of the nodes a DropShadow effect:

Figure 20-5

Figure 20-5 Applying an effect to a Group

Group {
    effect: DropShadow {
        offsetX: 5 offsetY: 5
    content: [
        Rectangle {
            x: 10 y: 10
            width: 100 height: 100
            fill: Color.ORANGE
        Circle {
           centerX: 75 centerY: 160 radius: 30
           fill: Color.YELLOW
  • + Share This
  • 🔖 Save To Your Account