46
46
public class CheckboxCheckerScalingTest {
47
47
private static Frame frame ;
48
48
private static Checkbox checkbox ;
49
- private static BufferedImage imageAfterChecked ;
50
- private static volatile boolean checkmarkFound = false ;
49
+ private static volatile Point point ;
50
+ private static boolean checkmarkFound = false ;
51
+ private static final int TOLERANCE = 5 ;
52
+ private static final int COLOR_CHECK_THRESHOLD = 8 ;
53
+ private static int colorCounter = 0 ;
51
54
52
55
public static void main (String [] args ) throws Exception {
53
56
System .setProperty ("sun.java2d.uiScale" , "2" );
54
57
Robot robot = new Robot ();
58
+
55
59
try {
56
60
EventQueue .invokeAndWait (() -> {
57
- frame = new Frame ("ComboBox checker scaling test" );
61
+ frame = new Frame ("CheckBox checker scaling test" );
58
62
checkbox = new Checkbox ("one" );
59
63
checkbox .setState (true );
60
64
frame .add (checkbox );
65
+ frame .setLocationRelativeTo (null );
61
66
frame .pack ();
62
67
frame .setVisible (true );
63
68
});
64
69
65
70
robot .waitForIdle ();
66
71
robot .delay (100 );
67
- EventQueue .invokeAndWait (() -> {
68
- Point point = checkbox .getLocationOnScreen ();
69
- Rectangle rect = new Rectangle (point .x + 5 , point .y + 7 , 8 , 8 );
70
- imageAfterChecked = robot .createScreenCapture (rect );
71
-
72
- check : {
73
- for (int i = 0 ; i < imageAfterChecked .getHeight (); i ++) {
74
- for (int j = 0 ; j < imageAfterChecked .getWidth (); j ++) {
75
- if (Color .black .getRGB () == imageAfterChecked .getRGB (i , j )) {
72
+ EventQueue .invokeAndWait (() -> point = checkbox .getLocationOnScreen ());
73
+ Rectangle rect = new Rectangle (point .x + 5 , point .y + 7 , 8 , 8 );
74
+ BufferedImage imageAfterChecked = robot .createScreenCapture (rect );
75
+ check :
76
+ {
77
+ for (int i = 0 ; i < imageAfterChecked .getHeight (); i ++) {
78
+ for (int j = 0 ; j < imageAfterChecked .getWidth (); j ++) {
79
+ Color pixelColor = new Color (imageAfterChecked .getRGB (i , j ));
80
+ if (compareColor (pixelColor )) {
81
+ if (++colorCounter >= COLOR_CHECK_THRESHOLD ) {
76
82
checkmarkFound = true ;
77
83
break check ;
78
84
}
79
85
}
86
+
80
87
}
81
88
}
82
- });
89
+ }
83
90
84
91
if (!checkmarkFound ) {
85
92
try {
@@ -99,4 +106,10 @@ public static void main(String[] args) throws Exception {
99
106
});
100
107
}
101
108
}
109
+
110
+ private static boolean compareColor (Color c ) {
111
+ return Math .abs (Color .black .getRed () - c .getRed ()) < TOLERANCE &&
112
+ Math .abs (Color .black .getGreen () - c .getGreen ()) < TOLERANCE &&
113
+ Math .abs (Color .black .getBlue () - c .getBlue ()) < TOLERANCE ;
114
+ }
102
115
}
0 commit comments