|
25 | 25 | import java.awt.Color;
|
26 | 26 | import java.awt.Graphics;
|
27 | 27 | import java.awt.Graphics2D;
|
| 28 | +import java.awt.GraphicsConfiguration; |
| 29 | +import java.awt.GraphicsDevice; |
| 30 | +import java.awt.GraphicsEnvironment; |
| 31 | +import java.awt.Point; |
28 | 32 | import java.awt.Rectangle;
|
29 | 33 | import java.awt.Robot;
|
30 | 34 | import java.awt.Window;
|
31 | 35 | import java.awt.image.BufferedImage;
|
| 36 | +import java.io.File; |
32 | 37 | import java.util.concurrent.Callable;
|
33 | 38 |
|
| 39 | +import javax.imageio.ImageIO; |
34 | 40 | import javax.swing.JFrame;
|
35 | 41 | import javax.swing.JMenuItem;
|
36 | 42 | import javax.swing.JPanel;
|
@@ -103,24 +109,56 @@ public void run() {
|
103 | 109 | Rectangle popupRectangle = Util.invokeOnEDT(new Callable<Rectangle>() {
|
104 | 110 | @Override
|
105 | 111 | public Rectangle call() throws Exception {
|
106 |
| - return popupMenu.getBounds(); |
| 112 | + return new Rectangle(popupMenu.getLocationOnScreen(), |
| 113 | + popupMenu.getSize()); |
107 | 114 | }
|
108 | 115 | });
|
109 | 116 |
|
110 | 117 | BufferedImage redBackgroundCapture = robot.createScreenCapture(popupRectangle);
|
| 118 | + BufferedImage redFrame = robot.createScreenCapture(frame.getBounds()); |
111 | 119 |
|
112 | 120 | SwingUtilities.invokeAndWait(new Runnable() {
|
113 | 121 | @Override
|
114 | 122 | public void run() {
|
115 | 123 | lowerFrame.getContentPane().setBackground(Color.GREEN);
|
| 124 | + lowerFrame.invalidate(); |
116 | 125 | }
|
117 | 126 | });
|
118 | 127 |
|
119 | 128 | robot.waitForIdle();
|
| 129 | + robot.delay(1000); // Give frame time to repaint |
120 | 130 |
|
121 | 131 | BufferedImage greenBackgroundCapture = robot.createScreenCapture(popupRectangle);
|
| 132 | + BufferedImage greenFrame = robot.createScreenCapture(frame.getBounds()); |
122 | 133 |
|
123 | 134 | if (Util.compareBufferedImages(redBackgroundCapture, greenBackgroundCapture)) {
|
| 135 | + try { |
| 136 | + GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); |
| 137 | + for (int i = 0; i < devices.length; i++) { |
| 138 | + GraphicsConfiguration[] screens = devices[i].getConfigurations(); |
| 139 | + for (int j = 0; j < screens.length; j++) { |
| 140 | + BufferedImage fullScreen = robot.createScreenCapture(screens[j].getBounds()); |
| 141 | + if (screens[j].getBounds().intersects(popupRectangle)) { |
| 142 | + Graphics g = fullScreen.getGraphics(); |
| 143 | + g.setColor(Color.CYAN); |
| 144 | + g.drawRect(popupRectangle.x - 1, popupRectangle.y - 1, |
| 145 | + popupRectangle.width + 2, popupRectangle.height + 2); |
| 146 | + g.dispose(); |
| 147 | + } |
| 148 | + ImageIO.write(fullScreen, "png", new File("dev" + i + "scr" + j + ".png")); |
| 149 | + } |
| 150 | + } |
| 151 | + ImageIO.write(redFrame, "png", new File("redframe.png")); |
| 152 | + ImageIO.write(redBackgroundCapture, "png", new File("redbg.png")); |
| 153 | + ImageIO.write(greenFrame, "png", new File("greenframe.png")); |
| 154 | + ImageIO.write(greenBackgroundCapture, "png", new File("greenbg.png")); |
| 155 | + } finally { |
| 156 | + SwingUtilities.invokeAndWait(() -> { |
| 157 | + frame.dispose(); |
| 158 | + lowerFrame.dispose(); |
| 159 | + }); |
| 160 | + } |
| 161 | + robot.waitForIdle(); |
124 | 162 | throw new RuntimeException("The test failed");
|
125 | 163 | }
|
126 | 164 |
|
@@ -148,8 +186,8 @@ public static void setOpaque(Window window, boolean opaque) {
|
148 | 186 | private static JFrame createFrame() {
|
149 | 187 | JFrame result = new JFrame();
|
150 | 188 |
|
151 |
| - result.setLocation(0, 0); |
152 | 189 | result.setSize(400, 300);
|
| 190 | + result.setLocationRelativeTo(null); |
153 | 191 | result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
154 | 192 | result.setUndecorated(true);
|
155 | 193 |
|
|
0 commit comments