1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /**
  26  * @test
  27  * @bug 8146042
  28  * @requires (os.family != "mac")
  29  * @run main RenderRectTest
  30  */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 
  35 import java.awt.image.BufferedImage;
  36 
  37 
  38 public class RenderRectTest {
  39     private static JFrame frame;
  40     private static Panel panel;
  41     private static Point pt;
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         SwingUtilities.invokeAndWait(() -> {
  46             frame =  new JFrame();
  47             panel = new Panel() {
  48                 @Override
  49                 public void paint(Graphics graphics) {
  50                     graphics.setColor(Color.black);
  51                     graphics.drawRect(100, 100, 9, 9);
  52                 }
  53             };
  54             panel.setBackground(Color.white);
  55             panel.setPreferredSize(new Dimension(200, 200));
  56             panel.setMinimumSize(new Dimension(200, 200));
  57             panel.setMaximumSize(new Dimension(200, 200));
  58             frame.getContentPane().add(panel);
  59             frame.pack();
  60             frame.setVisible(true);
  61         });
  62 
  63 
  64         Robot robot = new Robot();
  65         robot.waitForIdle();
  66 
  67         SwingUtilities.invokeAndWait(() -> pt = panel.getLocationOnScreen());
  68 
  69         pt.translate(100-1, 100-1);
  70         BufferedImage capture = robot.createScreenCapture(
  71                 new Rectangle(pt, new Dimension(10 + 2, 10 + 2)));
  72 
  73         SwingUtilities.invokeLater(() -> frame.dispose());
  74 
  75         int w = capture.getWidth();
  76         int h = capture.getHeight();
  77         for(int y = 0; y < h >> 1; y++) {
  78             for(int x = 0; x < w >> 1; x++) {
  79                 if( x > 0 && y > 0 && (x == 1 || y == 1)) {
  80                     if((capture.getRGB(x, y)
  81                       | capture.getRGB(x, h - 1 - y)
  82                       | capture.getRGB(w - 1 - x, y)
  83                       | capture.getRGB(w - 1 - x, h - 1 - y)) !=
  84                                                          Color.black.getRGB()) {
  85                         throw new RuntimeException("Wrong rectangle");
  86                     }
  87                 } else {
  88                     if((capture.getRGB(x, y)
  89                       & capture.getRGB(x, h - 1 - y)
  90                       & capture.getRGB(w - 1 - x, y)
  91                       & capture.getRGB(w - 1 - x, h - 1 - y)) !=
  92                                                          Color.white.getRGB()) {
  93                         throw new RuntimeException("Wrong rectangle");
  94                     }
  95                 }
  96             }
  97         }
  98     }
  99 }