Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JCheckBox/8032667/bug8032667_image_diff.java
32273 views
/*1* Copyright (c) 2014, 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;30import sun.awt.OSInfo;3132/* @test33* @bug 803266734* @summary [macosx] Components cannot be rendered in HiDPI to BufferedImage35* @run main bug8032667_image_diff36*/37public class bug8032667_image_diff {3839static final int IMAGE_WIDTH = 130;40static final int IMAGE_HEIGHT = 50;4142public static void main(String[] args) throws Exception {4344if(!OSInfo.OSType.MACOSX.equals(OSInfo.getOSType())){45return;46}4748SwingUtilities.invokeAndWait(new Runnable() {49@Override50public void run() {5152JCheckBox checkBox = new JCheckBox();53checkBox.setSelected(true);54checkBox.setSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT));5556final BufferedImage image1 = getHiDPIImage(checkBox);57final BufferedImage image2 = getScaledImage(checkBox);5859if(equal(image1, image2)){60throw new RuntimeException("2x image equals to non smooth image");61}62}63});64}6566static boolean equal(BufferedImage image1, BufferedImage image2) {6768int w = image1.getWidth();69int h = image1.getHeight();7071if (w != image2.getWidth() || h != image2.getHeight()) {72return false;73}7475for (int i = 0; i < w; i++) {76for (int j = 0; j < h; j++) {77int color1 = image1.getRGB(i, j);78int color2 = image2.getRGB(i, j);7980if (color1 != color2) {81return false;82}83}84}85return true;86}8788static BufferedImage getHiDPIImage(JComponent component) {89return getImage(component, 2, IMAGE_WIDTH, IMAGE_HEIGHT);90}9192static BufferedImage getScaledImage(JComponent component) {93Image image1x = getImage(component, 1, IMAGE_WIDTH, IMAGE_HEIGHT);94final BufferedImage image2x = new BufferedImage(952 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);96final Graphics g = image2x.getGraphics();97((Graphics2D) g).scale(2, 2);98g.drawImage(image1x, 0, 0, null);99g.dispose();100return image2x;101}102103static BufferedImage getImage(JComponent component, int scale, int width, int height) {104final BufferedImage image = new BufferedImage(105scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);106final Graphics g = image.getGraphics();107((Graphics2D) g).scale(scale, scale);108component.paint(g);109g.dispose();110return image;111}112}113114115