javafx - 捕捉像素并避免模糊的通用机制

标签 javafx blur

我正在创建一个包含两个彼此重叠的矩形的健康栏。内部显示当前健康状况,外部显示总体健康状况。

问题

生命值栏模糊。

到目前为止我所得到的

我读过有关 e 的内容。 G。 having to add 0.5 ,但这只是部分解决了问题。

屏幕截图,上图没有 0.5,下图添加了 0.5。

enter image description here

代码如下:

private class HealthBar extends Pane {

    Rectangle outerHealthRect;
    Rectangle innerHealthRect;

    public HealthBar() {

        double height = 10;

        double outerWidth = 60;
        double innerWidth = 40;

        // using 0.5, otherwise it would be blurry
        double x=snap( 0.0);
        double y=snap( 0.0);

        outerHealthRect = new Rectangle( x, y, outerWidth, height);
        outerHealthRect.setStroke(Color.BLACK);
        outerHealthRect.setFill(Color.WHITE);

        innerHealthRect = new Rectangle( x, y, innerWidth, height);
        innerHealthRect.setStroke(Color.TRANSPARENT);
        innerHealthRect.setFill(Color.LIMEGREEN);

        getChildren().addAll( outerHealthRect);
        getChildren().addAll( innerHealthRect);

    }

    public void setValue( double value) {
        innerHealthRect.setWidth( snap( outerHealthRect.getWidth() * value));
    }

    private double snap( double value) {
        return (int) value + 0.5;
    }
}

在我出于调试目的应用缩放后,很明显透明笔划不被视为与带有颜色的笔划相同宽度的透明线:

enter image description here

问题

  • 是否有一个选项可以捕捉到像素,即。 e.一般情况下都有清晰的像素,而不必用 0.5 来搞乱?
  • 如何使透明笔划与外部笔划的宽度相同?

我可以尝试使用 Canvas 的不同方法,但这些是影响您设置 JavaFX 应用程序的方式的常见问题。

非常感谢。

最佳答案

不要突然填充。我链接到的问题解释了线条是在像素上绘制的(通过中间 - 占用整个像素),这意味着 +0.5,因为坐标位于像素之间。

填充需要在整个像素上绘制到它们之间的空间。

enter image description here

public HealthBar() {

    double height = 10;

    double outerWidth = 60;
    double innerWidth = 40;

    double x=10,y=10;//away from pane edge just to see better

    outerHealthRect = new Rectangle( snap(x), snap(y), outerWidth, height);
    outerHealthRect.setStroke(Color.BLACK);
    outerHealthRect.setFill(Color.WHITE);

    //just move green bar right a bit to see edges
    //and move it down 1 pix so it's not over the black edge
    //this means height is -1 as well
    innerHealthRect = new Rectangle( x+11, y+1, innerWidth, height-1);
    innerHealthRect.setFill(Color.LIMEGREEN);
    //no need to draw transparent anything

    getChildren().addAll( outerHealthRect);
    getChildren().addAll( innerHealthRect);

}

关于javafx - 捕捉像素并避免模糊的通用机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163331/

相关文章:

javafx-2 - Javafx:确定节点的字体大小

macos - javafxpackager 在 Mac OS X 上错误地定位应用程序文件夹

java - HBox 不可见

java - 使用 Platform.runLater 更改非 javafx 线程中的按钮文本

android - 在 Android 上的 Cordova 应用程序中的弹出窗口后面模糊

ios - 全屏模糊 View

css - 模糊图像然后恢复正常状态

html - CSS Filter Multiple URL - 同时获得模糊和灰度

WPF:如果我在父项上使用投影效果,为什么文本和元素会模糊

java - 我们可以在 JavaFX 中实现我们自己的 Material 吗?