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.SwingUtilities2;
import static sun.java2d.pipe.Region.clipRound;
/**
* Factory object that can vend Borders appropriate for the metal L & F.
* @author Steve Wilson
@ -246,19 +248,6 @@ public class MetalBorders {
*/
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,
int w, int h) {
Color background;
@ -275,15 +264,27 @@ public class MetalBorders {
shadow = MetalLookAndFeel.getControlInfo();
}
Graphics2D g2d = (Graphics2D) g;
AffineTransform at = g2d.getTransform();
Stroke oldStk = g2d.getStroke();
Color oldColor = g2d.getColor();
AffineTransform at = null;
Stroke oldStk = null;
boolean resetTransform = false;
int stkWidth = 1;
double scaleFactor = 1;
// if m01 or m10 is non-zero, then there is a rotation or shear
// skip resetting the transform
boolean resetTransform = ((at.getShearX() == 0) && (at.getShearY() == 0));
if (g instanceof Graphics2D g2d) {
at = g2d.getTransform();
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 ytranslation;
@ -291,25 +292,22 @@ public class MetalBorders {
int height;
if (resetTransform) {
g2d.setTransform(new AffineTransform());
stkWidth = roundHalfDown(Math.min(at.getScaleX(), at.getScaleY()));
double xx = at.getScaleX() * x + at.getTranslateX();
double yy = at.getScaleY() * y + at.getTranslateY();
xtranslation = roundHalfDown(xx);
ytranslation = roundHalfDown(yy);
width = roundHalfDown(at.getScaleX() * w + xx) - xtranslation;
height = roundHalfDown(at.getScaleY() * h + yy) - ytranslation;
xtranslation = clipRound(xx);
ytranslation = clipRound(yy);
width = clipRound(at.getScaleX() * w + xx) - xtranslation;
height = clipRound(at.getScaleY() * h + yy) - ytranslation;
} else {
width = w;
height = h;
xtranslation = x;
ytranslation = y;
width = w;
height = h;
}
g2d.translate(xtranslation, ytranslation);
g.translate(xtranslation, ytranslation);
// scaled border
int thickness = (int) Math.ceil(4 * at.getScaleX());
int thickness = (int) Math.ceil(4 * scaleFactor);
g.setColor(background);
// Draw the bulk of the border
@ -318,17 +316,14 @@ public class MetalBorders {
}
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
// are positioned on the border
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 loc2 = thickness % 2 == 0 ? midPoint + stkWidth / 2 : midPoint + stkWidth;
// scaled corner
int corner = (int) Math.round(CORNER * at.getScaleX());
int corner = (int) Math.round(CORNER * scaleFactor);
// Draw the Long highlight lines
g.setColor(highlight);
@ -350,9 +345,9 @@ public class MetalBorders {
}
// restore previous transform
g2d.translate(-xtranslation, -ytranslation);
g.translate(-xtranslation, -ytranslation);
if (resetTransform) {
g2d.setColor(oldColor);
Graphics2D g2d = (Graphics2D) g;
g2d.setTransform(at);
g2d.setStroke(oldStk);
}