/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test * @bug 8146042 * @requires (os.family != "mac") * @run main RenderRectTest */ import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; public class RenderRectTest { private static JFrame frame; private static Panel panel; private static Point pt; public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(() -> { frame = new JFrame(); panel = new Panel() { @Override public void paint(Graphics graphics) { graphics.setColor(Color.black); graphics.drawRect(100, 100, 9, 9); } }; panel.setBackground(Color.white); panel.setPreferredSize(new Dimension(200, 200)); panel.setMinimumSize(new Dimension(200, 200)); panel.setMaximumSize(new Dimension(200, 200)); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }); Robot robot = new Robot(); robot.waitForIdle(); SwingUtilities.invokeAndWait(() -> pt = panel.getLocationOnScreen()); pt.translate(100-1, 100-1); BufferedImage capture = robot.createScreenCapture( new Rectangle(pt, new Dimension(10 + 2, 10 + 2))); SwingUtilities.invokeLater(() -> frame.dispose()); int w = capture.getWidth(); int h = capture.getHeight(); for(int y = 0; y < h >> 1; y++) { for(int x = 0; x < w >> 1; x++) { if( x > 0 && y > 0 && (x == 1 || y == 1)) { if((capture.getRGB(x, y) | capture.getRGB(x, h - 1 - y) | capture.getRGB(w - 1 - x, y) | capture.getRGB(w - 1 - x, h - 1 - y)) != Color.black.getRGB()) { throw new RuntimeException("Wrong rectangle"); } } else { if((capture.getRGB(x, y) & capture.getRGB(x, h - 1 - y) & capture.getRGB(w - 1 - x, y) & capture.getRGB(w - 1 - x, h - 1 - y)) != Color.white.getRGB()) { throw new RuntimeException("Wrong rectangle"); } } } } } }