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 :
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.