在JavaFX中将BlendMode添加到剪切节点

时间:2017-12-12 23:53:39

标签: java javafx clipping blending

有没有办法将BlendMode(或Blend效果)添加到已被剪裁的节点?似乎如果我尝试将一个剪辑添加到已经设置了BlendMode的Node,则BlendMode会被剪辑覆盖并且不再正常工作。一些示例代码重现问题:

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();
    }
}

1 个答案:

答案 0 :(得分:2)

尝试这样的事情(使用缓存矩形)

@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();
    }