RFR: 8349188: LineBorder does not scale correctly [v3]

Alexey Ivanov aivanov at openjdk.org
Sat Jun 28 20:11:41 UTC 2025


On Sat, 28 Jun 2025 04:28:23 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> src/java.desktop/share/classes/javax/swing/border/LineBorder.java line 166:
>> 
>>> 164:             Shape inner;
>>> 165: 
>>> 166:             int offs = clipRound(this.thickness * scaleFactor);
>> 
>> Please double-check whether you need to use `Region.clipScale()` instead.
>> 
>> I actually do not remember when to use one over the other. Maybe if you find a review request for the patch where these methods were added, you can confirm which one should be used. I checked the current source, and it seems that we randomly use one or the other, which seems incorrect.
>
> it might be possible we should use one for the left/top part and another for the right/bottom so we will not create a gaps.

> Please double-check whether you need to use Region.clipScale() instead.

@mrserb We use `Region.clipRound` for converting coordinates in `SwingUtilities3.paintBorder`:

https://github.com/openjdk/jdk/blob/a23de2ec090628b52532ee5d9bd4364a97499f5b/src/java.desktop/share/classes/com/sun/java/swing/SwingUtilities3.java#L231-L236

These formulas were introduced in #10681 ([JDK-8282958](https://bugs.openjdk.org/browse/JDK-8282958)); and after refactoring #11571 ([JDK-8294680](https://bugs.openjdk.org/browse/JDK-8294680)), the new formulas resolved [JDK-8294921](https://bugs.openjdk.org/browse/JDK-8294921) after #7449 ([JDK-8279614](https://bugs.openjdk.org/browse/JDK-8279614)) where there was a gap between the component edge and its border. These formulas specifically addressed the situation with gaps and were developed while we worked on making `LineBorder` and `EtchedBorder` render better at fractional scales.

So, using `clipRound` seems good enough. Initially, [I suggested](https://bugs.openjdk.org/browse/JDK-8349188?focusedId=14758933&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14758933) using `(int) (this.thickness * scaleFactor)`, but `clipRound` gives better results.

> I actually do not remember when to use one over the other. Maybe if you find a review request for the patch where these methods were added, you can confirm which one should be used.

I couldn't find anything explaining where `clipScale` is better than `clipRound`. The discussion for [JDK-8000629](https://bugs.openjdk.org/browse/JDK-8000629) started in [March 2013](https://mail.openjdk.org/pipermail/awt-dev/2013-March/004486.html) and continued in [April 2013](https://mail.openjdk.org/pipermail/awt-dev/2013-April/004577.html).

> I checked the current source, and it seems that we randomly use one or the other, which seems incorrect.

The javadoc for neither `clipScale` nor `clipRound` provides an example where each method is best suitable. So, it's not that surprising either is used randomly…

It seems to me, that both [`clipScale`](https://github.com/openjdk/jdk/blob/a23de2ec090628b52532ee5d9bd4364a97499f5b/src/java.desktop/share/classes/sun/java2d/pipe/Region.java#L159) and [`clipRound`](https://github.com/openjdk/jdk/blob/a23de2ec090628b52532ee5d9bd4364a97499f5b/src/java.desktop/share/classes/sun/java2d/pipe/Region.java#L140) yield the same result. The former uses `Math.round`, whereas the latter uses `Math.ceil` after subtracting `0.5`.

> it might be possible we should use one for the left/top part and another for the right/bottom so we will not create a gaps.

Perhaps… However, I'm pretty sure gaps are still possible whatever method we choose because fractional pixels don't fit nicely into the pixel grid.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/26025#discussion_r2173510928


More information about the client-libs-dev mailing list