Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JButton/8151303/PressedIconTest.java
41153 views
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
24
import java.awt.BorderLayout;
25
import java.awt.Color;
26
import java.awt.Dimension;
27
import java.awt.Graphics;
28
import java.awt.Point;
29
import java.awt.Robot;
30
import java.awt.event.InputEvent;
31
import java.awt.image.BaseMultiResolutionImage;
32
import java.awt.image.BufferedImage;
33
import javax.swing.Icon;
34
import javax.swing.ImageIcon;
35
import javax.swing.JFrame;
36
import javax.swing.JPanel;
37
import javax.swing.JToggleButton;
38
import javax.swing.SwingUtilities;
39
40
/**
41
* @test
42
* @key headful
43
* @bug 8151303
44
* @summary [macosx] [hidpi] JButton's low-res. icon is visible when clicking on it
45
* @run main/othervm PressedIconTest
46
* @run main/othervm -Dsun.java2d.uiScale=2 PressedIconTest
47
*/
48
49
public class PressedIconTest {
50
51
private final static int IMAGE_SIZE = 300;
52
53
private final static Color COLOR_1X = Color.RED;
54
private final static Color COLOR_2X = Color.BLUE;
55
private static JFrame frame;
56
private static volatile double scale = -1;
57
private static volatile int centerX;
58
private static volatile int centerY;
59
60
public static void main(String[] args) throws Exception {
61
Robot robot = new Robot();
62
robot.setAutoDelay(50);
63
64
SwingUtilities.invokeAndWait(() -> createAndShowGUI());
65
robot.waitForIdle();
66
67
SwingUtilities.invokeAndWait(() -> {
68
scale = frame.getGraphicsConfiguration().getDefaultTransform()
69
.getScaleX();
70
Point location = frame.getLocation();
71
Dimension size = frame.getSize();
72
centerX = location.x + size.width / 2;
73
centerY = location.y + size.height / 2;
74
});
75
robot.waitForIdle();
76
77
robot.mouseMove(centerX, centerY);
78
robot.mousePress(InputEvent.BUTTON1_MASK);
79
robot.waitForIdle();
80
Thread.sleep(100);
81
Color color = robot.getPixelColor(centerX, centerY);
82
robot.mouseRelease(InputEvent.BUTTON1_MASK);
83
84
SwingUtilities.invokeAndWait(() -> frame.dispose());
85
86
if ((scale == 1 && !similar(color, COLOR_1X))
87
|| (scale == 2 && !similar(color, COLOR_2X))) {
88
throw new RuntimeException("Colors are different!");
89
}
90
}
91
92
private static void createAndShowGUI() {
93
frame = new JFrame();
94
frame.setSize(IMAGE_SIZE, IMAGE_SIZE);
95
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
96
97
JPanel panel = new JPanel(new BorderLayout());
98
99
BufferedImage img1x = generateImage(1, COLOR_1X);
100
101
BufferedImage img2x = generateImage(2, COLOR_2X);
102
BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
103
new BufferedImage[]{img1x, img2x});
104
Icon mrIcon = new ImageIcon(mri);
105
106
JToggleButton button = new JToggleButton();
107
button.setIcon(mrIcon);
108
panel.add(button, BorderLayout.CENTER);
109
110
frame.getContentPane().add(panel);
111
frame.setVisible(true);
112
}
113
114
private static boolean similar(Color c1, Color c2) {
115
return similar(c1.getRed(), c2.getRed())
116
&& similar(c1.getGreen(), c2.getGreen())
117
&& similar(c1.getBlue(), c2.getBlue());
118
}
119
120
private static boolean similar(int n, int m) {
121
return Math.abs(n - m) <= 50;
122
}
123
124
private static BufferedImage generateImage(int scale, Color c) {
125
126
int size = IMAGE_SIZE * scale;
127
BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
128
Graphics g = img.createGraphics();
129
g.setColor(c);
130
g.fillRect(0, 0, size, size);
131
g.dispose();
132
return img;
133
}
134
}
135
136