11/*
2- * Copyright (c) 2005, 2012 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2005, 2025 , 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
2121 * questions.
2222 */
2323
24- import java .awt .AWTException ;
2524import java .awt .Color ;
2625import java .awt .Component ;
2726import java .awt .Dimension ;
27+ import java .awt .EventQueue ;
2828import java .awt .Frame ;
2929import java .awt .Graphics ;
3030import java .awt .Point ;
4242 * make sure the pixels on the screen are red.
4343 *
4444 * Note that we force the use of shared memory pixmaps in the shell script.
45- *
46- * @author Dmitri.Trembovetski
4745 */
4846
4947public class SharedMemoryPixmapsTest {
50- static final int IMAGE_SIZE = 100 ;
51- static boolean show = false ;
52- final Frame testFrame ;
53- /** Creates a new instance of SharedMemoryPixmapsTest */
54- public SharedMemoryPixmapsTest () {
48+ static final int IMAGE_SIZE = 200 ;
49+ static volatile boolean show = false ;
50+ static volatile Frame testFrame ;
51+ static volatile TestComponent testComponent ;
52+
53+ static void createUI () {
5554 testFrame = new Frame ("SharedMemoryPixmapsTest" );
56- testFrame .add (new TestComponent ());
55+ testComponent = new TestComponent ();
56+ testFrame .add (testComponent );
5757 testFrame .setUndecorated (true );
5858 testFrame .setResizable (false );
5959 testFrame .pack ();
@@ -62,20 +62,51 @@ public SharedMemoryPixmapsTest() {
6262 testFrame .toFront ();
6363 }
6464
65- public static void main (String [] args ) {
65+ public static void main (String [] args ) throws Exception {
6666 for (String s : args ) {
6767 if ("-show" .equals (s )) {
6868 show = true ;
6969 } else {
7070 System .err .println ("Usage: SharedMemoryPixmapsTest [-show]" );
7171 }
7272 }
73- new SharedMemoryPixmapsTest ();
73+ EventQueue .invokeAndWait (SharedMemoryPixmapsTest ::createUI );
74+ if (testRendering ()) {
75+ System .err .println ("Test Passed" );
76+ } else {
77+ System .err .println ("Test Failed" );
78+ }
79+ if (!show && testFrame != null ) {
80+ EventQueue .invokeAndWait (testFrame ::dispose );
81+ }
82+ }
83+
84+ static boolean testRendering () throws Exception {
85+ Robot r = new Robot ();
86+ r .waitForIdle ();
87+ r .delay (2000 );
88+ Point p = testComponent .getLocationOnScreen ();
89+ BufferedImage b =
90+ r .createScreenCapture (new Rectangle (p , testComponent .getPreferredSize ()));
91+ for (int y = 20 ; y < b .getHeight () - 40 ; y ++) {
92+ for (int x = 20 ; x < b .getWidth () - 40 ; x ++) {
93+ if (b .getRGB (x , y ) != Color .red .getRGB ()) {
94+ System .err .println ("Incorrect pixel at "
95+ + x + "x" + y + " : " +
96+ Integer .toHexString (b .getRGB (x , y )));
97+ if (show ) {
98+ return false ;
99+ }
100+ System .err .println ("Test Failed" );
101+ System .exit (1 );
102+ }
103+ }
104+ }
105+ return true ;
74106 }
75107
76- private class TestComponent extends Component {
108+ static class TestComponent extends Component {
77109 VolatileImage vi = null ;
78- boolean tested = false ;
79110
80111 void initVI () {
81112 int res ;
@@ -104,60 +135,15 @@ public synchronized void paint(Graphics g) {
104135 g .setColor (Color .green );
105136 g .fillRect (0 , 0 , getWidth (), getHeight ());
106137
138+ vi = null ;
107139 initVI ();
108140 g .drawImage (vi , 0 , 0 , null );
109141 } while (vi .contentsLost ());
110-
111- Toolkit .getDefaultToolkit ().sync ();
112- if (!tested ) {
113- if (testRendering ()) {
114- System .err .println ("Test Passed" );
115- } else {
116- System .err .println ("Test Failed" );
117- }
118- tested = true ;
119- }
120- if (!show ) {
121- testFrame .setVisible (false );
122- testFrame .dispose ();
123- }
124- }
125-
126- private boolean testRendering () throws RuntimeException {
127- try {
128- Thread .sleep (2000 );
129- } catch (InterruptedException ex ) {}
130- Robot r = null ;
131- try {
132- r = new Robot ();
133- } catch (AWTException ex ) {
134- ex .printStackTrace ();
135- throw new RuntimeException ("Can't create Robot" );
136- }
137- Point p = getLocationOnScreen ();
138- BufferedImage b =
139- r .createScreenCapture (new Rectangle (p , getPreferredSize ()));
140- for (int y = 0 ; y < b .getHeight (); y ++) {
141- for (int x = 0 ; x < b .getWidth (); x ++) {
142- if (b .getRGB (x , y ) != Color .red .getRGB ()) {
143- System .err .println ("Incorrect pixel" + " at "
144- + x + "x" + y + " : " +
145- Integer .toHexString (b .getRGB (x , y )));
146- if (show ) {
147- return false ;
148- }
149- System .err .println ("Test Failed" );
150- System .exit (1 );
151- }
152- }
153- }
154- return true ;
155142 }
156143
157144 @ Override
158145 public Dimension getPreferredSize () {
159146 return new Dimension (IMAGE_SIZE , IMAGE_SIZE );
160147 }
161148 }
162-
163149}
0 commit comments