Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JMenuItem/6209975/bug6209975.java
41153 views
1
/*
2
* Copyright (c) 2012, 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
/*
25
* @test
26
* @key headful
27
* @bug 6209975
28
* @summary regression: JMenuItem icons overimposed on JMenuItem labels under Metal LAF
29
* @author Alexander Zuev
30
* @run main bug6209975
31
*/
32
import javax.swing.*;
33
import java.awt.*;
34
import java.awt.event.InputEvent;
35
36
public class bug6209975 {
37
38
private static final ReturnObject RO1 = new ReturnObject();
39
private static final ReturnObject RO2 = new ReturnObject();
40
41
private static JMenu menu;
42
private static JButton button;
43
private static JFrame frame;
44
45
public static void main(String[] args) throws Exception {
46
try {
47
Robot robot = new Robot();
48
robot.setAutoDelay(500);
49
50
51
SwingUtilities.invokeAndWait(new Runnable() {
52
53
@Override
54
public void run() {
55
createAndShowGUI();
56
}
57
});
58
59
robot.waitForIdle();
60
61
Point clickPoint = getButtonClickPoint();
62
robot.mouseMove(clickPoint.x, clickPoint.y);
63
robot.mousePress(InputEvent.BUTTON1_MASK);
64
robot.mouseRelease(InputEvent.BUTTON1_MASK);
65
robot.waitForIdle();
66
67
clickPoint = getMenuClickPoint();
68
robot.mouseMove(clickPoint.x, clickPoint.y);
69
robot.mousePress(InputEvent.BUTTON1_MASK);
70
robot.mouseRelease(InputEvent.BUTTON1_MASK);
71
robot.waitForIdle();
72
73
if (RO1.itsValue <= RO2.itsValue) {
74
throw new RuntimeException("Offset if the second icon is invalid.");
75
}
76
} finally {
77
if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
78
}
79
}
80
81
private static Point getButtonClickPoint() throws Exception {
82
final Point[] result = new Point[1];
83
84
SwingUtilities.invokeAndWait(new Runnable() {
85
86
@Override
87
public void run() {
88
Point p = button.getLocationOnScreen();
89
Dimension size = button.getSize();
90
result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
91
}
92
});
93
return result[0];
94
}
95
96
private static Point getMenuClickPoint() throws Exception {
97
final Point[] result = new Point[1];
98
99
SwingUtilities.invokeAndWait(new Runnable() {
100
101
@Override
102
public void run() {
103
Point p = menu.getLocationOnScreen();
104
Dimension size = menu.getSize();
105
result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
106
}
107
});
108
return result[0];
109
}
110
111
private static void createAndShowGUI() {
112
frame = new JFrame("Test6209975");
113
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
114
frame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
115
frame.setLayout(new BorderLayout());
116
button = new JButton("Focus holder");
117
frame.add(button);
118
119
JMenuBar mb = new JMenuBar();
120
menu = new JMenu("File");
121
122
JMenuItem item;
123
124
item = new JMenuItem("Just a menu item");
125
item.setIcon(new MyIcon(RO1));
126
item.setHorizontalTextPosition(SwingConstants.LEADING);
127
menu.add(item);
128
129
item = new JMenuItem("Menu Item with another icon");
130
item.setIcon(new MyIcon(RO2));
131
item.setHorizontalTextPosition(SwingConstants.TRAILING);
132
menu.add(item);
133
134
mb.add(menu);
135
136
frame.setJMenuBar(mb);
137
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
138
frame.pack();
139
frame.setLocation(400, 300);
140
frame.setVisible(true);
141
}
142
143
public static class ReturnObject {
144
145
public volatile int itsValue;
146
}
147
148
public static class MyIcon implements Icon {
149
150
ReturnObject thisObject = null;
151
152
public MyIcon(ReturnObject ro) {
153
super();
154
thisObject = ro;
155
}
156
157
public void paintIcon(Component c, Graphics g, int x, int y) {
158
Color color = g.getColor();
159
g.setColor(Color.BLACK);
160
g.fillRect(x, y, 10, 10);
161
g.setColor(color);
162
thisObject.itsValue = x;
163
}
164
165
public int getIconWidth() {
166
return 10;
167
}
168
169
public int getIconHeight() {
170
return 10;
171
}
172
}
173
}
174
175