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

Michał Błaszak mibl at stibosystems.com
Tue Jan 8 20:24:38 UTC 2019

Hi Phil,

Thank you for the very comprehensive answer.
I was suspecting this must be something with trying to fit into the device DPI but the hint about -Dsun.java2d.uiScale=1 helps when you really want to hit the pixel.

With your permission I’d like to share your answer on StackOverflow so others could read it too.

Best regards

Michał Błaszak
Section Manager, Research & Development

From: Phil Race <philip.race at oracle.com>
Sent: wtorek, 8 stycznia 2019 20:48
To: Michał Błaszak <mibl at stibosystems.com>; 2d-dev at openjdk.java.net
Subject: Re: [OpenJDK 2D-Dev] Pixel-perfect drawing on JPanel

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 setting.

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:
[cid:image002.jpg at 01D4A794.F8653E10]

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/f19c04b4/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 16063 bytes
Desc: image002.jpg
URL: <https://mail.openjdk.java.net/pipermail/2d-dev/attachments/20190108/f19c04b4/image002-0001.jpg>

More information about the 2d-dev mailing list