4

I am wondering how to draw sharp thin lines using JavaFX. I would like my lines to be black, and 1 pixel high. Here is what I have at the moment:

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);
    }
}

Here is what I get :

result

The lines are quite thick, and calling the setStrokeWidth() method with a value < 1 has no effect on the height, but makes the black color fade off. Any idea how to get a 1 pixel high line?

I can achieve it by using rectangles with a height of 1 pixel, but it seems a bit dirty.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    This question should not be marked as duplicate, since the LineBuilder is now deprecated, and thus the original question is not up to date anymore. Moreover, this questions is about JavaFX 8, not JavaFX 2.2. The source code provided here does not even compile in 2.2 –  Sep 16 '15 at 16:44
  • I think this might solve your issue: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/StrokeType.html - using some other value than `OUTSIDE`. I'm just guessing here thought. – NoHarmDan Sep 16 '15 at 22:46

1 Answers1

0

If you use StrokeType.CENTERED, and start the x/y values on a half unit, then the lines appear to be hairlines to me.

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.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 unit numbers in JavaFX are for the corners of the pixels, so specifying location + 0.5 places the line in the middle of said pixel.

clstrfsck
  • 14,715
  • 4
  • 44
  • 59
  • No need to guess here. Just have a look at the javadoc of the Node class for example. Look for Coordinate System. – mipa Sep 17 '15 at 07:05