Path: blob/master/test/jdk/javax/swing/JButton/8151303/PressedIconTest.java
41153 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.BorderLayout;24import java.awt.Color;25import java.awt.Dimension;26import java.awt.Graphics;27import java.awt.Point;28import java.awt.Robot;29import java.awt.event.InputEvent;30import java.awt.image.BaseMultiResolutionImage;31import java.awt.image.BufferedImage;32import javax.swing.Icon;33import javax.swing.ImageIcon;34import javax.swing.JFrame;35import javax.swing.JPanel;36import javax.swing.JToggleButton;37import javax.swing.SwingUtilities;3839/**40* @test41* @key headful42* @bug 815130343* @summary [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it44* @run main/othervm PressedIconTest45* @run main/othervm -Dsun.java2d.uiScale=2 PressedIconTest46*/4748public class PressedIconTest {4950private final static int IMAGE_SIZE = 300;5152private final static Color COLOR_1X = Color.RED;53private final static Color COLOR_2X = Color.BLUE;54private static JFrame frame;55private static volatile double scale = -1;56private static volatile int centerX;57private static volatile int centerY;5859public static void main(String[] args) throws Exception {60Robot robot = new Robot();61robot.setAutoDelay(50);6263SwingUtilities.invokeAndWait(() -> createAndShowGUI());64robot.waitForIdle();6566SwingUtilities.invokeAndWait(() -> {67scale = frame.getGraphicsConfiguration().getDefaultTransform()68.getScaleX();69Point location = frame.getLocation();70Dimension size = frame.getSize();71centerX = location.x + size.width / 2;72centerY = location.y + size.height / 2;73});74robot.waitForIdle();7576robot.mouseMove(centerX, centerY);77robot.mousePress(InputEvent.BUTTON1_MASK);78robot.waitForIdle();79Thread.sleep(100);80Color color = robot.getPixelColor(centerX, centerY);81robot.mouseRelease(InputEvent.BUTTON1_MASK);8283SwingUtilities.invokeAndWait(() -> frame.dispose());8485if ((scale == 1 && !similar(color, COLOR_1X))86|| (scale == 2 && !similar(color, COLOR_2X))) {87throw new RuntimeException("Colors are different!");88}89}9091private static void createAndShowGUI() {92frame = new JFrame();93frame.setSize(IMAGE_SIZE, IMAGE_SIZE);94frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);9596JPanel panel = new JPanel(new BorderLayout());9798BufferedImage img1x = generateImage(1, COLOR_1X);99100BufferedImage img2x = generateImage(2, COLOR_2X);101BaseMultiResolutionImage mri = new BaseMultiResolutionImage(102new BufferedImage[]{img1x, img2x});103Icon mrIcon = new ImageIcon(mri);104105JToggleButton button = new JToggleButton();106button.setIcon(mrIcon);107panel.add(button, BorderLayout.CENTER);108109frame.getContentPane().add(panel);110frame.setVisible(true);111}112113private static boolean similar(Color c1, Color c2) {114return similar(c1.getRed(), c2.getRed())115&& similar(c1.getGreen(), c2.getGreen())116&& similar(c1.getBlue(), c2.getBlue());117}118119private static boolean similar(int n, int m) {120return Math.abs(n - m) <= 50;121}122123private static BufferedImage generateImage(int scale, Color c) {124125int size = IMAGE_SIZE * scale;126BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);127Graphics g = img.createGraphics();128g.setColor(c);129g.fillRect(0, 0, size, size);130g.dispose();131return img;132}133}134135136