Skip to content

Commit 09f3890

Browse files
committedMar 21, 2024
8297449: Update JInternalFrame Metal Border code
Backport-of: 09629570f5d064dc2a5cd670de8d648156ac3991
1 parent aa2cdb3 commit 09f3890

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed
 

‎src/java.desktop/share/classes/javax/swing/plaf/metal/MetalBorders.java

+33-38
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
import sun.swing.StringUIClientPropertyKey;
6666
import sun.swing.SwingUtilities2;
6767

68+
import static sun.java2d.pipe.Region.clipRound;
69+
6870
/**
6971
* Factory object that can vend Borders appropriate for the metal L & F.
7072
* @author Steve Wilson
@@ -247,19 +249,6 @@ public static class InternalFrameBorder extends AbstractBorder implements UIReso
247249
*/
248250
public InternalFrameBorder() {}
249251

250-
/**
251-
* Rounds a double to the nearest integer. It rounds 0.5 down,
252-
* for example 1.5 is rounded to 1.0.
253-
*
254-
* @param d number to be rounded
255-
* @return the rounded value
256-
*/
257-
private static int roundHalfDown(double d) {
258-
double decP = (Math.ceil(d) - d);
259-
return (int)((decP == 0.5) ? Math.floor(d) : Math.round(d));
260-
}
261-
262-
263252
public void paintBorder(Component c, Graphics g, int x, int y,
264253
int w, int h) {
265254
Color background;
@@ -276,41 +265,50 @@ public void paintBorder(Component c, Graphics g, int x, int y,
276265
shadow = MetalLookAndFeel.getControlInfo();
277266
}
278267

279-
Graphics2D g2d = (Graphics2D) g;
280-
AffineTransform at = g2d.getTransform();
281-
Stroke oldStk = g2d.getStroke();
282-
Color oldColor = g2d.getColor();
268+
AffineTransform at = null;
269+
Stroke oldStk = null;
270+
boolean resetTransform = false;
283271
int stkWidth = 1;
272+
double scaleFactor = 1;
273+
274+
if (g instanceof Graphics2D g2d) {
275+
at = g2d.getTransform();
276+
scaleFactor = at.getScaleX();
277+
oldStk = g2d.getStroke();
278+
279+
// if m01 or m10 is non-zero, then there is a rotation or shear
280+
// skip resetting the transform
281+
resetTransform = ((at.getShearX() == 0) && (at.getShearY() == 0));
284282

285-
// if m01 or m10 is non-zero, then there is a rotation or shear
286-
// skip resetting the transform
287-
boolean resetTransform = ((at.getShearX() == 0) && (at.getShearY() == 0));
283+
if (resetTransform) {
284+
g2d.setTransform(new AffineTransform());
285+
stkWidth = clipRound(Math.min(at.getScaleX(), at.getScaleY()));
286+
g2d.setStroke(new BasicStroke((float) stkWidth));
287+
}
288+
}
288289

289290
int xtranslation;
290291
int ytranslation;
291292
int width;
292293
int height;
293294

294295
if (resetTransform) {
295-
g2d.setTransform(new AffineTransform());
296-
stkWidth = roundHalfDown(Math.min(at.getScaleX(), at.getScaleY()));
297-
298296
double xx = at.getScaleX() * x + at.getTranslateX();
299297
double yy = at.getScaleY() * y + at.getTranslateY();
300-
xtranslation = roundHalfDown(xx);
301-
ytranslation = roundHalfDown(yy);
302-
width = roundHalfDown(at.getScaleX() * w + xx) - xtranslation;
303-
height = roundHalfDown(at.getScaleY() * h + yy) - ytranslation;
298+
xtranslation = clipRound(xx);
299+
ytranslation = clipRound(yy);
300+
width = clipRound(at.getScaleX() * w + xx) - xtranslation;
301+
height = clipRound(at.getScaleY() * h + yy) - ytranslation;
304302
} else {
305-
width = w;
306-
height = h;
307303
xtranslation = x;
308304
ytranslation = y;
305+
width = w;
306+
height = h;
309307
}
310-
g2d.translate(xtranslation, ytranslation);
308+
g.translate(xtranslation, ytranslation);
311309

312310
// scaled border
313-
int thickness = (int) Math.ceil(4 * at.getScaleX());
311+
int thickness = (int) Math.ceil(4 * scaleFactor);
314312

315313
g.setColor(background);
316314
// Draw the bulk of the border
@@ -319,17 +317,14 @@ public void paintBorder(Component c, Graphics g, int x, int y,
319317
}
320318

321319
if (c instanceof JInternalFrame && ((JInternalFrame)c).isResizable()) {
322-
// set new stroke to draw shadow and highlight lines
323-
g2d.setStroke(new BasicStroke((float) stkWidth));
324-
325320
// midpoint at which highlight & shadow lines
326321
// are positioned on the border
327322
int midPoint = thickness / 2;
328-
int offset = ((at.getScaleX() - stkWidth) >= 0 && stkWidth % 2 != 0) ? 1 : 0;
323+
int offset = (((scaleFactor - stkWidth) >= 0) && ((stkWidth % 2) != 0)) ? 1 : 0;
329324
int loc1 = thickness % 2 == 0 ? midPoint + stkWidth / 2 - stkWidth : midPoint;
330325
int loc2 = thickness % 2 == 0 ? midPoint + stkWidth / 2 : midPoint + stkWidth;
331326
// scaled corner
332-
int corner = (int) Math.round(CORNER * at.getScaleX());
327+
int corner = (int) Math.round(CORNER * scaleFactor);
333328

334329
// Draw the Long highlight lines
335330
g.setColor(highlight);
@@ -351,9 +346,9 @@ public void paintBorder(Component c, Graphics g, int x, int y,
351346
}
352347

353348
// restore previous transform
354-
g2d.translate(-xtranslation, -ytranslation);
349+
g.translate(-xtranslation, -ytranslation);
355350
if (resetTransform) {
356-
g2d.setColor(oldColor);
351+
Graphics2D g2d = (Graphics2D) g;
357352
g2d.setTransform(at);
358353
g2d.setStroke(oldStk);
359354
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Mar 21, 2024

@openjdk-notifier[bot]
Please sign in to comment.