/* * Copyright (c) 2016, 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. */ /* * @test * @bug 8150724 * @author a.stepanov * @summary Check that correct resolution variants are chosen for icons * when multiresolution image is used for their construction. * * @library ../../../../lib/testlibrary/ * @build ExtendedRobot * @run main/othervm -Dsun.java2d.uiScale=1 MultiresolutionIconTest * @run main/othervm -Dsun.java2d.uiScale=2 MultiresolutionIconTest */ import java.awt.*; import java.awt.image.BaseMultiResolutionImage; import java.awt.image.BufferedImage; import javax.swing.*; public class MultiresolutionIconTest extends JFrame { private final static int DELAY = 1000; private final static int SZ = 70; private final static int N = 5; // number of components private final static String SCALE = "sun.java2d.uiScale"; private final static Color C1X = Color.RED; private final static Color C2X = Color.BLUE; private JLabel lbl; private JTabbedPane tabbedPane; private final ExtendedRobot r; private static BufferedImage generateImage(int scale, Color c) { int x = (SZ / 2) * scale; BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(c); g.fillRect(0, 0, x, x); return img; } public MultiresolutionIconTest() throws Exception { r = new ExtendedRobot(); SwingUtilities.invokeAndWait(this::UI); } private void UI() { BufferedImage img1x = generateImage(1, C1X); BufferedImage img2x = generateImage(2, C2X); BaseMultiResolutionImage mri = new BaseMultiResolutionImage( new BufferedImage[]{img1x, img2x}); Icon icon = new ImageIcon(mri); setSize(N * SZ, SZ); setLocation(50, 50); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new GridLayout(1, 1)); JPanel p = new JPanel(); p.setLayout(new GridLayout(1, N)); JButton btn = new JButton(icon); p.add(btn); JToggleButton tbn = new JToggleButton(icon); p.add(tbn); JRadioButton rbn = new JRadioButton(icon); rbn.setHorizontalAlignment(SwingConstants.CENTER); p.add(rbn); JCheckBox cbx = new JCheckBox(icon); cbx.setHorizontalAlignment(SwingConstants.CENTER); p.add(cbx); lbl = new JLabel(icon); p.add(lbl); tabbedPane = new JTabbedPane(JTabbedPane.LEFT); tabbedPane.addTab("", icon, p); getContentPane().add(tabbedPane); setVisible(true); } private void doTest() { r.waitForIdle(2 * DELAY); String scale = System.getProperty(SCALE); System.out.println("scale = " + scale); Color expected = ((scale == null) || "1".equals(scale)) ? C1X : C2X; Point p = lbl.getLocationOnScreen(); int x = p.x + lbl.getWidth() / 2; int y = p.y + lbl.getHeight() / 3; int w = lbl.getWidth(); boolean ok = true; Color c; String components[] = new String[]{ "JLabel", "JCheckBox", "JRadioButton", "JToggleButton", "JButton"}; for (int i = 0; i < N; ++i) { c = r.getPixelColor(x - i * w, y); System.out.print(components[i] + " icon: "); if (c.equals(expected)) { System.out.println("ok"); } else { System.out.println("nok"); ok = false; } r.waitForIdle(); } x -= ((N - 1) * w + w / 2); x = (tabbedPane.getLocationOnScreen().x + x) / 2; c = r.getPixelColor(x, y); if (c.equals(expected)) { System.out.println("JTabbedPane icon: ok"); } else { System.out.println("JTabbedPane icon: nok"); ok = false; } if (!ok) { throw new RuntimeException("test failed"); } r.waitForIdle(); dispose(); } public static void main(String[] args) throws Exception { (new MultiresolutionIconTest()).doTest(); } }