JavaFX GraphicsContext倒圆
我一直在试图在网上找到一种方法来做这件事,但我什么也找不到 我想用JavaFX GraphicsContext画一个“倒圆”。 这些图片显示了我想要的 原件: 使用“倒圆”(我想画的): 在一个图像编辑器中,我可以擦除新图层中的圆形区域。。。在GraphicsContext中,我看不到任何函数可以做到这一点 我需要能够选择这个“圆”的中心点和半径JavaFX GraphicsContext倒圆,java,javafx,graphics,graphicscontext,Java,Javafx,Graphics,Graphicscontext,我一直在试图在网上找到一种方法来做这件事,但我什么也找不到 我想用JavaFX GraphicsContext画一个“倒圆”。 这些图片显示了我想要的 原件: 使用“倒圆”(我想画的): 在一个图像编辑器中,我可以擦除新图层中的圆形区域。。。在GraphicsContext中,我看不到任何函数可以做到这一点 我需要能够选择这个“圆”的中心点和半径 谢谢 我不太确定是否直接使用GraphicsContext,但您可以使用 绘制图像时,构造一个圆形路径并将其用作剪辑: @Override pub
谢谢 我不太确定是否直接使用
GraphicsContext
,但您可以使用
绘制图像时,构造一个圆形路径并将其用作剪辑:
@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();
}
我会用这个,但不幸的是我在做一个游戏,所以我用了一个Canvas@MCMasteryFabian的方式可能是首选,但是
GraphicsContext
也支持。如果你没有解决方案,你可以看看。这看起来是正确的,似乎它应该工作。。。我会找出我做错了什么
@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();
}