1/* 2 * Copyright (c) 2016, 2017, 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 24import java.awt.Image; 25import java.awt.Robot; 26import java.awt.Rectangle; 27import java.awt.Color; 28import java.awt.Point; 29import java.awt.Dimension; 30import java.awt.GradientPaint; 31import java.awt.Graphics2D; 32import java.awt.BorderLayout; 33import java.awt.image.BufferedImage; 34import java.awt.image.MultiResolutionImage; 35import java.util.List; 36import javax.swing.JButton; 37import javax.swing.JFrame; 38import javax.swing.JPanel; 39import javax.swing.SwingUtilities; 40import javax.swing.UIManager; 41import javax.swing.plaf.metal.MetalLookAndFeel; 42 43/* 44 * @test 45 * @bug 8163193 8165213 46 * @key headful 47 * @summary Metal L&F gradient is lighter on HiDPI display 48 * @run main/othervm -Dsun.java2d.uiScale=2 ButtonGradientTest 49 */ 50public class ButtonGradientTest { 51 private static JFrame frame; 52 private static JButton button; 53 private static List<Image> images; 54 private static Robot robot; 55 56 public static void main(String[] args) throws Exception { 57 try { 58 robot = new Robot(); 59 testGradient(); 60 } finally { 61 SwingUtilities.invokeAndWait(() -> { 62 if (frame != null) { 63 frame.dispose(); 64 } 65 }); 66 } 67 } 68 69 private static void testGradient() throws Exception { 70 // Create and show the GUI 71 SwingUtilities.invokeAndWait(ButtonGradientTest::createAndShowGUI); 72 robot.waitForIdle(); 73 robot.delay(500); 74 75 Rectangle rect = getButtonBounds(); 76 List<?> gradient = (List) UIManager.get("Button.gradient"); 77 float ratio = ((Number) gradient.get(0)).floatValue(); 78 Color c1 = (Color) gradient.get(2); 79 Color c2 = (Color) gradient.get(3); 80 int mid = (int) (ratio * rect.height); 81 // Get the expected gradient color 82 Color gradientColor = getGradientColor(rect.width, mid, c1, c2); 83 84 // Get color from robot captured hidpi image of the button 85 getImageFromRobot(); 86 int x = rect.x + rect.width / 2; 87 int y = rect.y + mid / 2; 88 Color buttonColor = new Color(((BufferedImage)(images.get(1))).getRGB( 89 (int)(x), (int)(y))); 90 91 if (!similarColors(buttonColor, gradientColor)) { 92 throw new RuntimeException("Button gradient is changed!"); 93 } 94 } 95 96 private static void createAndShowGUI() { 97 98 try { 99 UIManager.setLookAndFeel(new MetalLookAndFeel()); 100 } catch (Exception e) { 101 throw new RuntimeException(e); 102 } 103 104 frame = new JFrame(); 105 frame.setSize(300, 300); 106 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 107 108 JPanel panel = new JPanel(new BorderLayout()); 109 button = new JButton(""); 110 panel.add(button); 111 frame.getContentPane().add(panel); 112 frame.setVisible(true); 113 } 114 115 private static void getImageFromRobot() { 116 try { 117 Point butLoc = button.getLocationOnScreen(); 118 Dimension butSize = button.getSize(); 119 MultiResolutionImage multiResolutionImage = 120 robot.createMultiResolutionScreenCapture( 121 new Rectangle((int)butLoc.getX(), 122 (int)butLoc.getY(), (int)butSize.getWidth(), 123 (int)butSize.getHeight())); 124 images = multiResolutionImage.getResolutionVariants(); 125 } catch (Exception e) { 126 throw new RuntimeException(e); 127 } 128 129 if(images.size() < 2) { 130 throw new RuntimeException("HiDpi Image not captured - " + 131 "Check is this HiDpi display system?"); 132 } 133 } 134 135 private static Rectangle getButtonBounds() throws Exception { 136 Rectangle[] rectangles = new Rectangle[1]; 137 SwingUtilities.invokeAndWait(() -> { 138 rectangles[0] = button.getBounds(); 139 rectangles[0].setLocation(button.getLocationOnScreen()); 140 }); 141 return rectangles[0]; 142 } 143 144 private static Color getGradientColor(int w, int h, Color c1, Color c2) { 145 GradientPaint gradient = new GradientPaint(0, 0, c1, 0, h, c2, 146 true); 147 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 148 Graphics2D g = img.createGraphics(); 149 g.setPaint(gradient); 150 g.fillRect(0, 0, w, h); 151 g.dispose(); 152 return new Color(img.getRGB(w / 2, h / 2)); 153 } 154 155 private static boolean similarColors(Color c1, Color c2) { 156 return similar(c1.getRed(), c2.getRed()) 157 && similar(c1.getGreen(), c2.getGreen()) 158 && similar(c1.getBlue(), c2.getBlue()); 159 } 160 161 private static boolean similar(int i1, int i2) { 162 return Math.abs(i2 - i1) < 6; 163 } 164}