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

如果您运行上面的代码,您可能会提到重叠的两条线会创建另一种颜色。是否可以防止这种行为?

4

1 回答 1

2

您可以通过两种方式防止它:
1)使用 setSnapToPixel

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

2) 或者,将行的 startY 和 endY 值定义为一半:

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

对于这些技术细节,我将留给jewelsea 的精彩回答:
JavaFX 2 中线条的确切尺寸是多少?
如何在 JavaFX 2.2 中绘制清晰、不透明的细线?

于 2013-11-10T13:41:08.473 回答