JavaFX GraphicsContext clearRect不适用于剪辑蒙版 [英] JavaFX GraphicsContext clearRect doesn't work with clip mask

查看:181
本文介绍了JavaFX GraphicsContext clearRect不适用于剪辑蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GraphicsContext的clearRect方法的文档指出它使用了当前剪辑,但是当前不适用于我.考虑:

The documentation for the clearRect method of GraphicsContext states that it uses the current clip, but this isn't currently working for me. Consider:

GraphicsContext context = canvas.getGraphicsContext2D();
context.beginPath();
context.rect(0,0,100,100); //Set the current path to a rectangle
context.stroke(); //Highlights where the current path is
context.clip();   //Intersect current clip with rectangle
context.fillOval(80, 80, 40, 40); //This correctly draws the oval clipped
context.clearRect(0,0,100,100); //This does nothing at all

上面的代码正确设置了剪贴蒙版,可以通过fillOval正常工作这一事实来证明,但是clearRect却不起作用(尽管在没有context.clip()的情况下也可以正常工作).为什么是这样?

The above code sets the clip mask correctly, as evidenced by the fact that fillOval works correctly, however clearRect does nothing (although it works normally without the context.clip()). Why is this?

(请注意,我特别需要剪贴蒙板才能工作,稍后我打算将其设置为特定形状以擦除非矩形形状.)

(Note that I specifically need the clip mask to be working, as later I plan on setting it to specific shapes to erase in non-rectangular shapes.)

-编辑-

要清楚,clearRect实际上不执行任何操作,甚至不擦除椭圆形.我意识到它不会擦除描边的矩形,但这不是我所关心的.

To be clear, clearRect does literally nothing, not even erase the oval. I realise that it won't erase the stroked rectangle but that's not what I'm concerned about.

-编辑2-

更新到最新的JDK已部分解决了该问题.上面的代码现在可以正常工作.但是,使用非矩形夹蒙版仍然存在相同的问题.例如

Updating to the latest JDK has partially fixed the issue. The above code now works correctly. However, using a non-rectangular clip mask still has the same problem. e.g.

GraphicsContext context = canvas.getGraphicsContext2D();
context.beginPath();
context.arc(50, 50, 40, 40, 0, 360); // Make a circular clip mask
context.closePath();
context.clip();
context.fillRect(0, 0, 200, 200); //Draw a circle clipped correctly, shows clip mask is working
context.clearRect(0, 0, 200, 200); //Does nothing

我意识到我可以使用保存和恢复来恢复矩形剪贴蒙版,然后clearRect可以工作.但是,我希望能够以非矩形形状擦除.

I realise I could use save and restore to get a rectangular clip mask back, and then clearRect would work. However I want to be able to erase in non-rectangular shapes.

用于重现此代码的完整代码是(通过在eclipse中创建一个新的JavaFX项目并添加上述行来创建的):

Full code for reproducing this is (created by making a new JavaFX project in eclipse and adding the above lines):

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 400, 400);
            primaryStage.setScene(scene);
            primaryStage.show();

            Canvas canvas = new Canvas(500, 500);
            root.getChildren().add(canvas);

            GraphicsContext context = canvas.getGraphicsContext2D();
            context.beginPath();
            context.arc(50, 50, 40, 40, 0, 360);
            context.closePath();
            context.clip();
            context.fillRect(0, 0, 200, 200);
            context.clearRect(0, 0, 200, 200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

这应该显示一个空白屏幕,但是圆圈没有被清除.

This should show a blank screen, but the circle is not being cleared.

推荐答案

根据Edit 2,此行为似乎是一个错误.总而言之,当设置了非矩形剪辑时,clearRect无效. (如果即使使用矩形剪辑也无法使用它,请更新到最新的JDK.)我已经为此提交了一个错误报告.

As per Edit 2, this behaviour seems to be a bug. In summary, clearRect has no effect when a non-rectangular clip is set. (If it isn't working for you even with a rectangular clip, update to the latest JDK.) I've filed a bug report for this.

这篇关于JavaFX GraphicsContext clearRect不适用于剪辑蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆