Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JCheckBox/8032667/bug8032667_image_diff.java
32273 views
1
/*
2
* Copyright (c) 2014, 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
import java.awt.Dimension;
24
import java.awt.Graphics;
25
import java.awt.Graphics2D;
26
import java.awt.Image;
27
import java.awt.image.BufferedImage;
28
import javax.swing.JCheckBox;
29
import javax.swing.JComponent;
30
import javax.swing.SwingUtilities;
31
import sun.awt.OSInfo;
32
33
/* @test
34
* @bug 8032667
35
* @summary [macosx] Components cannot be rendered in HiDPI to BufferedImage
36
* @run main bug8032667_image_diff
37
*/
38
public class bug8032667_image_diff {
39
40
static final int IMAGE_WIDTH = 130;
41
static final int IMAGE_HEIGHT = 50;
42
43
public static void main(String[] args) throws Exception {
44
45
if(!OSInfo.OSType.MACOSX.equals(OSInfo.getOSType())){
46
return;
47
}
48
49
SwingUtilities.invokeAndWait(new Runnable() {
50
@Override
51
public void run() {
52
53
JCheckBox checkBox = new JCheckBox();
54
checkBox.setSelected(true);
55
checkBox.setSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT));
56
57
final BufferedImage image1 = getHiDPIImage(checkBox);
58
final BufferedImage image2 = getScaledImage(checkBox);
59
60
if(equal(image1, image2)){
61
throw new RuntimeException("2x image equals to non smooth image");
62
}
63
}
64
});
65
}
66
67
static boolean equal(BufferedImage image1, BufferedImage image2) {
68
69
int w = image1.getWidth();
70
int h = image1.getHeight();
71
72
if (w != image2.getWidth() || h != image2.getHeight()) {
73
return false;
74
}
75
76
for (int i = 0; i < w; i++) {
77
for (int j = 0; j < h; j++) {
78
int color1 = image1.getRGB(i, j);
79
int color2 = image2.getRGB(i, j);
80
81
if (color1 != color2) {
82
return false;
83
}
84
}
85
}
86
return true;
87
}
88
89
static BufferedImage getHiDPIImage(JComponent component) {
90
return getImage(component, 2, IMAGE_WIDTH, IMAGE_HEIGHT);
91
}
92
93
static BufferedImage getScaledImage(JComponent component) {
94
Image image1x = getImage(component, 1, IMAGE_WIDTH, IMAGE_HEIGHT);
95
final BufferedImage image2x = new BufferedImage(
96
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
97
final Graphics g = image2x.getGraphics();
98
((Graphics2D) g).scale(2, 2);
99
g.drawImage(image1x, 0, 0, null);
100
g.dispose();
101
return image2x;
102
}
103
104
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
105
final BufferedImage image = new BufferedImage(
106
scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
107
final Graphics g = image.getGraphics();
108
((Graphics2D) g).scale(scale, scale);
109
component.paint(g);
110
g.dispose();
111
return image;
112
}
113
}
114
115