JavaFX 图形 "blurred"或消除锯齿? (没有使用效果)

标签 java javafx

我正在创建一些形状,尽管没有应用任何效果,但一切似乎都很模糊,就像抗锯齿一样。

例如,在黑色背景上绘制的一条宽度为 1 像素的白线呈现为灰色!将宽度更改为 2px 会导致白色,但定义不明确。

搜索时,返回形状上的方法 setSmooth(false),但调用它没有任何区别。

我应该在 StageScene 上更改或禁用什么?

最佳答案

参见 Shape文档:

Most nodes tend to have only integer translations applied to them and quite often they are defined using integer coordinates as well. For this common case, fills of shapes with straight line edges tend to be crisp since they line up with the cracks between pixels that fall on integer device coordinates and thus tend to naturally cover entire pixels.

On the other hand, stroking those same shapes can often lead to fuzzy outlines because the default stroking attributes specify both that the default stroke width is 1.0 coordinates which often maps to exactly 1 device pixel and also that the stroke should straddle the border of the shape, falling half on either side of the border. Since the borders in many common shapes tend to fall directly on integer coordinates and those integer coordinates often map precisely to integer device locations, the borders tend to result in 50% coverage over the pixel rows and columns on either side of the border of the shape rather than 100% coverage on one or the other. Thus, fills may typically be crisp, but strokes are often fuzzy.

Two common solutions to avoid these fuzzy outlines are to use wider strokes that cover more pixels completely - typically a stroke width of 2.0 will achieve this if there are no scale transforms in effect - or to specify either the StrokeType.INSIDE or StrokeType.OUTSIDE stroke styles - which will bias the default single unit stroke onto one of the full pixel rows or columns just inside or outside the border of the shape.

另请参阅 Node 的文档:

At the device pixel level, integer coordinates map onto the corners and cracks between the pixels and the centers of the pixels appear at the midpoints between integer pixel locations. Because all coordinate values are specified with floating point numbers, coordinates can precisely point to these corners (when the floating point values have exact integer values) or to any location on the pixel. For example, a coordinate of (0.5, 0.5) would point to the center of the upper left pixel on the Stage. Similarly, a rectangle at (0, 0) with dimensions of 10 by 10 would span from the upper left corner of the upper left pixel on the Stage to the lower right corner of the 10th pixel on the 10th scanline. The pixel center of the last pixel inside that rectangle would be at the coordinates (9.5, 9.5).

因此,当您的笔画宽度奇数时,您可以选择干净的线条:

  1. 使用 StrokeType.INSIDE 或 StrokeType.OUTSIDE 笔触样式。
  2. 将形状的坐标偏移 0.5 个像素,使笔划在线条上对齐,而不是在线条之间的裂缝上对齐。
  3. 只需使用下一个偶数作为笔画宽度,例如1 => 2、3 => 4 等

至于为什么setSmooth(false)不起作用,我也不清楚,我的猜测是它所指的抗锯齿独立于当笔划集中在像素之间有裂缝,但我不知道为什么会这样。

关于JavaFX 图形 "blurred"或消除锯齿? (没有使用效果),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9779693/

相关文章:

java - 如何在javafx中跨多个窗口创建 "Change background colour option"

java - 玩家的类没有正确序列化

java - 我的舞台在 while 循环中没有响应

java - 将图像上的两个点映射到另一个点

java - 在 hibernate 和 glassfish 3.0.1 中使用 log4j

java - (JDK 11) 通过依赖库的反射运行 JAR 存档

JavaFX 拖放到 GridPane?

java - ws 消息 validator 中的多个 xsd 架构 - 为什么在 schemaCollection 中找不到资源,但在 xsdSchema 中找到资源?

java - wicket 项目中的 @component spring 注释?

java - 如何使用另一个实例列表在java中有效地创建实例列表