【问题标题】:JavaFX clipping produces a 'lottery scratch ticket'-EffectJavaFX 剪裁产生“彩票刮奖券”-效果
【发布时间】:2017-10-20 10:33:01
【问题描述】:

我开始玩JavaFX GraphicsContext。 尤其是剪辑部分对我来说很有趣。

所以我尝试创建一些图形并为它创建一个剪贴蒙版(一个简单的矩形,可以四处移动)

但我注意到它有一些奇怪的行为(不确定这是错误还是代码使用不正确)

您可以在下面找到显示问题的示例应用程序。

描述我对我的代码的期望: 白色画布,带有带有文本的洋红色矩形,仅在洋红色上方可见(尽管它被绘制)

其实这正是你看到的第一

当您移动应用程序窗口时,洋红色矩形会四处移动(如预期的那样)!但是 ANTIQUEWHITE 填充变得可见(我从未预料到),并且任何曾经被洋红色覆盖的区域现在都可见(没有剪裁)

ANTIQUEWHITE 和 MAGENTA 的东西,用于使问题更加明显。由于整个画布在开始时被清除并且只完成了一次剪辑,因此重新绘制(或在旧图上绘制)应该不是问题

启动应用程序并移动它以查看“彩票刮奖券”效果

public class ClippingExampleApp extends Application {

    private boolean clip = true;
    private static Bounds clippingArea = new BoundingBox(100, 50, 300, 300);

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Clipping Test App");
        primaryStage.setX(100);
        primaryStage.setY(50);

        Group root = new Group();
        Canvas canvas = new Canvas(640, 480);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));

        ChangeListener<Number> updateBounds = new ChangeListener<Number>() {

            @Override
            public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
                clippingArea = new BoundingBox(primaryStage.getX(), primaryStage.getY(), 300, 300);
                draw(canvas, clip);
            }
        };
        primaryStage.yProperty().addListener(updateBounds);
        primaryStage.xProperty().addListener(updateBounds);
        primaryStage.widthProperty().addListener(updateBounds);
        primaryStage.heightProperty().addListener(updateBounds);

        primaryStage.show();
        clippingArea = new BoundingBox(primaryStage.getX(), primaryStage.getY(), 300, 300);
        draw(canvas, clip);
    }

    private static void draw(Canvas canvas, boolean clip) {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        // CLEAR THE COMPLETE CANVAS
        gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
        gc.save();
        if (clip) {
            // clipping rect
            gc.rect(clippingArea.getMinX(), clippingArea.getMinY(), clippingArea.getWidth(), clippingArea.getHeight());
            gc.clip();

            // fill the whole background (this should only affect the clipped
            // area 
            gc.setFill(Color.ANTIQUEWHITE);
            gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
            // this should overlap the Color.ANTIQUEWHITE - so no ANTIQUEWHITE is visible
            gc.setFill(Color.MAGENTA);
            gc.fillRect(clippingArea.getMinX(), clippingArea.getMinY(), clippingArea.getWidth(), clippingArea.getHeight());

            // finally fill the text, which sould only be visible where the magenta rect is...
            String text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.";
            gc.setFill(Color.BLACK);
            gc.fillText(text, 50, 100);
        }
        gc.restore();
    }
}

【问题讨论】:

  • 你有什么问题?
  • 显然问题是如何解决这种奇怪的行为并剪掉洋红色的区域

标签: java canvas javafx graphicscontext


【解决方案1】:

来自documentation for restore()

注意当前路径不会恢复。

因此,您进行的所有gc.rect() 调用都会累积到用作剪辑的单个路径中。

如果你添加

gc.beginPath();

要清除 if (clip) 块开头的路径,您会看到我认为您所期望的行为。

【讨论】:

  • 谢谢!实际上我认为每次绘画后都会清除路径 - 并且不需要 beginPath。
猜你喜欢
  • 2022-07-14
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多