Skip to content

Commit

Permalink
8297449: Update JInternalFrame Metal Border code
Browse files Browse the repository at this point in the history
Reviewed-by: aivanov, kizune
  • Loading branch information
Harshitha Onkar committed Dec 1, 2022
1 parent 9f94cbe commit 0962957
Showing 1 changed file with 33 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 @@ -246,19 +248,6 @@ public static class InternalFrameBorder extends AbstractBorder implements UIReso
*/
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;
Expand All @@ -275,41 +264,50 @@ 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;
double scaleFactor = 1;

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 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 (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 @@ -318,17 +316,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 @@ -350,9 +345,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

3 comments on commit 0962957

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 0962957 Mar 19, 2024

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 0962957 Mar 19, 2024

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch backport-GoeLin-09629570 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 09629570 from the openjdk/jdk repository.

The commit being backported was authored by Harshitha Onkar on 1 Dec 2022 and was reviewed by Alexey Ivanov and Alexander Zuev.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-09629570:backport-GoeLin-09629570
$ git checkout backport-GoeLin-09629570
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-09629570

Please sign in to comment.