[OpenJDK 2D-Dev] Pixel-perfect drawing on JPanel

Phil Race philip.race at oracle.com
Tue Jan 8 19:47:31 UTC 2019

This is a known problem of sorts. It is called the Raster Problem.

JDK 9 and later support windows hi-dpi mode so you now have a scaled JPanel.
Something like 1.33 scale, the exact value depending on your windows DPI 

The lines are all drawn independently, and the "y" is mapped to the 
closest device pixel
and the height of all of these rather than being the impossible "1.33" 
device pixels wide
is 1 device pixel wide. So you end up with gaps.

The fillRect case (so far as I can see)  handles this better because it
will occasionally fill 2 pixels wide rather than leaving a gap.

I am not sure why (what should be) the wide line code, filling spans, 
isn't doing
that too. So there is likely a bug here, but a fix for that can't solve 
the raster problem.
At best you'd get what is happening with fillRect.

Turning on anti-aliased rendering may be the easiest option for you.
You'll then get blended rendering of course.

Or you will need to be more conscious of the effects of fractional scaling
on lo-res devices.

Another workaround is


On 12/20/18 3:11 AM, Michał Błaszak wrote:
> Hi All,
> I hope this is a known problem but just in case it’s not.
> Trying to make an app drawing something on JPanel I noticed some 
> annoying inaccuracies.
> The attached piece of code is supposed to draw a series of lines one 
> below the other (swapping colors are to demonstrate what's happening).
> The result is presented in the following picture:
> That means that some pixels are unreachable.
> I tested it on several jdk releases. Results are:
> OpenJDK Runtime Environment (build 1.8.0_40-b25): Works correctly
> OpenJDK Runtime Environment (build 9+181): Wrong behavior
> OpenJDK Runtime Environment 18.3 (build 10.0.2+13): Wrong behavior
> OpenJDK Runtime Environment 18.9 (build 11.0.1+13): Wrong behavior
> The same inaccuracies are observable when using drawRect() or fillRect().
> I wanted to report is a bug but I do not know how to do it.
> Thank you for looking at it.
> Best regards
> *Michał Błaszak*
> Section Manager, Research & Development

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/2d-dev/attachments/20190108/66dd9796/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.jpg
Type: image/jpeg
Size: 19047 bytes
Desc: not available
URL: <https://mail.openjdk.java.net/pipermail/2d-dev/attachments/20190108/66dd9796/image003-0001.jpg>

More information about the 2d-dev mailing list