Ma *_*Sch 5 javafx canvas erase
我是JavaFX的新手,我试图在画布上绘制一些东西.
首先,我将线条颜色设置为黑色并画一条线.
canvas.getGraphicsContext2D().setStroke(Color.BLACK);
canvas.getGraphicsContext2D().strokeLine(20,20,100,100);
Run Code Online (Sandbox Code Playgroud)
在此之后,我试图通过在此行上画一条白线来擦除这条线:
canvas.getGraphicsContext2D().setStroke(Color.WHITE);
canvas.getGraphicsContext2D().strokeLine(20,20,100,100);
Run Code Online (Sandbox Code Playgroud)
但是画布上会留下一些灰色像素.这是什么原因,我该如何防止这种情况?
这就是我创建场景的方式
Pane root = new Pane();
canvas = new Canvas(200, 200);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.strokeLine(20,20,100,100);
scene = new Scene(root, 200, 200);
this.setColor(Color.WHITE);
root.getChildren().add(canvas);
Run Code Online (Sandbox Code Playgroud)
谢谢,马丁
此问题与抗锯齿有关。如果某个点的坐标为非整数,则其绘制将被分成多个像素并进行部分填充。像这样(放大):
因此,如果您绘制与水平或垂直线不同的东西,您就会得到这种效果。如果您想解决此问题,请使用逐像素绘图:
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
Canvas canvas = new Canvas();
canvas.setWidth(200);
canvas.setHeight(200);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(4);
gc.setStroke(Color.BLACK);
drawLine(gc, 20, 20, 180, 180);
gc.setStroke(Color.WHITE);
drawLine(gc, 20, 20, 180, 180);
Pane pane = new Pane(canvas);
primaryStage.setScene(new Scene(pane));
primaryStage.show();
}
private void drawLine(GraphicsContext gc, double x1, double y1, double x2, double y2) {
double lineWidth = gc.getLineWidth();
Color color = (Color) gc.getStroke();
PixelWriter pw = gc.getPixelWriter();
double k = (y2 - y1) / (x2 - x1);
double b = (x1 * y2 - x2 * y1) / (x1 - x2);
int val;
for (int x = (int) x1; x <= (int) x2; x++) {
val = (int) (k * x + b);
for (int y = (int) (val - (lineWidth-1) / 2); y < val + lineWidth / 2; y++) {
pw.setColor(x, y, color);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但是太复杂了。您将需要为不同的形状创建很多方法。最好的解决方案是进行清洁和更改后重新绘制。
| 归档时间: |
|
| 查看次数: |
578 次 |
| 最近记录: |