11/*
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.
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
3636
3737public class CopyAreaOOB extends Canvas {
3838
39- private static boolean done ;
39+ private static Robot robot = null ;
4040
4141 public void paint (Graphics g ) {
42- synchronized (this ) {
43- if (done ) {
44- return ;
45- }
46- }
47-
4842 int w = getWidth ();
4943 int h = getHeight ();
5044
@@ -64,10 +58,23 @@ public void paint(Graphics g) {
6458
6559 Toolkit .getDefaultToolkit ().sync ();
6660
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" );
7070 }
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 );
7178 }
7279
7380 public Dimension getPreferredSize () {
@@ -105,42 +112,11 @@ public static void main(String[] args) {
105112 frame .setLocationRelativeTo (null );
106113 frame .setVisible (true );
107114
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-
119115 try {
120- Thread .sleep (2000 );
116+ Thread .sleep (3000 );
121117 } 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 ();
136120 }
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 );
145121 }
146122}
0 commit comments