Skip to content

Commit

Permalink
8297449: Update JInternalFrame Metal Border code
Browse files Browse the repository at this point in the history
Reviewed-by: lucy
Backport-of: 09629570f5d064dc2a5cd670de8d648156ac3991
  • Loading branch information
Andrew Lu committed Apr 19, 2024
1 parent 6499067 commit a33a174
Showing 1 changed file with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
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
Expand Down Expand Up @@ -227,18 +229,6 @@ public Insets getBorderInsets(Component c, Insets newInsets) {
public static class InternalFrameBorder extends AbstractBorder implements UIResource {
private static final int CORNER = 14;

/**
* 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) {

Expand All @@ -256,41 +246,51 @@ public void paintBorder(Component c, Graphics g, int x, int y,
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;

// 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));
double scaleFactor = 1;

if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g;
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;
int width;
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
Expand All @@ -299,17 +299,14 @@ public void paintBorder(Component c, Graphics g, int x, int y,
}

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);
Expand All @@ -331,9 +328,9 @@ public void paintBorder(Component c, Graphics g, int x, int y,
}

// 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);
}
Expand Down

1 comment on commit a33a174

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.