用FXGraphics2D(Java.awt)替换Java中的剪辑

问题描述 投票:0回答:1

因此,我正在使用Java处理FXGraphics2D应用程序,其中您的形状被裁剪,并且您会在背景中看到随机生成的彩色线条背景。一切都很好,但是随后我想添加该功能以使用鼠标移动剪辑Shape。

在这种情况下,鼠标单击在画布上或被拖动到的位置。您不必在形状上拖动FYI。

似乎存在一个问题,即删除剪辑或仅清理画布。现在,由于您对FXGraphics2D相当陌生,但是我自己在文档或互联网上的任何地方都找不到它。

没有人知道如何解决完全清除画布,从剪辑中分离出来或以某种方式替换剪辑的问题吗?

提前感谢!

单击几下后的当前状态:https://i.stack.imgur.com/dPbqI.png在我看来,FXGraphics2D将下一个剪辑放在旧的剪辑后面。

我到目前为止的代码;

绘制

public void draw(FXGraphics2D graphics) {
        graphics.setTransform(new AffineTransform());
        graphics.setBackground(Color.white);
        graphics.clearRect(0, 0, (int)canvas.getWidth(), (int)canvas.getHeight());

        graphics.setPaint(Color.black);
        graphics.draw(clippingEllipse.getShape());
        graphics.clip(clippingEllipse.getShape());

        Set<Line> lineSet = Line.getLineSet(1000,1920,1080);
        for (Line line : lineSet){
            graphics.setPaint(line.getColor());
            graphics.drawLine(line.getX1(), line.getY1(), line.getX2(), line.getY2());
        }
    }

Handle(setOnMousePressed()和setOnMouseDragged()的方法调用]

private void handle(MouseEvent event) {
        this.clippingEllipse.setX((int) event.getX() - this.clippingEllipse.getWidth() / 2);
        this.clippingEllipse.setY((int) event.getY() - this.clippingEllipse.getWidth() / 2);
    }

开始

        this.clippingEllipse = new ClippingEllipse(1920/2-100, 1080/2-100, 200);

        BorderPane mainPane = new BorderPane();
        canvas = new ResizableCanvas(g -> draw(g), mainPane);
        mainPane.setCenter(canvas);
        FXGraphics2D g2d = new FXGraphics2D(canvas.getGraphicsContext2D());

        new AnimationTimer() {
            long last = -1;
            @Override
            public void handle(long now) {
        if(last == -1)
                    last = now;
        update((now - last) / 1000000000.0);
        last = now;
        draw(g2d);
            }
        }.start();

        canvas.setOnMousePressed(event -> handle(event));

        canvas.setOnMouseDragged(event -> handle(event));

        stage.setScene(new Scene(mainPane));
        stage.setFullScreen(true);
        stage.setTitle("Spotlight");
        stage.show();
        draw(g2d);
java javafx awt javafx-8
1个回答
0
投票

我为感兴趣的人找到的解决方案;

graphics.clip(clippingEllipse.getShape());graphics.setClip(clippingEllipse.getShape());

在draw()方法的末尾添加;graphics.clip(null);

© www.soinside.com 2019 - 2024. All rights reserved.