Search code examples
javajavafxclippingblending

Adding a BlendMode to a Clipped Node in JavaFX


Is there a way to add a BlendMode (or Blend effect) to a Node that has been clipped? It seems that if I try to add a clip to a Node with a BlendMode already set, the BlendMode is overridden by the clip and no longer works properly. Some example code to reproduce the problem:

package display.fx.demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.BlendMode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class BlendModeClipProblem extends Application {
    public Rectangle blueRect = new Rectangle(0, 0, 30, 30);
    public Rectangle redRect = new Rectangle(15, 15, 30, 30);

    @Override
    public void start(final Stage stage)
            throws Exception {
        final Pane pane = new Pane();
        final Scene scene = new Scene(pane);
        pane.getChildren().add(blueRect);
        pane.getChildren().add(redRect);

        blueRect.setFill(Color.BLUE);
        redRect.setFill(Color.RED);

        redRect.setBlendMode(BlendMode.ADD);

        // Comment this next line to see blending
        redRect.setClip(new Rectangle(15, 15, 20, 20));

        stage.setScene(scene);
        stage.show();
    }
}

Solution

  • Try something like this (use Cached Rectangle)

    @Override
        public void start(final Stage stage)
                throws Exception {
            final Pane pane = new Pane();
            final Scene scene = new Scene(pane);
            pane.getChildren().add(blueRect);
            pane.getChildren().add(redRect);
    
            blueRect.setFill(Color.BLUE);
            redRect.setFill(Color.RED);
    
            redRect.setBlendMode(BlendMode.ADD);
    
            redRect.setCache(true);
            redRect.setCacheHint(CacheHint.QUALITY);
    
            redRect.setClip(new Rectangle(15, 15, 20, 20));
    
            stage.setScene(scene);
            stage.show();
        }