这可能是有史以来最简单的问题。我有一个像这样的JavaFx Canvas设置:
Canvas canvas = new Canvas(300, 300);
GraphicsContext context = canvas.getGraphicsContext2D();
// make a big rectangle
context.setFill(Color.BLUE);
context.fillRect(50, 50, 200, 200);
// clip
context.beginPath();
context.rect(100, 100, 100, 100);
context.closePath();
context.clip();
// so now this draws a clipped smaller rectangle
context.setFill(Color.RED);
context.fillRect(50, 50, 200, 200);
context.removeClip(); // ???
// remove clip so this white rectangle is shown
context.setStroke(Color.WHITE);
context.setLineWidth(3);
context.strokeRect(75, 75, 150, 150);我尝试了GraphicsContext#restore() (除了剪裁和创建矩形路径之外,它恢复所有内容,从0开始,使用画布的大小,然后再次调用clip() )。
如何从GraphicsContext中删除裁剪
发布于 2016-04-03 18:20:57
在JavaFX中的剪裁行为至少是很难的。“去掉你说的剪裁?”这个怎么样。
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
Canvas canvas = new Canvas();
canvas.setHeight(400);
canvas.setWidth(400);
GraphicsContext graphics = canvas.getGraphicsContext2D();
//graphics.save();
graphics.beginPath();
graphics.rect(0,0,200,200);
graphics.clip();
graphics.setFill(Color.RED);
graphics.fillOval(100, 100, 200, 200);
//graphics.restore();
graphics.beginPath();
graphics.rect(200,200,200,200);
graphics.clip();
graphics.setFill(Color.BLUE);
graphics.fillOval(100, 100, 200, 200);
root.getChildren().add(canvas);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}当我在电脑上这样做的时候,我只会得到一个蓝色的圆圈。没有剪裁。有人可能会期待一个红四分之一的圆和一个蓝色四分之一的圆。不是的。注释保存和恢复调用,它的行为与预期的一样。
https://stackoverflow.com/questions/36388042
复制相似问题