JavaFX graphics "blurred" or anti-aliased? (No effects used)


I'm creating some shapes to learn and everything seems to be blurred, like some anti-aliasing, and I´m not using any effects (I don´t get these lessons yet, lol).

For exemple, I draw a white line in a black backgroud with 1 pixel width, but it´s get grey! If I change the width to 2px it gets white, but still not really defined.

When I was "googleing" everything I found was a bout a method setSmooth(false) on shapes, but it does not make any difference. Is ther anything I should change or disable on Stage or Scene I don´t know?

Any help is appreciated. Thanks in advice...

Sorry about my english still need improvements too lol... but it´s not my natural language.

3/20/2012 12:03:32 AM

Accepted Answer

See the Shape documentation:

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.

And see also the documentation of 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).

So your options for clean lines when you have an odd stroke width are:

  1. Use a StrokeType.INSIDE or StrokeType.OUTSIDE stroke style.
  2. Offset the co-ordinates of shapes by 0.5 of a pixel so that the strokes line up on the lines rather than the cracks between lines.
  3. Just use the next even number up as the stroke width, e.g. 1 => 2, 3 => 4, etc.

As to why setSmooth(false) does not work, I don't know exactly, my guess is that the antialiasing it refers to is independent of the antialiasing styles performed when strokes are centered on the cracks between pixels, but I would not know why that would be.

3/20/2012 8:26:05 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow