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 24import java.awt.Color; 25import java.awt.Frame; 26import java.awt.Graphics; 27import java.awt.Graphics2D; 28import java.awt.Image; 29import java.awt.MediaTracker; 30import java.awt.Toolkit; 31import java.awt.image.BaseMultiResolutionImage; 32import java.awt.image.BufferedImage; 33import java.io.File; 34import javax.imageio.ImageIO; 35import javax.swing.GrayFilter; 36import java.awt.image.MultiResolutionImage; 37import javax.swing.JLabel; 38 39/** 40 * @test 41 * @bug 8156182 42 * @summary [macosx] HiDPI/Retina icons do not work for disabled 43 * JButton/JMenuItem etc. 44 * @run main/othervm -Dsun.java2d.uiScale=2 MultiResolutionDisabledImageTest 45 */ 46public class MultiResolutionDisabledImageTest { 47 48 private static final String IMAGE_NAME_1X = "image.png"; 49 private static final String IMAGE_NAME_2X = "image@2x.png"; 50 private static final int IMAGE_SIZE = 100; 51 private static final Color COLOR_1X = Color.GREEN; 52 private static final Color COLOR_2X = Color.BLUE; 53 54 public static void main(String[] args) throws Exception { 55 56 Image baseMRImage = new BaseMultiResolutionImage(createImage(1), 57 createImage(2)); 58 testMRDisabledImage(baseMRImage); 59 60 saveImages(); 61 Image toolkitMRImage = Toolkit.getDefaultToolkit().getImage(IMAGE_NAME_1X); 62 63 if (toolkitMRImage instanceof MultiResolutionImage) { 64 testMRDisabledImage(toolkitMRImage); 65 } 66 } 67 68 private static void testMRDisabledImage(Image image) throws Exception { 69 70 Image disabledImage = GrayFilter.createDisabledImage(image); 71 MediaTracker mediaTracker = new MediaTracker(new JLabel()); 72 mediaTracker.addImage(disabledImage, 0); 73 mediaTracker.waitForID(0); 74 75 BufferedImage buffImage = new BufferedImage(IMAGE_SIZE, 76 IMAGE_SIZE, 77 BufferedImage.TYPE_INT_RGB); 78 79 int x = IMAGE_SIZE / 2; 80 int y = IMAGE_SIZE / 2; 81 82 Graphics2D g = buffImage.createGraphics(); 83 84 g.scale(1, 1); 85 g.drawImage(disabledImage, 0, 0, null); 86 int rgb1x = buffImage.getRGB(x, y); 87 88 g.scale(2, 2); 89 g.drawImage(disabledImage, 0, 0, null); 90 int rgb2x = buffImage.getRGB(x, y); 91 92 g.dispose(); 93 94 if (rgb1x == rgb2x) { 95 throw new RuntimeException("Disabled image is the same for the base" 96 + "image and the resolution variant"); 97 } 98 99 } 100 101 private static BufferedImage createImage(int scale) throws Exception { 102 BufferedImage image = new BufferedImage(scale * 200, scale * 300, 103 BufferedImage.TYPE_INT_RGB); 104 Graphics g = image.createGraphics(); 105 g.setColor(scale == 1 ? COLOR_1X : COLOR_2X); 106 g.fillRect(0, 0, scale * 200, scale * 300); 107 g.dispose(); 108 return image; 109 } 110 111 private static void saveImages() throws Exception { 112 saveImage(createImage(1), IMAGE_NAME_1X); 113 saveImage(createImage(2), IMAGE_NAME_2X); 114 } 115 116 private static void saveImage(BufferedImage image, String name) throws Exception { 117 ImageIO.write(image, "png", new File(name)); 118 } 119} 120