【发布时间】:2015-05-26 00:31:23
【问题描述】:
这是具有不同填充的 4 个按钮的示例,它演示了填充值的四舍五入。
第一个按钮:(3,6,4,6) 第二个按钮:(2.5, 6, 4.5, 6) 第三个按钮:(2,6,5,6) 第四个按钮:(2.4, 6, 4.6, 6)
我预计某种抗锯齿会产生文本被放置在像素之间的错觉。
可以避免这种舍入吗?如果不是,那么填充值加倍的目的是什么?
public class ButtonTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
StackPane root = new StackPane();
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER);
Button firstButton = new Button("Text");
Button secondButton = new Button("Text");
Button thirdButton = new Button("Text");
Button fourthButton = new Button("Text");
Insets firstPadding = new Insets(3, 6, 4, 6);
Insets secondPadding = new Insets(2.5, 6, 4.5, 6);
Insets thirdPadding = new Insets(2, 6, 5, 6);
Insets fourthPadding = new Insets(2.4, 6, 4.6, 6);
firstButton.setPadding(firstPadding);
secondButton.setPadding(secondPadding);
thirdButton.setPadding(thirdPadding);
fourthButton.setPadding(fourthPadding);
firstButton.setTooltip(new Tooltip(firstPadding.toString()));
secondButton.setTooltip(new Tooltip(secondPadding.toString()));
thirdButton.setTooltip(new Tooltip(thirdPadding.toString()));
fourthButton.setTooltip(new Tooltip(fourthPadding.toString()));
hBox.getChildren().add(firstButton);
hBox.getChildren().add(secondButton);
hBox.getChildren().add(thirdButton);
hBox.getChildren().add(fourthButton);
root.getChildren().add(hBox);
Scene scene = new Scene(root, 300, 200);
scene.getStylesheets().add("test.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
CSS:
.button {
-fx-snap-to-pixel: false;
}
更新
奇怪的是:如果按钮沿 Y 轴平移 0.5 或 -0.5,那么按钮会移动但文本保持不变。
第一个按钮不平移,第二个平移-0.5,第三个平移-1。都有相同的填充。
第二个按钮按预期模糊,因为它位于中间像素位置,但似乎由于某种原因文本仅由整数值翻译。也许文本被限制为整数翻译,以确保它始终清晰(可读)并且不会过度模糊。
【问题讨论】:
-
查看Region::snapToPixel 并尝试在您正在使用的容器(例如,您将按钮放入的 HBox)上将 snap to pixel 设置为 false。
-
它改变了字体渲染,但垂直位置保持不变。
-
尝试为每个按钮设置一个线条图形(例如
button.setGraphic(new Line(0, 0, 20, 0));)。如果button.setSnapToPixel(false),这条线将在像素边界上出现锯齿,如果您有部分像素的按钮填充,则锯齿会有所不同。因此,我认为您在更新中的猜测可能是正确的,即无论snapToPixel设置如何,文本总是被捕捉到像素,而图形等其他内容则不然。至少在非视网膜 mac(这是我的文本环境)上的 OS X 10.9 上使用 Java 8u40。 `
标签: javafx