Creating Visual Effects in JavaFX

Previous
Next

1 Blending Objects

Blend is an effect that combines two objects using one of the predefined blend modes. A blend mode defines the manner in which the objects are mixed together. For example, in Figure 1-1, you can see examples of some blend modes applied to an intersecting circle and a square.

Figure 1-1 Different Blend Modes

Description of Figure 1-1 follows
Description of "Figure 1-1 Different Blend Modes"

Note that the produced effect also depends on the order of the elements in the code. For example, Figure 1-2 shows the difference that appears when the blend mode is applied to objects specified in the code in a different order.

Figure 1-2 Same Blend Mode Applied to Objects in Different Order

Description of Figure 1-2 follows
Description of "Figure 1-2 Same Blend Mode Applied to Objects in Different Order"

Example 1-1 shows a code snippet for the blend effect in the sample application.

Example 1-1 Blend Effect

    static Node blendMode() {
        Rectangle r = new Rectangle();
        r.setX(590);
        r.setY(50);
        r.setWidth(50);
        r.setHeight(50);
        r.setFill(Color.BLUE);
 
        Circle c = new Circle();
        c.setFill(Color.rgb(255, 0, 0, 0.5f));
        c.setCenterX(590);
        c.setCenterY(50);
        c.setRadius(25);
 
        Group g = new Group();
        g.setBlendMode(BlendMode.MULTIPLY);
        g.getChildren().add(r);
        g.getChildren().add(c);
        return g;
Previous
Next