2
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;


public class LinesTest extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        final AnchorPane root = new AnchorPane();

        final Line line1 = new Line(20, 10, 300, 10);
        final Line line2 = new Line(250, 10, 400, 10);
        root.getChildren().addAll(line1, line2);

        stage.setTitle("Lines Test");
        stage.setScene(new Scene(root, 500, 500));
        stage.show();
    }

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

If you run the code above you could mention that two lines overlapping create another color. Is it possible to prevent this behavior?

temper
  • 395
  • 2
  • 9

1 Answers1

2

You can prevent it in 2 ways:
1) Use setSnapToPixel

final AnchorPane root = new AnchorPane();
root.setSnapToPixel(true);

2) Or, define lines' startY and endY values as half:

final Line line1 = new Line(20, 10.5, 300, 10.5);
final Line line2 = new Line(250, 10.5, 400, 10.5);

For the technical details of these I will leave to jewelsea's great answers:
What are a line's exact dimensions in JavaFX 2?
How to draw a crisp, opaque hairline in JavaFX 2.2?

Community
  • 1
  • 1
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • I've made some experiments. What is interesting: 1) on retina display - no problem at all 2) on external monitor, root.setSnapToPixel(true) didn't fix an issue 3) on external monitor, the second approach with fractional coordinates - works! – temper Nov 11 '13 at 15:32