1
1
/*
2
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
36
36
37
37
public class CopyAreaOOB extends Canvas {
38
38
39
- private static boolean done ;
39
+ private static Robot robot = null ;
40
40
41
41
public void paint (Graphics g ) {
42
- synchronized (this ) {
43
- if (done ) {
44
- return ;
45
- }
46
- }
47
-
48
42
int w = getWidth ();
49
43
int h = getHeight ();
50
44
@@ -64,10 +58,23 @@ public void paint(Graphics g) {
64
58
65
59
Toolkit .getDefaultToolkit ().sync ();
66
60
67
- synchronized (this ) {
68
- done = true ;
69
- notifyAll ();
61
+ BufferedImage capture = null ;
62
+ try {
63
+ Thread .sleep (500 );
64
+ if (robot == null ) robot = new Robot ();
65
+ Point pt1 = getLocationOnScreen ();
66
+ Rectangle rect = new Rectangle (pt1 .x , pt1 .y , 400 , 400 );
67
+ capture = robot .createScreenCapture (rect );
68
+ } catch (Exception e ) {
69
+ throw new RuntimeException ("Problems handling Robot" );
70
70
}
71
+ // Test pixels
72
+ testRegion (capture , "green" , 0 , 0 , 400 , 10 , 0xff00ff00 );
73
+ testRegion (capture , "original red" , 0 , 10 , 50 , 400 , 0xffff0000 );
74
+ testRegion (capture , "background" , 50 , 10 , 60 , 400 , 0xff000000 );
75
+ testRegion (capture , "in-between" , 60 , 10 , 110 , 20 , 0xff000000 );
76
+ testRegion (capture , "copied red" , 60 , 20 , 110 , 400 , 0xffff0000 );
77
+ testRegion (capture , "background" , 110 , 10 , 400 , 400 , 0xff000000 );
71
78
}
72
79
73
80
public Dimension getPreferredSize () {
@@ -105,42 +112,11 @@ public static void main(String[] args) {
105
112
frame .setLocationRelativeTo (null );
106
113
frame .setVisible (true );
107
114
108
- // Wait until the component's been painted
109
- synchronized (test ) {
110
- while (!done ) {
111
- try {
112
- test .wait ();
113
- } catch (InterruptedException e ) {
114
- throw new RuntimeException ("Failed: Interrupted" );
115
- }
116
- }
117
- }
118
-
119
115
try {
120
- Thread .sleep (2000 );
116
+ Thread .sleep (3000 );
121
117
} catch (InterruptedException ex ) {}
122
-
123
- // Grab the screen region
124
- BufferedImage capture = null ;
125
- try {
126
- Robot robot = new Robot ();
127
- Point pt1 = test .getLocationOnScreen ();
128
- Rectangle rect = new Rectangle (pt1 .x , pt1 .y , 400 , 400 );
129
- capture = robot .createScreenCapture (rect );
130
- } catch (Exception e ) {
131
- throw new RuntimeException ("Problems creating Robot" );
132
- } finally {
133
- if (!show ) {
134
- frame .dispose ();
135
- }
118
+ if (!show ) {
119
+ frame .dispose ();
136
120
}
137
-
138
- // Test pixels
139
- testRegion (capture , "green" , 0 , 0 , 400 , 10 , 0xff00ff00 );
140
- testRegion (capture , "original red" , 0 , 10 , 50 , 400 , 0xffff0000 );
141
- testRegion (capture , "background" , 50 , 10 , 60 , 400 , 0xff000000 );
142
- testRegion (capture , "in-between" , 60 , 10 , 110 , 20 , 0xff000000 );
143
- testRegion (capture , "copied red" , 60 , 20 , 110 , 400 , 0xffff0000 );
144
- testRegion (capture , "background" , 110 , 10 , 400 , 400 , 0xff000000 );
145
121
}
146
122
}
0 commit comments