11/*
2- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3131*/
3232
3333
34- import java .awt .*;
35- import java .awt .event .InputEvent ;
36- import java .awt .geom .*;
34+ import java .awt .Color ;
35+ import java .awt .EventQueue ;
36+ import java .awt .Frame ;
37+ import java .awt .Graphics ;
38+ import java .awt .Point ;
39+ import java .awt .Rectangle ;
40+ import java .awt .Robot ;
41+ import java .awt .geom .Area ;
42+ import java .awt .geom .Ellipse2D ;
43+ import java .awt .geom .Rectangle2D ;
3744
3845
3946public class ShapeNotSetSometimes {
4047
4148 private Frame backgroundFrame ;
4249 private Frame window ;
43- private static final Color BACKGROUND_COLOR = Color .BLUE ;
44- private Shape shape ;
45- private int [][] pointsToCheck ;
50+ private static final Color BACKGROUND_COLOR = Color .GREEN ;
51+ private static final Color SHAPE_COLOR = Color .WHITE ;
52+ private Point [] pointsOutsideToCheck ;
53+ private Point [] shadedPointsToCheck ;
54+ private Point innerPoint ;
55+
56+ private final Rectangle bounds = new Rectangle (220 , 400 , 300 , 300 );
4657
4758 private static Robot robot ;
4859
@@ -54,43 +65,39 @@ public ShapeNotSetSometimes() throws Exception {
5465 private void initializeGUI () {
5566 backgroundFrame = new BackgroundFrame ();
5667 backgroundFrame .setUndecorated (true );
57- backgroundFrame .setSize (300 , 300 );
58- backgroundFrame .setLocation (20 , 400 );
68+ backgroundFrame .setBounds (bounds );
5969 backgroundFrame .setVisible (true );
6070
61- shape = null ;
62- String shape_name = null ;
63- Area a ;
64- GeneralPath gp ;
65- shape_name = "Rounded-corners" ;
66- a = new Area ();
67- a .add (new Area (new Rectangle2D .Float (50 , 0 , 100 , 150 )));
68- a . add ( new Area ( new Rectangle2D . Float ( 0 , 50 , 200 , 50 )));
69- a . add ( new Area ( new Ellipse2D . Float ( 0 , 0 , 100 , 100 )));
70- a . add ( new Area ( new Ellipse2D . Float ( 0 , 50 , 100 , 100 )) );
71- a . add ( new Area ( new Ellipse2D . Float ( 100 , 0 , 100 , 100 )));
72- a . add ( new Area ( new Ellipse2D . Float ( 100 , 50 , 100 , 100 )));
73- shape = a ;
74- pointsToCheck = new int [][] {
75- // inside shape
76- { 106 , 86 }, { 96 , 38 }, { 76 , 107 }, { 180 , 25 }, { 24 , 105 },
77- { 196 , 77 }, { 165 , 50 }, { 14 , 113 }, { 89 , 132 }, { 167 , 117 },
78- // outside shape
79- { 165 , 196 }, { 191 , 163 }, { 146 , 185 }, { 61 , 170 }, { 148 , 171 } ,
80- { 82 , 172 }, { 186 , 11 }, { 199 , 141 }, { 13 , 173 }, { 187 , 3 }
71+ Area area = new Area () ;
72+ area . add ( new Area ( new Rectangle2D . Float ( 100 , 50 , 100 , 150 ))) ;
73+ area . add ( new Area ( new Rectangle2D . Float ( 50 , 100 , 200 , 50 ))) ;
74+ area . add ( new Area ( new Ellipse2D . Float ( 50 , 50 , 100 , 100 ))) ;
75+ area . add ( new Area ( new Ellipse2D . Float ( 50 , 100 , 100 , 100 ))) ;
76+ area . add ( new Area (new Ellipse2D . Float ( 150 , 50 , 100 , 100 )) );
77+ area .add (new Area (new Ellipse2D .Float (150 , 100 , 100 , 100 )));
78+
79+
80+ innerPoint = new Point ( 150 , 130 );
81+ pointsOutsideToCheck = new Point [] {
82+ new Point ( 150 , 20 ),
83+ new Point ( 280 , 120 ),
84+ new Point ( 150 , 250 ),
85+ new Point ( 20 , 120 )
86+ };
87+
88+ shadedPointsToCheck = new Point [] {
89+ new Point ( 62 , 62 ) ,
90+ new Point ( 240 , 185 )
8191 };
8292
8393 window = new TestFrame ();
8494 window .setUndecorated (true );
85- window .setSize (200 , 200 );
86- window .setLocation (70 , 450 );
87- window .setShape (shape );
95+ window .setBounds (bounds );
96+ window .setShape (area );
8897 window .setVisible (true );
89-
90- System .out .println ("Checking " + window .getClass ().getSuperclass ().getName () + " with " + shape_name + " shape (" + window .getShape () + ")..." );
9198 }
9299
93- class BackgroundFrame extends Frame {
100+ static class BackgroundFrame extends Frame {
94101
95102 @ Override
96103 public void paint (Graphics g ) {
@@ -107,8 +114,8 @@ class TestFrame extends Frame {
107114 @ Override
108115 public void paint (Graphics g ) {
109116
110- g .setColor (Color . WHITE );
111- g .fillRect (0 , 0 , 200 , 200 );
117+ g .setColor (SHAPE_COLOR );
118+ g .fillRect (0 , 0 , bounds . width , bounds . height );
112119
113120 super .paint (g );
114121 }
@@ -124,48 +131,58 @@ public static void main(String[] args) throws Exception {
124131 }
125132
126133 private void doTest () throws Exception {
127- Point wls = backgroundFrame .getLocationOnScreen ();
134+ EventQueue .invokeAndWait (backgroundFrame ::toFront );
135+ robot .waitForIdle ();
128136
129- robot .mouseMove (wls .x + 5 , wls .y + 5 );
130- robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
131- robot .delay (10 );
132- robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
133- robot .delay (500 );
137+ EventQueue .invokeAndWait (window ::toFront );
138+ robot .waitForIdle ();
134139
135- EventQueue . invokeAndWait ( window :: requestFocus );
140+ robot . delay ( 500 );
136141
137- robot .waitForIdle ();
138142 try {
139- Thread .sleep (300 );
140- } catch (InterruptedException e ) {
141- // ignore this one
142- }
143+ colorCheck (innerPoint .x , innerPoint .y , SHAPE_COLOR , true );
143144
144- // check transparency
145- final int COUNT_TARGET = 10 ;
146-
147- // checking outside points only
148- for (int i = COUNT_TARGET ; i < COUNT_TARGET * 2 ; i ++) {
149- int x = pointsToCheck [i ][0 ];
150- int y = pointsToCheck [i ][1 ];
151- boolean inside = i < COUNT_TARGET ;
152- Color c = robot .getPixelColor (window .getX () + x , window .getY () + y );
153- System .out .println ("checking " + x + ", " + y + ", color = " + c );
154- if (inside && BACKGROUND_COLOR .equals (c ) || !inside && !BACKGROUND_COLOR .equals (c )) {
155- System .out .println ("window.getX() = " + window .getX () + ", window.getY() = " + window .getY ());
156- System .err .println ("Checking for transparency failed: point: " +
157- (window .getX () + x ) + ", " + (window .getY () + y ) +
158- ", color = " + c + (inside ? " is of un" : " is not of " ) +
159- "expected background color " + BACKGROUND_COLOR );
160- throw new RuntimeException ("Test failed. The shape has not been applied." );
145+ for (Point point : pointsOutsideToCheck ) {
146+ colorCheck (point .x , point .y , BACKGROUND_COLOR , true );
161147 }
162- }
163148
164- EventQueue .invokeAndWait (new Runnable () {
165- public void run () {
149+ for (Point point : shadedPointsToCheck ) {
150+ colorCheck (point .x , point .y , SHAPE_COLOR , false );
151+ }
152+ } finally {
153+ EventQueue .invokeAndWait (() -> {
166154 backgroundFrame .dispose ();
167155 window .dispose ();
168- }
169- });
156+ });
157+ }
158+ }
159+
160+ private void colorCheck (int x , int y , Color expectedColor , boolean mustBeExpectedColor ) {
161+
162+ int screenX = window .getX () + x ;
163+ int screenY = window .getY () + y ;
164+
165+ Color actualColor = robot .getPixelColor (screenX , screenY );
166+
167+ System .out .printf (
168+ "Checking %3d, %3d, %35s should %sbe %35s\n " ,
169+ x , y ,
170+ actualColor ,
171+ (mustBeExpectedColor ) ? "" : "not " ,
172+ expectedColor
173+ );
174+
175+ if (mustBeExpectedColor != expectedColor .equals (actualColor )) {
176+ System .out .printf ("window.getX() = %3d, window.getY() = %3d\n " , window .getX (), window .getY ());
177+
178+ System .err .printf (
179+ "Checking for transparency failed: point: %3d, %3d\n \t actual %s\n \t expected %s%s\n " ,
180+ screenX ,
181+ screenY ,
182+ actualColor ,
183+ mustBeExpectedColor ? "" : "not " ,
184+ expectedColor );
185+ throw new RuntimeException ("Test failed. The shape has not been applied." );
186+ }
170187 }
171188}
0 commit comments