21
21
* questions.
22
22
*/
23
23
24
-
25
-
26
24
import java .awt .Color ;
27
25
import java .awt .Dimension ;
28
26
import java .awt .EventQueue ;
44
42
* @key headful
45
43
* @bug 6988428
46
44
* @summary Tests whether shape is always set
47
- * @run main ShapeNotSetSometimes
45
+ * @run main/othervm -Dsun.java2d.uiScale=1 ShapeNotSetSometimes
48
46
*/
49
47
50
48
public class ShapeNotSetSometimes {
@@ -55,22 +53,24 @@ public class ShapeNotSetSometimes {
55
53
private Point [] pointsOutsideToCheck ;
56
54
private Point [] shadedPointsToCheck ;
57
55
private Point innerPoint ;
58
- private final Rectangle bounds = new Rectangle (220 , 400 , 300 , 300 );
59
56
60
57
private static Robot robot ;
61
58
private static final Color BACKGROUND_COLOR = Color .GREEN ;
62
59
private static final Color SHAPE_COLOR = Color .WHITE ;
60
+ private static final int DIM = 300 ;
61
+ private static final int DELTA = 2 ;
63
62
64
63
public ShapeNotSetSometimes () throws Exception {
65
64
EventQueue .invokeAndWait (this ::initializeGUI );
66
65
robot .waitForIdle ();
67
- robot .delay (1000 );
66
+ robot .delay (500 );
68
67
}
69
68
70
69
private void initializeGUI () {
71
70
backgroundFrame = new BackgroundFrame ();
72
71
backgroundFrame .setUndecorated (true );
73
- backgroundFrame .setBounds (bounds );
72
+ backgroundFrame .setSize (DIM , DIM );
73
+ backgroundFrame .setLocationRelativeTo (null );
74
74
backgroundFrame .setVisible (true );
75
75
76
76
Area area = new Area ();
@@ -81,23 +81,27 @@ private void initializeGUI() {
81
81
area .add (new Area (new Ellipse2D .Float (150 , 50 , 100 , 100 )));
82
82
area .add (new Area (new Ellipse2D .Float (150 , 100 , 100 , 100 )));
83
83
84
-
84
+ // point at the center of white ellipse
85
85
innerPoint = new Point (150 , 130 );
86
+
87
+ // mid points on the 4 sides - on the green background frame
86
88
pointsOutsideToCheck = new Point [] {
87
89
new Point (150 , 20 ),
88
90
new Point (280 , 120 ),
89
91
new Point (150 , 250 ),
90
92
new Point (20 , 120 )
91
93
};
92
94
95
+ // points just outside the ellipse (opposite side of diagonal)
93
96
shadedPointsToCheck = new Point [] {
94
97
new Point (62 , 62 ),
95
98
new Point (240 , 185 )
96
99
};
97
100
98
101
window = new TestFrame ();
99
102
window .setUndecorated (true );
100
- window .setBounds (bounds );
103
+ window .setSize (DIM , DIM );
104
+ window .setLocationRelativeTo (null );
101
105
window .setShape (area );
102
106
window .setVisible (true );
103
107
}
@@ -108,7 +112,7 @@ static class BackgroundFrame extends Frame {
108
112
public void paint (Graphics g ) {
109
113
110
114
g .setColor (BACKGROUND_COLOR );
111
- g .fillRect (0 , 0 , 300 , 300 );
115
+ g .fillRect (0 , 0 , DIM , DIM );
112
116
113
117
super .paint (g );
114
118
}
@@ -120,7 +124,7 @@ class TestFrame extends Frame {
120
124
public void paint (Graphics g ) {
121
125
122
126
g .setColor (SHAPE_COLOR );
123
- g .fillRect (0 , 0 , bounds . width , bounds . height );
127
+ g .fillRect (0 , 0 , DIM , DIM );
124
128
125
129
super .paint (g );
126
130
}
@@ -155,17 +159,24 @@ private void doTest() throws Exception {
155
159
}
156
160
} finally {
157
161
EventQueue .invokeAndWait (() -> {
158
- backgroundFrame .dispose ();
159
- window .dispose ();
162
+ if (backgroundFrame != null ) {
163
+ backgroundFrame .dispose ();
164
+ }
165
+ if (window != null ) {
166
+ window .dispose ();
167
+ }
160
168
});
161
169
}
162
170
}
163
171
164
172
private void colorCheck (int x , int y , Color expectedColor , boolean mustBeExpectedColor ) {
165
-
166
173
int screenX = window .getX () + x ;
167
174
int screenY = window .getY () + y ;
168
175
176
+ robot .mouseMove (screenX , screenY );
177
+ robot .waitForIdle ();
178
+ robot .delay (50 );
179
+
169
180
Color actualColor = robot .getPixelColor (screenX , screenY );
170
181
171
182
System .out .printf (
@@ -176,7 +187,7 @@ private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpecte
176
187
expectedColor
177
188
);
178
189
179
- if (mustBeExpectedColor != expectedColor . equals ( actualColor )) {
190
+ if (mustBeExpectedColor != colorCompare ( expectedColor , actualColor )) {
180
191
captureScreen ();
181
192
System .out .printf ("window.getX() = %3d, window.getY() = %3d\n " , window .getX (), window .getY ());
182
193
System .err .printf (
@@ -190,6 +201,15 @@ private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpecte
190
201
}
191
202
}
192
203
204
+ private static boolean colorCompare (Color expected , Color actual ) {
205
+ if (Math .abs (expected .getRed () - actual .getRed ()) <= DELTA
206
+ && Math .abs (expected .getGreen () - actual .getGreen ()) <= DELTA
207
+ && Math .abs (expected .getBlue () - actual .getBlue ()) <= DELTA ) {
208
+ return true ;
209
+ }
210
+ return false ;
211
+ }
212
+
193
213
private static void captureScreen () {
194
214
Dimension screenSize = Toolkit .getDefaultToolkit ().getScreenSize ();
195
215
Rectangle screenBounds = new Rectangle (0 , 0 , screenSize .width , screenSize .height );
0 commit comments