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/JWindow/ShapedAndTranslucentWindows/Common.java
38853 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
24
import javax.swing.*;
25
import javax.swing.border.EmptyBorder;
26
import java.awt.*;
27
import java.awt.event.*;
28
import java.awt.geom.Area;
29
import java.awt.geom.Rectangle2D;
30
import java.awt.image.BufferedImage;
31
import java.security.SecureRandom;
32
33
public abstract class Common {
34
35
ExtendedRobot robot;
36
Class<? extends JFrame> windowClass;
37
JFrame background;
38
BufferedImage foreground;
39
Window window;
40
volatile boolean gradientBackgroundEnabled = false;
41
volatile int gradientWidth = 255;
42
volatile int gradientHeight = 255;
43
44
float opacity = 1.0f;
45
float perPixelTranslucency = 1.0f;
46
static Color BG_COLOR = Color.BLUE;
47
static Color FG_COLOR = Color.RED;
48
static final int delay = 1000;
49
static final SecureRandom random = new SecureRandom();
50
static final int dl = 100;
51
static final Class[] WINDOWS_TO_TEST = { JWindow.class, JFrame.class, JDialog.class };
52
53
volatile int clicked;
54
55
public Common(Class windowClass, float opacity, float perPixelTranslucency, boolean gradient) throws Exception {
56
this.gradientBackgroundEnabled = gradient;
57
this.perPixelTranslucency = perPixelTranslucency;
58
this.opacity = opacity;
59
robot = new ExtendedRobot();
60
this.windowClass = windowClass;
61
EventQueue.invokeAndWait(this::initBackgroundFrame);
62
EventQueue.invokeAndWait(this::initGUI);
63
}
64
65
public Common(Class windowClass) throws Exception {
66
this(windowClass, 1.0f, 1.0f, false);
67
}
68
69
public Common(Class windowClass, boolean gradient) throws Exception {
70
this(windowClass, 1.0f, 1.0f, gradient);
71
}
72
73
public abstract void doTest() throws Exception;
74
75
public void dispose() {
76
window.dispose();
77
background.dispose();
78
}
79
80
public void applyShape() {};
81
82
public void applyDynamicShape() {
83
final Area a = new Area();
84
Dimension size = window.getSize();
85
for (int x = 0; x < 3; x++) {
86
for (int y = 0; y < 3; y++) {
87
a.add(new Area(new Rectangle2D.Double(
88
x * size.getWidth() / 17*6, y * size.getHeight() / 17*6,
89
size.getWidth() / 17*5, size.getHeight() / 17*5)));
90
}
91
}
92
window.setShape(a);
93
}
94
95
public BufferedImage getForegroundWindow() throws Exception {
96
final BufferedImage f[] = new BufferedImage[1];
97
EventQueue.invokeAndWait( () -> {
98
f[0] = new BufferedImage(window.getWidth(),
99
window.getHeight(), BufferedImage.TYPE_INT_RGB);
100
window.printAll(f[0].createGraphics());
101
});
102
robot.waitForIdle(delay);
103
return f[0];
104
}
105
106
public static boolean checkTranslucencyMode(GraphicsDevice.WindowTranslucency mode) {
107
108
if (!GraphicsEnvironment
109
.getLocalGraphicsEnvironment()
110
.getDefaultScreenDevice()
111
.isWindowTranslucencySupported(mode)){
112
System.out.println(mode+" translucency mode isn't supported");
113
return false;
114
} else {
115
return true;
116
}
117
118
}
119
120
public void applyAppDragNResizeSupport() {
121
MouseAdapter m = new MouseAdapter() {
122
123
private Point dragOrigin = null;
124
private Dimension origSize = null;
125
private Point origLoc = null;
126
private boolean left = false;
127
private boolean top = false;
128
private boolean bottom = false;
129
private boolean right = false;
130
131
public void mousePressed(MouseEvent e) {
132
dragOrigin = e.getLocationOnScreen();
133
origSize = window.getSize();
134
origLoc = window.getLocationOnScreen();
135
right = (origLoc.x + window.getWidth() - dragOrigin.x) < 5;
136
left = !right && dragOrigin.x - origLoc.x < 5;
137
bottom = (origLoc.y + window.getHeight() - dragOrigin.y) < 5;
138
top = !bottom && dragOrigin.y - origLoc.y < 5;
139
}
140
141
public void mouseReleased(MouseEvent e) { resize(e); }
142
public void mouseDragged(MouseEvent e) { resize(e); }
143
144
void resize(MouseEvent e) {
145
Point dragDelta = e.getLocationOnScreen();
146
dragDelta.translate(-dragOrigin.x, -dragOrigin.y);
147
Point newLoc = new Point(origLoc);
148
newLoc.translate(dragDelta.x, dragDelta.y);
149
Dimension newSize = new Dimension(origSize);
150
if (left || right) {
151
newSize.width += right ? dragDelta.x : -dragDelta.x;
152
}
153
if (top || bottom) {
154
newSize.height += bottom ? dragDelta.y : -dragDelta.y;
155
}
156
if (right || (top || bottom) && !left) {
157
newLoc.x = origLoc.x;
158
}
159
if (bottom || (left || right) && !top) {
160
newLoc.y = origLoc.y;
161
}
162
window.setBounds(newLoc.x, newLoc.y, newSize.width, newSize.height);
163
}
164
};
165
for (Component comp : window.getComponents()) {
166
comp.addMouseListener(m);
167
comp.addMouseMotionListener(m);
168
}
169
170
window.addMouseListener(m);
171
window.addMouseMotionListener(m);
172
}
173
174
public void checkTranslucentShape() throws Exception {
175
foreground = getForegroundWindow();
176
Point[] points = new Point[4];
177
178
Dimension size = window.getSize();
179
Point location = window.getLocationOnScreen();
180
181
points[0] = new Point(20, 20);
182
points[1] = new Point(20, size.height-20);
183
points[2] = new Point(size.width-20, 20);
184
points[3] = new Point(size.width-20, size.height-20);
185
186
for (Point p : points){
187
p.translate(location.x, location.y);
188
Color actual = robot.getPixelColor(p.x, p.y);
189
if (actual.equals(BG_COLOR)|| actual.equals(FG_COLOR))
190
throw new RuntimeException("Error in point "+p+": "+actual+" equals to foreground or background color");
191
else
192
System.out.println("OK with foreground point "+p);
193
}
194
}
195
196
public void checkDynamicShape() throws Exception {
197
Point[] points = new Point[4];
198
199
Dimension size = window.getSize();
200
201
int blockSizeX = (int) (size.getWidth() / 17);
202
int blockSizeY = (int) (size.getHeight() / 17);
203
204
// background
205
points[0] = new Point((int) (blockSizeX * 5.5), (int) (blockSizeY * 5.5));
206
points[1] = new Point((int) (size.getWidth() - blockSizeX * 5.5), (int) (size.getHeight() - blockSizeY * 5.5));
207
points[2] = new Point((int) (blockSizeX * 5.5), (int) (size.getHeight() - blockSizeY * 5.5));
208
points[3] = new Point((int) (size.getWidth() - blockSizeX * 5.5), (int) (blockSizeY * 5.5));
209
checkShape(points, true);
210
211
// foreground
212
if (opacity < 1.0f){
213
checkTranslucentShape();
214
} else {
215
points[0] = new Point(3 * blockSizeX, 3 * blockSizeY);
216
points[1] = new Point(14 * blockSizeX, 14 * blockSizeY);
217
points[2] = new Point(3 * blockSizeX, 14 * blockSizeY);
218
points[3] = new Point(14 * blockSizeX, 3 * blockSizeY);
219
checkShape(points, false);
220
}
221
}
222
223
public void checkShape(Point[] points, boolean areBackgroundPoints) throws Exception {
224
225
Point location = window.getLocationOnScreen();
226
227
for (Point p : points) {
228
p.translate(location.x, location.y);
229
Color pixel = robot.getPixelColor(p.x, p.y);
230
if (areBackgroundPoints) {
231
if (pixel.getRed() != 0
232
|| pixel.getGreen() != 0 )
233
throw new RuntimeException("Background point " + p +
234
" color " + pixel +
235
" does not equal to background color " + BG_COLOR);
236
else
237
System.out.println("OK with background point " + p);
238
} else {
239
if (pixel.equals(BG_COLOR))
240
throw new RuntimeException("Foreground point " + p +
241
" color " + pixel +
242
" equals to background color " + BG_COLOR);
243
else
244
System.out.println("OK with foreground point " + p);
245
}
246
}
247
}
248
249
public void initBackgroundFrame() {
250
background = new JFrame();
251
background.setUndecorated(true);
252
background.getContentPane().setBackground(BG_COLOR);
253
background.setSize(500, 500);
254
background.setLocation(dl, dl);
255
background.setVisible(true);
256
}
257
258
public void initGUI() {
259
Container contentPane;
260
if (windowClass.equals(Frame.class)) {
261
window = new JFrame();
262
((JFrame) window).setUndecorated(true);
263
contentPane = ((JFrame) window).getContentPane();
264
} else if (windowClass.equals(Dialog.class)) {
265
window = new JDialog(background);
266
((JDialog) window).setUndecorated(true);
267
contentPane = ((JDialog) window).getContentPane();
268
} else {
269
window = new JWindow(background);
270
contentPane = ((JWindow) window).getContentPane();
271
}
272
273
if (perPixelTranslucency < 1.0f) {
274
contentPane.setBackground(colorWithOpacity(FG_COLOR, perPixelTranslucency));
275
window.setBackground(colorWithOpacity(FG_COLOR, perPixelTranslucency));
276
} else {
277
contentPane.setBackground(FG_COLOR);
278
window.setBackground(FG_COLOR);
279
}
280
281
window.setLocation(2 * dl, 2 * dl);
282
window.setSize(255, 255);
283
window.setPreferredSize(new Dimension(255, 255));
284
createSwingComponents();
285
if (opacity < 1.0f)
286
window.setOpacity(opacity);
287
288
window.addComponentListener(new ComponentAdapter() {
289
@Override
290
public void componentResized(ComponentEvent e) {
291
applyShape();
292
}
293
});
294
applyShape();
295
window.setVisible(true);
296
applyAppDragNResizeSupport();
297
window.toFront();
298
}
299
300
public void createSwingComponents() {
301
Container contentPane;
302
if (gradientBackgroundEnabled) {
303
JPanel jPanel = new JPanel() {
304
@Override
305
protected void paintComponent(Graphics g) {
306
if (g instanceof Graphics2D) {
307
Color background = Color.RED;
308
Paint p = new GradientPaint(0.0f, 0.0f, colorWithOpacity(background, 0),
309
0.0f, gradientHeight - 3, colorWithOpacity(background, 1), true);
310
Graphics2D g2d = (Graphics2D) g;
311
g2d.setPaint(p);
312
g2d.fillRect(0, 3, gradientWidth, gradientHeight - 3);
313
} else {
314
super.paintComponent(g);
315
}
316
}
317
};
318
jPanel.setBorder(new EmptyBorder(15, 5, 5, 5));
319
jPanel.setOpaque(false);
320
321
contentPane = jPanel;
322
323
RootPaneContainer.class.cast(window).setContentPane(contentPane);
324
} else {
325
contentPane = RootPaneContainer.class.cast(window).getContentPane();
326
}
327
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
328
329
JButton button = new JButton("JButton");
330
window.add(button);
331
332
JTextArea textArea = new JTextArea("JTextArea");
333
window.add(textArea);
334
335
JCheckBox checkbox = new JCheckBox("JCheckBox");
336
checkbox.setOpaque(false);
337
window.add(checkbox);
338
339
JComboBox comboBox = new JComboBox(new String[]{"JComboBox", "Some item"});
340
window.add(comboBox);
341
342
JLabel label = new JLabel("JLabel");
343
window.add(label);
344
345
JTextField textField = new JTextField("JTextField");
346
window.add(textField);
347
348
JPanel panel = new JPanel();
349
panel.setOpaque(false);
350
window.add(panel);
351
352
JComboBox comboBox2 = new JComboBox(new String[]{"JComboBox2", "Another item"});
353
window.add(comboBox2);
354
355
JRadioButton radioButton = new JRadioButton("JRadioButton");
356
radioButton.setOpaque(false);
357
window.add(radioButton);
358
}
359
360
Color colorWithOpacity(Color color, float opacity) {
361
return new Color(color.getColorSpace(), color.getColorComponents(null), opacity);
362
}
363
364
public void checkTranslucent() throws Exception {
365
checkTranslucentShape();
366
367
// Drag
368
Point location = window.getLocationOnScreen();
369
robot.dragAndDrop(location.x + 30, location.y + 5, location.x + dl + random.nextInt(dl), location.y + random.nextInt(dl));
370
robot.waitForIdle(delay);
371
checkTranslucentShape();
372
373
// Resize
374
location = window.getLocationOnScreen();
375
robot.dragAndDrop(location.x + 4, location.y + 4, location.x + random.nextInt(2*dl)-dl, location.y + random.nextInt(2*dl)-dl);
376
robot.waitForIdle(delay);
377
checkTranslucentShape();
378
379
EventQueue.invokeAndWait(this::dispose);
380
}
381
382
public void checkDynamic() throws Exception {
383
checkDynamicShape();
384
385
// Drag
386
Point location = window.getLocationOnScreen();
387
robot.dragAndDrop(location.x + 30, location.y + 5, location.x + dl + random.nextInt(dl), location.y + random.nextInt(dl));
388
robot.waitForIdle(delay);
389
checkDynamicShape();
390
391
// Resize
392
location = window.getLocationOnScreen();
393
robot.dragAndDrop(location.x + 4, location.y + 4, location.x + random.nextInt(2*dl)-dl, location.y + random.nextInt(2*dl)-dl);
394
robot.waitForIdle(delay);
395
checkDynamicShape();
396
397
EventQueue.invokeAndWait(this::dispose);
398
}
399
400
void checkClick(int x, int y, int flag) throws Exception {
401
402
System.out.println("Trying to click point " + x + ", " + y + ", looking for " + flag + " flag to trigger.");
403
404
clicked = 0;
405
robot.mouseMove(x, y);
406
robot.click();
407
408
for (int i = 0; i < 100; i++)
409
if ((clicked & (1 << flag)) == 0)
410
robot.delay(50);
411
else
412
break;
413
414
if ((clicked & (1 << flag)) == 0)
415
throw new RuntimeException("FAIL: Flag " + flag + " is not triggered for point " + x + ", " + y + "!");
416
}
417
}
418
419