JavaFx细线颜色失真

时间:2013-11-10 09:38:48

标签: javafx

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

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

1 个答案:

答案 0 :(得分: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的大答案:
What are a line's exact dimensions in JavaFX 2?
How to draw a crisp, opaque hairline in JavaFX 2.2?