java 如何使用 Javafx Canvas 绘制 1 像素线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27846659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw an 1 pixel line using Javafx Canvas?
提问by Chocksmith
I've been googling and searching, found some some related questions/posts but none of the address my problem.
我一直在谷歌搜索和搜索,发现了一些相关的问题/帖子,但没有一个解决我的问题。
I am drawing lines directly on canvas (JavaFX) using:
我使用以下方法直接在画布(JavaFX)上绘制线条:
gc.setStroke(color);
gc.setLineWidth(lineWidth);
gc.strokeLine(startX, startY, endX, endY);
I want 1 pixel width lines. So I set lineWidth=1.
I get this:
我想要 1 像素宽度的线。所以我设置 lineWidth=1。我明白了:
Note that the lines are blurred. It is not 1 pixel. I've tried to set lineWidth to 0.1 or 0.01, etc. It does not change the result.
请注意,线条是模糊的。它不是 1 像素。我尝试将 lineWidth 设置为 0.1 或 0.01 等。它不会改变结果。
By the way... I do not understand why this parameter is a double. I read somewhere that it has to do with DPI. But I do not understand what is the unit and how it is converted to pixels. Oracle's documentation does not help. (or I did not find the one that helps)
顺便说一句......我不明白为什么这个参数是双精度的。我在某处读到它与 DPI 有关。但我不明白单位是什么以及它如何转换为像素。Oracle 的文档没有帮助。(或者我没有找到有帮助的那个)
I'd like to get this instead:
我想得到这个:
This was implemented in another platform. Note that lines are sharp and have just one 1 pixel.
这是在另一个平台上实现的。请注意,线条很清晰,只有一个 1 像素。
回答by James_D
Imagine each pixel as a (small) rectangle (instead of a point). The integer coordinates are the boundaries between pixels; so a (horizontal or vertical) line with integer coordinates falls "between pixels". This is rendered via antialising, approximating half of the line on one pixel and half on the other. Moving the line 0.5 pixels left or right moves it to the center of the pixel, getting around the issue.
把每个像素想象成一个(小)矩形(而不是一个点)。整数坐标是像素之间的边界;所以具有整数坐标的(水平或垂直)线落在“像素之间”。这是通过抗锯齿呈现的,在一个像素上近似一半的线,在另一个像素上近似一半。将线向左或向右移动 0.5 个像素将其移动到像素的中心,从而解决问题。
Here's a sample:
这是一个示例:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SharpCanvasTest extends Application {
@Override
public void start(Stage primaryStage) {
Canvas sharpCanvas = createCanvasGrid(600, 300, true);
Canvas blurryCanvas = createCanvasGrid(600, 300, false);
VBox root = new VBox(5, sharpCanvas, blurryCanvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
private Canvas createCanvasGrid(int width, int height, boolean sharp) {
Canvas canvas = new Canvas(width, height);
GraphicsContext gc = canvas.getGraphicsContext2D() ;
gc.setLineWidth(1.0);
for (int x = 0; x < width; x+=10) {
double x1 ;
if (sharp) {
x1 = x + 0.5 ;
} else {
x1 = x ;
}
gc.moveTo(x1, 0);
gc.lineTo(x1, height);
gc.stroke();
}
for (int y = 0; y < height; y+=10) {
double y1 ;
if (sharp) {
y1 = y + 0.5 ;
} else {
y1 = y ;
}
gc.moveTo(0, y1);
gc.lineTo(width, y1);
gc.stroke();
}
return canvas ;
}
public static void main(String[] args) {
launch(args);
}
}
And the results:
结果:
回答by Fausto Odilon
Use coordinates in this notation x.5.
在此符号中使用坐标 x.5。
Look my example:
看我的例子:
gc.setFill(Color.BLACK);
gc.setLineWidth(1.0);
gc.strokeRect(50, 100, 25.0, 25.0);
gc.strokeRect(100.5, 100.5, 25.0, 25.0);
You will get two squares, the second sharp.
你会得到两个正方形,第二个是锐角。
Reference: https://dlsc.com/2014/04/10/javafx-tip-2-sharp-drawing-with-canvas-api/
参考:https: //dlsc.com/2014/04/10/javafx-tip-2-sharp-drawing-with-canvas-api/