8258979: The image didn't show correctly with GTK LAF

Reviewed-by: psadhukhan, tr
This commit is contained in:
Abhishek Kumar 2024-01-12 07:53:41 +00:00
parent 8d9814a521
commit 82a63a03c0
4 changed files with 144 additions and 137 deletions
src/java.desktop/share/classes/javax/swing/plaf/synth
test/jdk/javax/swing/JTree

@ -73,7 +73,9 @@ public class SynthTreeUI extends BasicTreeUI
private boolean useTreeColors;
private Icon expandedIconWrapper = new ExpandedIconWrapper();
private Icon expandedIconWrapper;
private Icon collapsedIconWrapper;
/**
*
@ -99,12 +101,19 @@ public class SynthTreeUI extends BasicTreeUI
return expandedIconWrapper;
}
@Override
public Icon getCollapsedIcon() {
return collapsedIconWrapper;
}
/**
* {@inheritDoc}
*/
@Override
protected void installDefaults() {
updateStyle(tree);
expandedIconWrapper = new IconWrapper(expandedIcon);
collapsedIconWrapper = new IconWrapper(collapsedIcon);
}
private void updateStyle(JTree tree) {
@ -778,44 +787,40 @@ public class SynthTreeUI extends BasicTreeUI
}
//
// BasicTreeUI directly uses expandIcon outside of the Synth methods.
// BasicTreeUI directly uses expandIcon and collapsedIcon outside of the
// Synth methods.
// To get the correct context we return an instance of this that fetches
// the SynthContext as needed.
//
private class ExpandedIconWrapper implements SynthIcon {
private class IconWrapper implements SynthIcon {
Icon iconType;
public IconWrapper(Icon type) {
super();
iconType = type;
}
public void paintIcon(SynthContext context, Graphics g, int x,
int y, int w, int h) {
if (context == null) {
context = getContext(tree);
SynthGraphicsUtils.paintIcon(expandedIcon, context, g, x, y, w, h);
}
else {
SynthGraphicsUtils.paintIcon(expandedIcon, context, g, x, y, w, h);
}
SynthGraphicsUtils.paintIcon(iconType, context, g, x, y, w, h);
}
public int getIconWidth(SynthContext context) {
int width;
if (context == null) {
context = getContext(tree);
width = SynthGraphicsUtils.getIconWidth(expandedIcon, context);
}
else {
width = SynthGraphicsUtils.getIconWidth(expandedIcon, context);
}
return width;
return SynthGraphicsUtils.getIconWidth(iconType, context);
}
public int getIconHeight(SynthContext context) {
int height;
if (context == null) {
context = getContext(tree);
height = SynthGraphicsUtils.getIconHeight(expandedIcon, context);
}
else {
height = SynthGraphicsUtils.getIconHeight(expandedIcon, context);
}
return height;
return SynthGraphicsUtils.getIconHeight(iconType, context);
}
}
}

@ -1,36 +0,0 @@
<!--
Copyright (c) 2014, 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
under the terms of the GNU General Public License version 2 only, as
published by the Free Software Foundation.
This code is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
version 2 for more details (a copy is included in the LICENSE file that
accompanied this code).
You should have received a copy of the GNU General Public License version
2 along with this work; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
or visit www.oracle.com if you need additional information or have any
questions.
-->
<html>
<body>
Verify that scaled icons are rendered smoothly.
1. Run the test.
2. Check that Collapsed and Expanded JTree icons are drawn smoothly.
If so, press PASS, else press FAIL.
<applet code="bug8038113.class" width=400 height=400></applet>
</body>
</html>

@ -1,82 +0,0 @@
/*
* Copyright (c) 2014, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.Icon;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicTreeUI;
/* @test
* @bug 8038113
* @summary [macosx] JTree icon is not rendered in high resolution on Retina
* @run applet/manual=yesno bug8038113.html
*/
public class bug8038113 extends JApplet {
@Override
public void init() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JTree tree = new JTree();
final BasicTreeUI treeUI = (BasicTreeUI) tree.getUI();
final JPanel panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(0.5f));
g2.scale(2, 2);
int x = 10;
int y = 10;
Icon collapsedIcon = treeUI.getCollapsedIcon();
Icon expandeIcon = treeUI.getExpandedIcon();
int w = collapsedIcon.getIconWidth();
int h = collapsedIcon.getIconHeight();
collapsedIcon.paintIcon(this, g, x, y);
g.drawRect(x, y, w, h);
y += 10 + h;
w = expandeIcon.getIconWidth();
h = expandeIcon.getIconHeight();
expandeIcon.paintIcon(this, g, x, y);
g.drawRect(x, y, w, h);
}
};
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.CENTER);
}
});
}
}

@ -0,0 +1,120 @@
/*
* Copyright (c) 2014, 2024, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import static java.awt.image.BufferedImage.TYPE_INT_RGB;
/* @test
* @key headful
* @bug 8038113 8258979
* @summary [macosx] JTree icon is not rendered in high resolution on Retina,
* collapsed icon is not rendered for GTK LAF as well.
* @run main bug8038113
*/
public class bug8038113 {
public static void main(String[] args) throws Exception {
for (UIManager.LookAndFeelInfo laf :
UIManager.getInstalledLookAndFeels()) {
if (!laf.getName().contains("Motif")) {
System.out.println("Testing LAF: " + laf.getName());
SwingUtilities.invokeAndWait(() -> test(laf));
}
}
}
public static void test(UIManager.LookAndFeelInfo laf) {
setLookAndFeel(laf);
final JTree tree = new JTree();
final BasicTreeUI treeUI = (BasicTreeUI) tree.getUI();
Icon collapsedIcon = treeUI.getCollapsedIcon();
Icon expandedIcon = treeUI.getExpandedIcon();
BufferedImage img1 = paintToImage(expandedIcon);
BufferedImage img2 = paintToImage(collapsedIcon);
if (!isImgRendered(img1)) {
try {
ImageIO.write(img1, "png", new File("Expanded_Icon_" + laf.getName() + ".png"));
} catch (IOException ignored) {
}
throw new RuntimeException("Test Failed, Expanded not rendered for: "+laf.getName());
}
if (!isImgRendered(img2)) {
try {
ImageIO.write(img2, "png", new File("Collapsed_Icon_" + laf.getName() + ".png"));
} catch (IOException ignored) {
}
throw new RuntimeException("Test Failed, Collapsed Icon not rendered for: "+laf.getName());
}
System.out.println("Test Passed");
}
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
try {
UIManager.setLookAndFeel(laf.getClassName());
} catch (UnsupportedLookAndFeelException ignored) {
System.out.println("Unsupported LAF: " + laf.getClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static BufferedImage paintToImage(Icon content) {
BufferedImage im = new BufferedImage(content.getIconWidth(),
content.getIconHeight(), TYPE_INT_RGB);
Graphics2D g = (Graphics2D) im.getGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, content.getIconWidth(), content.getIconHeight());
content.paintIcon(new JLabel(), g, 0, 0);
g.dispose();
return im;
}
private static boolean isImgRendered(BufferedImage img) {
Color white = new Color(255, 255, 255);
for (int x = 0; x < img.getWidth(); ++x) {
for (int y = 0; y < img.getHeight(); ++y) {
if (img.getRGB(x, y) != white.getRGB()) {
return true;
}
}
}
return false;
}
}