11/*
2- * Copyright (c) 1999, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1999, 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
3333 * @run main RepaintTest
3434 */
3535
36- import java .awt .*;
36+ import java .awt .BorderLayout ;
37+ import java .awt .Button ;
38+ import java .awt .Color ;
39+ import java .awt .Component ;
40+ import java .awt .Container ;
41+ import java .awt .Frame ;
42+ import java .awt .Panel ;
43+ import java .awt .Point ;
44+ import java .awt .Rectangle ;
45+ import java .awt .TextField ;
46+ import java .awt .Toolkit ;
3747import java .awt .event .*;
3848import javax .swing .JFrame ;
3949import javax .swing .JButton ;
4050import javax .swing .JTextField ;
4151import javax .swing .JPanel ;
42- import java .io .*;
43- import java .awt .image .*;
52+ import java .awt .image .BufferedImage ;
53+ import java .awt .image .PixelGrabber ;
54+ import java .io .File ;
4455
4556public class RepaintTest {
46- private static int delay = 150 ;
57+ private static final int delay = 150 ;
4758
4859 private Frame frame ;
4960 private Container panel1 , panel2 ;
5061 private Component button ;
5162 private Component textField ;
5263 private ExtendedRobot robot ;
53- private Object buttonLock = new Object ();
64+ private final Object buttonLock = new Object ();
5465 private boolean passed = true ;
5566 private boolean buttonClicked = false ;
56- private int MAX_TOLERANCE_LEVEL = 10 ;
67+ private final int MAX_TOLERANCE_LEVEL = 10 ;
5768
5869 public static void main (String [] args ) {
5970 RepaintTest test = new RepaintTest ();
@@ -81,25 +92,6 @@ public void run() {
8192 }
8293 }
8394
84- /**
85- * Do screen capture and save it as image
86- */
87- private static void captureScreenAndSave () {
88-
89- try {
90- Robot robot = new Robot ();
91- Dimension screenSize = Toolkit .getDefaultToolkit ().getScreenSize ();
92- Rectangle rectangle = new Rectangle (0 , 0 , screenSize .width , screenSize .height );
93- System .out .println ("About to screen capture - " + rectangle );
94- BufferedImage image = robot .createScreenCapture (rectangle );
95- javax .imageio .ImageIO .write (image , "jpg" , new File ("ScreenImage.jpg" ));
96- robot .delay (3000 );
97- } catch (Throwable t ) {
98- System .out .println ("WARNING: Exception thrown while screen capture!" );
99- t .printStackTrace ();
100- }
101- }
102-
10395 private void initializeGUI (boolean swingControl ) {
10496 frame = swingControl ? new JFrame () : new Frame ();
10597 frame .setLayout (new BorderLayout ());
@@ -115,10 +107,10 @@ private void initializeGUI(boolean swingControl) {
115107 panel2 .add (textField );
116108 frame .add (panel2 , BorderLayout .SOUTH );
117109 frame .add (panel1 , BorderLayout .NORTH );
110+ frame .setLocationRelativeTo (null );
118111
119112 frame .setBackground (Color .green );
120113 frame .setVisible (true );
121- frame .toFront ();
122114 }
123115 private Component createButton (boolean swingControl , String txt ) {
124116 if (swingControl ) {
@@ -136,7 +128,7 @@ public void actionPerformed(ActionEvent e) {
136128 }
137129 });
138130 return jbtn ;
139- }else {
131+ } else {
140132 Button btn = new Button (txt );
141133 btn .addActionListener (new ActionListener () {
142134 public void actionPerformed (ActionEvent e ) {
@@ -175,10 +167,9 @@ public void run() {
175167
176168 robot .mouseMove (button .getLocationOnScreen ().x + button .getSize ().width / 2 ,
177169 button .getLocationOnScreen ().y + button .getSize ().height / 2 );
170+
171+ robot .click ();
178172 robot .waitForIdle (delay );
179- robot .mousePress (InputEvent .BUTTON1_MASK );
180- robot .waitForIdle (delay );
181- robot .mouseRelease (InputEvent .BUTTON1_MASK );
182173
183174 if (! buttonClicked ) {
184175 synchronized (buttonLock ) {
@@ -230,7 +221,6 @@ public void run() {
230221 Toolkit .getDefaultToolkit ().getSystemEventQueue ().invokeAndWait (new Runnable () {
231222 public void run () {
232223 frame .setExtendedState (Frame .NORMAL );
233- frame .toFront ();
234224 }
235225 });
236226 } catch (Exception e ) {
0 commit comments