1
1
/*
2
- * Copyright (c) 2015, 2017 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2022 , 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
27
27
import java .awt .Frame ;
28
28
import java .awt .Graphics ;
29
29
import java .awt .Panel ;
30
+ import java .awt .Point ;
30
31
import java .awt .Rectangle ;
31
32
import java .awt .Robot ;
32
33
import java .awt .image .BufferedImage ;
33
34
import javax .swing .UIManager ;
35
+ import javax .imageio .ImageIO ;
36
+ import java .io .File ;
37
+ import java .io .IOException ;
34
38
35
39
/**
36
40
* @test
37
41
* @key headful
38
- * @bug 8073320
39
- * @summary Windows HiDPI support
42
+ * @bug 8073320 8280861
43
+ * @summary Linux and Windows HiDPI support
40
44
* @author Alexander Scherbatiy
41
45
* @requires (os.family == "linux" | os.family == "windows")
42
46
* @run main/othervm -Dsun.java2d.win.uiScaleX=3 -Dsun.java2d.win.uiScaleY=2
43
47
* HiDPIRobotScreenCaptureTest
48
+ * @run main/othervm -Dsun.java2d.uiScale=1 HiDPIRobotScreenCaptureTest
49
+ * @run main/othervm -Dsun.java2d.uiScale=2 HiDPIRobotScreenCaptureTest
44
50
*/
45
51
46
52
public class HiDPIRobotScreenCaptureTest {
@@ -60,7 +66,14 @@ public static void main(String[] args) throws Exception {
60
66
}
61
67
62
68
Frame frame = new Frame ();
63
- frame .setBounds (40 , 30 , 400 , 300 );
69
+ // Position the frame on prime number coordinates to avoid
70
+ // them being multiple of the desktop scale; this tests Linux
71
+ // color picker better.
72
+ // Also, the position should be far enough from the top left
73
+ // corner of the screen to reduce the chance of being repositioned
74
+ // by the system because that area's occupied by the global
75
+ // menu bar and such.
76
+ frame .setBounds (83 , 97 , 400 , 300 );
64
77
frame .setUndecorated (true );
65
78
66
79
Panel panel = new Panel (new BorderLayout ());
@@ -86,11 +99,12 @@ public void paint(Graphics g) {
86
99
frame .setVisible (true );
87
100
Robot robot = new Robot ();
88
101
robot .waitForIdle ();
89
- Thread . sleep ( 200 );
102
+ robot . delay ( 500 );
90
103
91
104
Rectangle rect = canvas .getBounds ();
92
105
rect .setLocation (canvas .getLocationOnScreen ());
93
106
107
+ System .out .println ("Creating screen capture of " + rect );
94
108
BufferedImage image = robot .createScreenCapture (rect );
95
109
frame .dispose ();
96
110
@@ -101,20 +115,38 @@ public void paint(Graphics g) {
101
115
throw new RuntimeException ("Wrong image size!" );
102
116
}
103
117
104
- if (image .getRGB (w / 4 , h / 4 ) != COLORS [0 ].getRGB ()) {
105
- throw new RuntimeException ("Wrong image color!" );
106
- }
107
-
108
- if (image .getRGB (3 * w / 4 , h / 4 ) != COLORS [1 ].getRGB ()) {
109
- throw new RuntimeException ("Wrong image color!" );
110
- }
118
+ checkRectColor (image , new Rectangle (0 , 0 , w / 2 , h / 2 ), COLORS [0 ]);
119
+ checkRectColor (image , new Rectangle (w / 2 , 0 , w / 2 , h / 2 ), COLORS [1 ]);
120
+ checkRectColor (image , new Rectangle (0 , h / 2 , w / 2 , h / 2 ), COLORS [2 ]);
121
+ checkRectColor (image , new Rectangle (w / 2 , h / 2 , w / 2 , h / 2 ), COLORS [3 ]);
122
+ }
111
123
112
- if (image .getRGB (w / 4 , 3 * h / 4 ) != COLORS [2 ].getRGB ()) {
113
- throw new RuntimeException ("Wrong image color!" );
114
- }
124
+ private static final int OFFSET = 5 ;
125
+ static void checkRectColor (BufferedImage image , Rectangle rect , Color expectedColor ) {
126
+ System .out .println ("Checking rectangle " + rect + " to have color " + expectedColor );
127
+ final Point [] pointsToCheck = new Point [] {
128
+ new Point (rect .x + OFFSET , rect .y + OFFSET ), // top left corner
129
+ new Point (rect .x + rect .width - OFFSET , rect .y + OFFSET ), // top right corner
130
+ new Point (rect .x + rect .width / 2 , rect .y + rect .height / 2 ), // center
131
+ new Point (rect .x + OFFSET , rect .y + rect .height - OFFSET ), // bottom left corner
132
+ new Point (rect .x + rect .width - OFFSET , rect .y + rect .height - OFFSET ) // bottom right corner
133
+ };
115
134
116
- if (image .getRGB (3 * w / 4 , 3 * h / 4 ) != COLORS [3 ].getRGB ()) {
117
- throw new RuntimeException ("Wrong image color!" );
135
+ for (final var point : pointsToCheck ) {
136
+ System .out .print ("Checking color at " + point + " to be equal to " + expectedColor );
137
+ final int actualColor = image .getRGB (point .x , point .y );
138
+ if (actualColor != expectedColor .getRGB ()) {
139
+ System .out .println ("... Mismatch: found " + new Color (actualColor ) + " instead. Check image.png." );
140
+ try {
141
+ ImageIO .write (image , "png" , new File ("image.png" ));
142
+ } catch (IOException e ) {
143
+ System .out .println ("failed to save image.png." );
144
+ e .printStackTrace ();
145
+ }
146
+ throw new RuntimeException ("Wrong image color!" );
147
+ } else {
148
+ System .out .println ("... OK" );
149
+ }
118
150
}
119
151
}
120
- }
152
+ }
0 commit comments