Skip to content

Commit 9c817d3

Browse files
DamonGuyaivanov-jdk
authored andcommitted
8015854: [macosx] JButton's HTML ImageView adding unwanted padding
Reviewed-by: psadhukhan, aivanov
1 parent 733c790 commit 9c817d3

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed

src/java.desktop/macosx/classes/com/apple/laf/AquaButtonUI.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,15 @@ public void paint(final Graphics g, final JComponent c) {
305305
}
306306

307307
// performs icon and text rect calculations
308-
final String text = layoutAndGetText(g, b, aquaBorder, i, viewRect, iconRect, textRect);
308+
final String text;
309+
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
310+
if (v != null) {
311+
// use zero insets for view since layout only handles text calculations
312+
text = layoutAndGetText(g, b, aquaBorder, new Insets(0,0,0,0),
313+
viewRect, iconRect, textRect);
314+
} else {
315+
text = layoutAndGetText(g, b, aquaBorder, i, viewRect, iconRect, textRect);
316+
}
309317

310318
// Paint the Icon
311319
if (b.getIcon() != null) {
@@ -317,7 +325,6 @@ public void paint(final Graphics g, final JComponent c) {
317325
}
318326

319327
if (text != null && !text.isEmpty()) {
320-
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
321328
if (v != null) {
322329
v.paint(g, textRect);
323330
} else {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8015854
27+
* @requires (os.family == "mac")
28+
* @summary Tests HTML image as JButton text for unwanted padding on macOS Aqua LAF
29+
* @run main HtmlButtonImageTest
30+
*/
31+
32+
import java.awt.Color;
33+
import java.awt.Dimension;
34+
import java.awt.Graphics2D;
35+
import java.awt.image.BufferedImage;
36+
import java.io.File;
37+
import java.io.IOException;
38+
import java.nio.file.Path;
39+
40+
import javax.imageio.ImageIO;
41+
import javax.swing.JButton;
42+
import javax.swing.SwingUtilities;
43+
import javax.swing.UIManager;
44+
45+
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
46+
47+
public final class HtmlButtonImageTest {
48+
private static JButton button;
49+
private static Path testDir;
50+
private static BufferedImage image;
51+
52+
private static final int BUTTON_HEIGHT = 37;
53+
private static final int BUTTON_WIDTH = 37;
54+
private static final int SQUARE_HEIGHT = 19;
55+
private static final int SQUARE_WIDTH = 19;
56+
private static final int centerX = BUTTON_WIDTH / 2;
57+
private static final int centerY = BUTTON_HEIGHT / 2;
58+
private static final int minX = centerX - (SQUARE_WIDTH / 2);
59+
private static final int minY = centerY - (SQUARE_HEIGHT / 2);
60+
private static final int maxX = centerX + (SQUARE_WIDTH / 2);
61+
private static final int maxY = centerY + (SQUARE_HEIGHT / 2);
62+
63+
public static void main(String[] args) throws Exception {
64+
UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
65+
testDir = Path.of(System.getProperty("test.classes", "."));
66+
generateRedSquare();
67+
68+
SwingUtilities.invokeAndWait(HtmlButtonImageTest::createButton);
69+
SwingUtilities.invokeAndWait(HtmlButtonImageTest::paintButton);
70+
71+
testImageCentering(image.getRGB(centerX, centerY),
72+
image.getRGB(minX, minY),
73+
image.getRGB(minX, maxY),
74+
image.getRGB(maxX, minY),
75+
image.getRGB(maxX, maxY));
76+
}
77+
78+
private static void generateRedSquare() throws IOException {
79+
BufferedImage bImg = new BufferedImage(SQUARE_WIDTH, SQUARE_HEIGHT,
80+
TYPE_INT_ARGB);
81+
Graphics2D cg = bImg.createGraphics();
82+
cg.setColor(Color.RED);
83+
cg.fillRect(0, 0, SQUARE_WIDTH, SQUARE_HEIGHT);
84+
ImageIO.write(bImg, "png", new File(testDir + "/red_square.png"));
85+
}
86+
87+
private static void createButton() {
88+
button = new JButton();
89+
button.setSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
90+
button.setText("<html><img src='"
91+
+ testDir.resolve("red_square.png").toUri() + "'></html>");
92+
}
93+
94+
private static void paintButton() {
95+
image = new BufferedImage(BUTTON_HEIGHT, BUTTON_WIDTH, TYPE_INT_ARGB);
96+
Graphics2D graphics2D = image.createGraphics();
97+
button.paint(graphics2D);
98+
graphics2D.dispose();
99+
}
100+
101+
private static boolean checkRedColor(int rgb) {
102+
return (rgb == Color.RED.getRGB());
103+
}
104+
105+
private static void testImageCentering(int... colors) throws IOException {
106+
for (int c : colors) {
107+
if (!checkRedColor(c)) {
108+
ImageIO.write(image, "png",
109+
new File(testDir + "/fail_image.png"));
110+
throw new RuntimeException("HTML image not centered in button");
111+
}
112+
}
113+
System.out.println("Passed");
114+
}
115+
}

0 commit comments

Comments
 (0)