1 /*
   2  * Copyright (c) 2016, 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 import java.awt.BorderLayout;
  25 import java.awt.Color;
  26 import java.awt.Dimension;
  27 import java.awt.Graphics;
  28 import java.awt.Point;
  29 import java.awt.Robot;
  30 import java.awt.event.InputEvent;
  31 import java.awt.image.BaseMultiResolutionImage;
  32 import java.awt.image.BufferedImage;
  33 import javax.swing.Icon;
  34 import javax.swing.ImageIcon;
  35 import javax.swing.JFrame;
  36 import javax.swing.JPanel;
  37 import javax.swing.JToggleButton;
  38 import javax.swing.SwingUtilities;
  39 
  40 /**
  41  * @test
  42  * @bug 8151303
  43  * @summary [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
  44  * @run main/othervm  bug8151303
  45  * @run main/othervm -Dsun.java2d.uiScale=2 bug8151303
  46  */
  47 public class bug8151303 {
  48 
  49     private final static int IMAGE_SIZE = 300;
  50 
  51     private final static Color COLOR_1X = Color.RED;
  52     private final static Color COLOR_2X = Color.BLUE;
  53     private static JFrame frame;
  54     private static volatile double scale = -1;
  55     private static volatile int centerX;
  56     private static volatile int centerY;
  57 
  58     public static void main(String[] args) throws Exception {
  59         Robot robot = new Robot();
  60         robot.setAutoDelay(50);
  61 
  62         SwingUtilities.invokeAndWait(() -> createAndShowGUI());
  63         robot.waitForIdle();
  64 
  65         SwingUtilities.invokeAndWait(() -> {
  66             scale = frame.getGraphicsConfiguration().getDefaultTransform()
  67                     .getScaleX();
  68             Point location = frame.getLocation();
  69             Dimension size = frame.getSize();
  70             centerX = location.x + size.width / 2;
  71             centerY = location.y + size.height / 2;
  72         });
  73         robot.waitForIdle();
  74 
  75         robot.mouseMove(centerX, centerY);
  76         robot.mousePress(InputEvent.BUTTON1_MASK);
  77         robot.waitForIdle();
  78         Thread.sleep(100);
  79         Color color = robot.getPixelColor(centerX, centerY);
  80         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  81 
  82         SwingUtilities.invokeAndWait(() -> frame.dispose());
  83 
  84         if ((scale == 1 && !similar(color, COLOR_1X))
  85                 || (scale == 2 && !similar(color, COLOR_2X))) {
  86             throw new RuntimeException("Colors are different!");
  87         }
  88     }
  89 
  90     private static void createAndShowGUI() {
  91         frame = new JFrame();
  92         frame.setSize(IMAGE_SIZE, IMAGE_SIZE);
  93         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94 
  95         JPanel panel = new JPanel(new BorderLayout());
  96 
  97         BufferedImage img1x = generateImage(1, COLOR_1X);
  98 
  99         BufferedImage img2x = generateImage(2, COLOR_2X);
 100         BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
 101                 new BufferedImage[]{img1x, img2x});
 102         Icon mrIcon = new ImageIcon(mri);
 103 
 104         JToggleButton button = new JToggleButton();
 105         button.setIcon(mrIcon);
 106         panel.add(button, BorderLayout.CENTER);
 107 
 108         frame.getContentPane().add(panel);
 109         frame.setVisible(true);
 110     }
 111 
 112     private static boolean similar(Color c1, Color c2) {
 113         return similar(c1.getRed(), c2.getRed())
 114                 && similar(c1.getGreen(), c2.getGreen())
 115                 && similar(c1.getBlue(), c2.getBlue());
 116     }
 117 
 118     private static boolean similar(int n, int m) {
 119         return Math.abs(n - m) <= 50;
 120     }
 121 
 122     private static BufferedImage generateImage(int scale, Color c) {
 123 
 124         int size = IMAGE_SIZE * scale;
 125         BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
 126         Graphics g = img.createGraphics();
 127         g.setColor(c);
 128         g.fillRect(0, 0, size, size);
 129         g.dispose();
 130         return img;
 131     }
 132 }