簡體   English   中英

JavaFX GraphicsContext倒置圓

[英]JavaFX GraphicsContext Inverted Circle

我一直試圖在網上找到一種方法,但我找不到任何東西。

我想用JavaFX GraphicsContext繪制一個“反向圓”。 這些圖像展示了我想要的東西。

原版的: 原始圖片

用“倒圓”(我想畫的): 倒圓圈

在圖像編輯器中,我可以刪除新圖層中的圓形區域...我沒有看到任何可以在GraphicsContext中執行此操作的函數。

我需要能夠選擇這個“圓圈”的中心點和半徑。

謝謝!

我不太確定直接使用GraphicsContext ,但你可以使用Blending來完成

ImageView image; // Your image
Circle mask = new Circle();

Group g = new Group();
g.setBlendMode(BlendMode.SRC_ATOP);
g.getChildren.add(image);
g.getChildren.add(mask);

構建圓形路徑並在繪制圖像時將其用作剪輯:

@Override
public void start(Stage primaryStage) {
    Image image = new Image("https://i.stack.imgur.com/zEoW1.jpg");
    double w = image.getWidth();
    double h = image.getHeight();

    Canvas canvas = new Canvas(w, h);
    GraphicsContext gc = canvas.getGraphicsContext2D();

    // draw background
    gc.setFill(Color.BLACK);
    gc.fillRect(0, 0, w, h);

    double r = Math.min(h, w) * 2 / 5;
    double cx = w / 2;
    double cy = h / 2;

    // create circular path
    gc.beginPath();
    gc.moveTo(cx - r, cy); // to first point on the circle
    gc.arc(cx, cy, r, r, 180, 360);
    gc.closePath();

    gc.clip();

    gc.drawImage(image, 0, 0);

    StackPane root = new StackPane();
    root.getChildren().add(canvas);

    Scene scene = new Scene(root);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM