Skip to content

Commit

Permalink
8294680: Refactor scaled border rendering
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Ivanov <aivanov@openjdk.org>
Reviewed-by: rmahajan, achung, aivanov, jdv
  • Loading branch information
Harshitha Onkar and aivanov-jdk committed Jan 19, 2023
1 parent b317658 commit 80ab50b
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 155 deletions.
131 changes: 124 additions & 7 deletions src/java.desktop/share/classes/com/sun/java/swing/SwingUtilities3.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,19 +25,26 @@

package com.sun.java.swing;

import sun.awt.AppContext;
import sun.awt.SunToolkit;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.applet.Applet;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.Window;
import java.awt.geom.AffineTransform;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

import javax.swing.JComponent;
import javax.swing.RepaintManager;

import sun.awt.AppContext;
import sun.awt.SunToolkit;

import static sun.java2d.pipe.Region.clipRound;

/**
* A collection of utility methods for Swing.
* <p>
Expand Down Expand Up @@ -135,4 +142,114 @@ public static RepaintManager getDelegateRepaintManager(Component
}
return delegate;
}

/**
* A task which paints an <i>unscaled</i> border after {@code Graphics}
* transforms are removed. It's used with the
* {@link #paintBorder(Component, Graphics, int, int, int, int, UnscaledBorderPainter)
* SwingUtilities3.paintBorder} which manages changing the transforms and calculating
* the coordinates and size of the border.
*/
@FunctionalInterface
public interface UnscaledBorderPainter {
/**
* Paints the border for the specified component after the
* {@code Graphics} transforms are removed.
*
* <p>
* The <i>x</i> and <i>y</i> of the painted border are zero.
*
* @param c the component for which this border is being painted
* @param g the paint graphics
* @param w the width of the painted border, in physical pixels
* @param h the height of the painted border, in physical pixels
* @param scaleFactor the scale that was in the {@code Graphics}
*
* @see #paintBorder(Component, Graphics, int, int, int, int, UnscaledBorderPainter)
* SwingUtilities3.paintBorder
* @see javax.swing.border.Border#paintBorder(Component, Graphics, int, int, int, int)
* Border.paintBorder
*/
void paintUnscaledBorder(Component c, Graphics g,
int w, int h,
double scaleFactor);
}

/**
* Paints the border for a component ensuring its sides have consistent
* thickness at different scales.
* <p>
* It performs the following steps:
* <ol>
* <li>Reset the scale transform on the {@code Graphics},</li>
* <li>Call {@code painter} to paint the border,</li>
* <li>Restores the transform.</li>
* </ol>
*
* @param c the component for which this border is being painted
* @param g the paint graphics
* @param x the x position of the painted border
* @param y the y position of the painted border
* @param w the width of the painted border
* @param h the height of the painted border
* @param painter the painter object which paints the border after
* the transform on the {@code Graphics} is reset
*/
public static void paintBorder(Component c, Graphics g,
int x, int y,
int w, int h,
UnscaledBorderPainter painter) {

// Step 1: Reset Transform
AffineTransform at = null;
Stroke oldStroke = null;
boolean resetTransform = false;
double scaleFactor = 1;

int xtranslation = x;
int ytranslation = y;
int width = w;
int height = h;

if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g;
at = g2d.getTransform();
oldStroke = g2d.getStroke();
scaleFactor = Math.min(at.getScaleX(), at.getScaleY());

// if m01 or m10 is non-zero, then there is a rotation or shear,
// or if scale=1, skip resetting the transform in these cases.
resetTransform = ((at.getShearX() == 0) && (at.getShearY() == 0))
&& ((at.getScaleX() > 1) || (at.getScaleY() > 1));

if (resetTransform) {
/* Deactivate the HiDPI scaling transform,
* so we can do paint operations in the device
* pixel coordinate system instead of the logical coordinate system.
*/
g2d.setTransform(new AffineTransform());
double xx = at.getScaleX() * x + at.getTranslateX();
double yy = at.getScaleY() * y + at.getTranslateY();
xtranslation = clipRound(xx);
ytranslation = clipRound(yy);
width = clipRound(at.getScaleX() * w + xx) - xtranslation;
height = clipRound(at.getScaleY() * h + yy) - ytranslation;
}
}

g.translate(xtranslation, ytranslation);

// Step 2: Call respective paintBorder with transformed values
painter.paintUnscaledBorder(c, g, width, height, scaleFactor);

// Step 3: Restore previous stroke & transform
g.translate(-xtranslation, -ytranslation);
if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(oldStroke);
if (resetTransform) {
g2d.setTransform(at);
}
}
}
}
61 changes: 14 additions & 47 deletions src/java.desktop/share/classes/javax/swing/border/EtchedBorder.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,10 +30,10 @@
import java.awt.Insets;
import java.awt.Color;
import java.awt.Component;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.beans.ConstructorProperties;

import com.sun.java.swing.SwingUtilities3;

/**
* A class which implements a simple etched border which can
* either be etched-in or etched-out. If no highlight/shadow
Expand Down Expand Up @@ -150,59 +150,26 @@ private void paintBorderShadow(Graphics g, Color c, int w, int h, int stkWidth)
* @param height the height of the painted border
*/
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
// We remove any initial transforms to prevent rounding errors
// when drawing in non-integer scales
AffineTransform at = null;
Stroke oldStk = null;
int stkWidth = 1;
boolean resetTransform = false;
if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g;
at = g2d.getTransform();
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 = (int) Math.floor(Math.min(at.getScaleX(), at.getScaleY()));
g2d.setStroke(new BasicStroke((float) stkWidth));
}
}
SwingUtilities3.paintBorder(c, g,
x, y,
width, height,
this::paintUnscaledBorder);
}

int w;
int h;
int xtranslation;
int ytranslation;
if (resetTransform) {
w = (int) Math.floor(at.getScaleX() * width - 1);
h = (int) Math.floor(at.getScaleY() * height - 1);
xtranslation = (int) Math.ceil(at.getScaleX()*x+at.getTranslateX());
ytranslation = (int) Math.ceil(at.getScaleY()*y+at.getTranslateY());
} else {
w = width;
h = height;
xtranslation = x;
ytranslation = y;
private void paintUnscaledBorder(Component c, Graphics g,
int w, int h,
double scaleFactor) {
int stkWidth = (int) Math.floor(scaleFactor);
if (g instanceof Graphics2D) {
((Graphics2D) g).setStroke(new BasicStroke((float) stkWidth));
}

g.translate(xtranslation, ytranslation);

paintBorderShadow(g, (etchType == LOWERED) ? getHighlightColor(c)
: getShadowColor(c),
w, h, stkWidth);
paintBorderHighlight(g, (etchType == LOWERED) ? getShadowColor(c)
: getHighlightColor(c),
w, h, stkWidth);

g.translate(-xtranslation, -ytranslation);

// Set the transform we removed earlier
if (resetTransform) {
Graphics2D g2d = (Graphics2D) g;
g2d.setTransform(at);
g2d.setStroke(oldStk);
}
}

/**
Expand Down
63 changes: 15 additions & 48 deletions src/java.desktop/share/classes/javax/swing/border/LineBorder.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -34,9 +34,8 @@
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.beans.ConstructorProperties;
import java.awt.geom.AffineTransform;

import static sun.java2d.pipe.Region.clipRound;
import com.sun.java.swing.SwingUtilities3;

/**
* A class which implements a line border of arbitrary thickness
Expand Down Expand Up @@ -144,73 +143,41 @@ public LineBorder(Color color, int thickness, boolean roundedCorners) {
* @param height the height of the painted border
*/
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
SwingUtilities3.paintBorder(c, g,
x, y,
width, height,
this::paintUnscaledBorder);
}

private void paintUnscaledBorder(Component c, Graphics g,
int w, int h,
double scaleFactor) {
if ((this.thickness > 0) && (g instanceof Graphics2D)) {
Graphics2D g2d = (Graphics2D) g;

AffineTransform at = g2d.getTransform();

// if m01 or m10 is non-zero, then there is a rotation or shear
// or if no Scaling enabled,
// skip resetting the transform
boolean resetTransform = ((at.getShearX() == 0) && (at.getShearY() == 0)) &&
((at.getScaleX() > 1) || (at.getScaleY() > 1));

int xtranslation;
int ytranslation;
int w;
int h;
int offs;

if (resetTransform) {
/* Deactivate the HiDPI scaling transform,
* so we can do paint operations in the device
* pixel coordinate system instead of the logical coordinate system.
*/
g2d.setTransform(new AffineTransform());
double xx = at.getScaleX() * x + at.getTranslateX();
double yy = at.getScaleY() * y + at.getTranslateY();
xtranslation = clipRound(xx);
ytranslation = clipRound(yy);
w = clipRound(at.getScaleX() * width + xx) - xtranslation;
h = clipRound(at.getScaleY() * height + yy) - ytranslation;
offs = this.thickness * (int) at.getScaleX();
} else {
w = width;
h = height;
xtranslation = x;
ytranslation = y;
offs = this.thickness;
}

g2d.translate(xtranslation, ytranslation);

Color oldColor = g2d.getColor();
g2d.setColor(this.lineColor);

Shape outer;
Shape inner;

int offs = this.thickness * (int) scaleFactor;
int size = offs + offs;
if (this.roundedCorners) {
float arc = .2f * offs;
outer = new RoundRectangle2D.Float(0, 0, w, h, offs, offs);
inner = new RoundRectangle2D.Float(offs, offs, w - size, h - size, arc, arc);
}
else {
} else {
outer = new Rectangle2D.Float(0, 0, w, h);
inner = new Rectangle2D.Float(offs, offs, w - size, h - size);
}

Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
path.append(outer, false);
path.append(inner, false);
g2d.fill(path);
g2d.setColor(oldColor);

g2d.translate(-xtranslation, -ytranslation);

if (resetTransform) {
g2d.setTransform(at);
}
g2d.setColor(oldColor);
}
}

Expand Down

1 comment on commit 80ab50b

@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.