8297449: Update JInternalFrame Metal Border code

Reviewed-by: aivanov, kizune
This commit is contained in:
Harshitha Onkar 2022-12-01 18:57:50 +00:00
parent 9f94cbec51
commit 09629570f5

View File

@ -65,6 +65,8 @@ import javax.swing.text.JTextComponent;
import sun.swing.StringUIClientPropertyKey; import sun.swing.StringUIClientPropertyKey;
import sun.swing.SwingUtilities2; import sun.swing.SwingUtilities2;
import static sun.java2d.pipe.Region.clipRound;
/** /**
* Factory object that can vend Borders appropriate for the metal L & F. * Factory object that can vend Borders appropriate for the metal L & F.
* @author Steve Wilson * @author Steve Wilson
@ -246,19 +248,6 @@ public class MetalBorders {
*/ */
public InternalFrameBorder() {} public InternalFrameBorder() {}
/**
* Rounds a double to the nearest integer. It rounds 0.5 down,
* for example 1.5 is rounded to 1.0.
*
* @param d number to be rounded
* @return the rounded value
*/
private static int roundHalfDown(double d) {
double decP = (Math.ceil(d) - d);
return (int)((decP == 0.5) ? Math.floor(d) : Math.round(d));
}
public void paintBorder(Component c, Graphics g, int x, int y, public void paintBorder(Component c, Graphics g, int x, int y,
int w, int h) { int w, int h) {
Color background; Color background;
@ -275,15 +264,27 @@ public class MetalBorders {
shadow = MetalLookAndFeel.getControlInfo(); shadow = MetalLookAndFeel.getControlInfo();
} }
Graphics2D g2d = (Graphics2D) g; AffineTransform at = null;
AffineTransform at = g2d.getTransform(); Stroke oldStk = null;
Stroke oldStk = g2d.getStroke(); boolean resetTransform = false;
Color oldColor = g2d.getColor();
int stkWidth = 1; int stkWidth = 1;
double scaleFactor = 1;
// if m01 or m10 is non-zero, then there is a rotation or shear if (g instanceof Graphics2D g2d) {
// skip resetting the transform at = g2d.getTransform();
boolean resetTransform = ((at.getShearX() == 0) && (at.getShearY() == 0)); scaleFactor = at.getScaleX();
oldStk = g2d.getStroke();
// if m01 or m10 is non-zero, then there is a rotation or shear
// skip resetting the transform
resetTransform = ((at.getShearX() == 0) && (at.getShearY() == 0));
if (resetTransform) {
g2d.setTransform(new AffineTransform());
stkWidth = clipRound(Math.min(at.getScaleX(), at.getScaleY()));
g2d.setStroke(new BasicStroke((float) stkWidth));
}
}
int xtranslation; int xtranslation;
int ytranslation; int ytranslation;
@ -291,25 +292,22 @@ public class MetalBorders {
int height; int height;
if (resetTransform) { if (resetTransform) {
g2d.setTransform(new AffineTransform());
stkWidth = roundHalfDown(Math.min(at.getScaleX(), at.getScaleY()));
double xx = at.getScaleX() * x + at.getTranslateX(); double xx = at.getScaleX() * x + at.getTranslateX();
double yy = at.getScaleY() * y + at.getTranslateY(); double yy = at.getScaleY() * y + at.getTranslateY();
xtranslation = roundHalfDown(xx); xtranslation = clipRound(xx);
ytranslation = roundHalfDown(yy); ytranslation = clipRound(yy);
width = roundHalfDown(at.getScaleX() * w + xx) - xtranslation; width = clipRound(at.getScaleX() * w + xx) - xtranslation;
height = roundHalfDown(at.getScaleY() * h + yy) - ytranslation; height = clipRound(at.getScaleY() * h + yy) - ytranslation;
} else { } else {
width = w;
height = h;
xtranslation = x; xtranslation = x;
ytranslation = y; ytranslation = y;
width = w;
height = h;
} }
g2d.translate(xtranslation, ytranslation); g.translate(xtranslation, ytranslation);
// scaled border // scaled border
int thickness = (int) Math.ceil(4 * at.getScaleX()); int thickness = (int) Math.ceil(4 * scaleFactor);
g.setColor(background); g.setColor(background);
// Draw the bulk of the border // Draw the bulk of the border
@ -318,17 +316,14 @@ public class MetalBorders {
} }
if (c instanceof JInternalFrame && ((JInternalFrame)c).isResizable()) { if (c instanceof JInternalFrame && ((JInternalFrame)c).isResizable()) {
// set new stroke to draw shadow and highlight lines
g2d.setStroke(new BasicStroke((float) stkWidth));
// midpoint at which highlight & shadow lines // midpoint at which highlight & shadow lines
// are positioned on the border // are positioned on the border
int midPoint = thickness / 2; int midPoint = thickness / 2;
int offset = ((at.getScaleX() - stkWidth) >= 0 && stkWidth % 2 != 0) ? 1 : 0; int offset = (((scaleFactor - stkWidth) >= 0) && ((stkWidth % 2) != 0)) ? 1 : 0;
int loc1 = thickness % 2 == 0 ? midPoint + stkWidth / 2 - stkWidth : midPoint; int loc1 = thickness % 2 == 0 ? midPoint + stkWidth / 2 - stkWidth : midPoint;
int loc2 = thickness % 2 == 0 ? midPoint + stkWidth / 2 : midPoint + stkWidth; int loc2 = thickness % 2 == 0 ? midPoint + stkWidth / 2 : midPoint + stkWidth;
// scaled corner // scaled corner
int corner = (int) Math.round(CORNER * at.getScaleX()); int corner = (int) Math.round(CORNER * scaleFactor);
// Draw the Long highlight lines // Draw the Long highlight lines
g.setColor(highlight); g.setColor(highlight);
@ -350,9 +345,9 @@ public class MetalBorders {
} }
// restore previous transform // restore previous transform
g2d.translate(-xtranslation, -ytranslation); g.translate(-xtranslation, -ytranslation);
if (resetTransform) { if (resetTransform) {
g2d.setColor(oldColor); Graphics2D g2d = (Graphics2D) g;
g2d.setTransform(at); g2d.setTransform(at);
g2d.setStroke(oldStk); g2d.setStroke(oldStk);
} }