JavaFX: draw sharp thin lines

I want to know how to use JavaFX to draw sharp thin lines I think my line is black, 1 pixel high This is what I have now:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            root.setSnapToPixel(true);
            Scene scene = new Scene(root,400,400);

            Line line = new Line();
            Line line2 = new Line();

            line.setStartX(0.0f);
            line.setEndX(100f);
            line.setStartY(30f);
            line.setEndY(30f);
            line.setstrokeWidth(1f);
            line.setstrokeType(strokeType.OUTSIDE);
            line.setstroke(Color.BLACK);

            line2.setStartX(50.0f);
            line2.setEndX(200f);
            line2.setStartY(100f);
            line2.setEndY(100f);
            line2.setstrokeWidth(1f);
            line2.setstrokeType(strokeType.OUTSIDE);
            line2.setstroke(Color.BLACK);

            root.getChildren().addAll(line,line2);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

This is what I got:

These lines are very thick, and the setstrokewidth () method is called with the value < 0 1 has no effect on height, but will make the black gradient Know how to get 1 pixel high line? I can do this by using a rectangle with a height of 1 pixel, but it looks a little dirty

Solution

If you use stroketype Centered and start the X / y value on half a unit, then these lines don't look good to me

public void start(Stage primaryStage) {
    try {
        BorderPane root = new BorderPane();
        root.setSnapToPixel(true);
        Scene scene = new Scene(root,400);

        Line line = new Line();
        Line line2 = new Line();

        line.setStartX(0.5);
        line.setEndX(100.5);
        line.setStartY(30.5);
        line.setEndY(30.5);
        line.setstrokeWidth(1.0);
        line.setstrokeType(strokeType.CENTERED);
        line.setstroke(Color.BLACK);

        line2.setStartX(50.5);
        line2.setEndX(200.5);
        line2.setStartY(100.5);
        line2.setEndY(100.5);
        line2.setstrokeWidth(1.0);
        line2.setstrokeType(strokeType.CENTERED);
        line2.setstroke(Color.BLACK);

        root.getChildren().addAll(line,line2);

        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

My guess is that the number of units in JavaFX is for the corner of the pixel, so specifying the position of 0.5 will put the line in the middle of the pixel

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>