Path: blob/master/test/jdk/javax/swing/JCheckBox/8032667/bug8032667_image_diff.java
41153 views
/*1* Copyright (c) 2014, 2018, 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*/22import java.awt.Dimension;23import java.awt.Graphics;24import java.awt.Graphics2D;25import java.awt.Image;26import java.awt.image.BufferedImage;27import javax.swing.JCheckBox;28import javax.swing.JComponent;29import javax.swing.SwingUtilities;3031import jdk.test.lib.Platform;3233/* @test34* @bug 803266735* @summary [macosx] Components cannot be rendered in HiDPI to BufferedImage36* @library /test/lib37* @build jdk.test.lib.Platform38* @run main bug8032667_image_diff39*/40public class bug8032667_image_diff {4142static final int IMAGE_WIDTH = 130;43static final int IMAGE_HEIGHT = 50;4445public static void main(String[] args) throws Exception {4647if (!Platform.isOSX()) {48return;49}5051SwingUtilities.invokeAndWait(new Runnable() {52@Override53public void run() {5455JCheckBox checkBox = new JCheckBox();56checkBox.setSelected(true);57checkBox.setSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT));5859final BufferedImage image1 = getHiDPIImage(checkBox);60final BufferedImage image2 = getScaledImage(checkBox);6162if(equal(image1, image2)){63throw new RuntimeException("2x image equals to non smooth image");64}65}66});67}6869static boolean equal(BufferedImage image1, BufferedImage image2) {7071int w = image1.getWidth();72int h = image1.getHeight();7374if (w != image2.getWidth() || h != image2.getHeight()) {75return false;76}7778for (int i = 0; i < w; i++) {79for (int j = 0; j < h; j++) {80int color1 = image1.getRGB(i, j);81int color2 = image2.getRGB(i, j);8283if (color1 != color2) {84return false;85}86}87}88return true;89}9091static BufferedImage getHiDPIImage(JComponent component) {92return getImage(component, 2, IMAGE_WIDTH, IMAGE_HEIGHT);93}9495static BufferedImage getScaledImage(JComponent component) {96Image image1x = getImage(component, 1, IMAGE_WIDTH, IMAGE_HEIGHT);97final BufferedImage image2x = new BufferedImage(982 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);99final Graphics g = image2x.getGraphics();100((Graphics2D) g).scale(2, 2);101g.drawImage(image1x, 0, 0, null);102g.dispose();103return image2x;104}105106static BufferedImage getImage(JComponent component, int scale, int width, int height) {107final BufferedImage image = new BufferedImage(108scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);109final Graphics g = image.getGraphics();110((Graphics2D) g).scale(scale, scale);111component.paint(g);112g.dispose();113return image;114}115}116117118