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  * @run main RenderRectTest
  29  */
  30 
  31 import javax.swing.*;
  32 import java.awt.*;
  33 
  34 import java.awt.image.BufferedImage;
  35 
  36 
  37 public class RenderRectTest {
  38     private static JFrame frame;
  39     private static Panel panel;
  40     private static Point pt;
  41 
  42     public static void main(String[] args) throws Exception {
  43 
  44         SwingUtilities.invokeAndWait(() -> {
  45             frame =  new JFrame();
  46             panel = new Panel() {
  47                 @Override
  48                 public void paint(Graphics graphics) {
  49                     graphics.setColor(Color.black);
  50                     graphics.drawRect(100, 100, 9, 9);
  51                 }
  52             };
  53             panel.setBackground(Color.white);
  54             panel.setPreferredSize(new Dimension(200, 200));
  55             panel.setMinimumSize(new Dimension(200, 200));
  56             panel.setMaximumSize(new Dimension(200, 200));
  57             frame.getContentPane().add(panel);
  58             frame.pack();
  59             frame.setVisible(true);
  60         });
  61 
  62 
  63         Robot robot = new Robot();
  64         robot.waitForIdle();
  65 
  66         SwingUtilities.invokeAndWait(() -> pt = panel.getLocationOnScreen());
  67 
  68         pt.translate(100-1, 100-1);
  69         BufferedImage capture = robot.createScreenCapture(
  70                 new Rectangle(pt, new Dimension(10 + 2, 10 + 2)));
  71 
  72         SwingUtilities.invokeLater(() -> frame.dispose());
  73 
  74         int w = capture.getWidth();
  75         int h = capture.getHeight();
  76         for(int y = 0; y < h >> 1; y++) {
  77             for(int x = 0; x < w >> 1; x++) {
  78                 if( x > 0 && y > 0 && (x == 1 || y == 1)) {
  79                     if((capture.getRGB(x, y)
  80                       | capture.getRGB(x, h - 1 - y)
  81                       | capture.getRGB(w - 1 - x, y)
  82                       | capture.getRGB(w - 1 - x, h - 1 - y)) !=
  83                                                          Color.black.getRGB()) {
  84                         throw new RuntimeException("Wrong rectangle");
  85                     }
  86                 } else {
  87                     if((capture.getRGB(x, y)
  88                       & capture.getRGB(x, h - 1 - y)
  89                       & capture.getRGB(w - 1 - x, y)
  90                       & capture.getRGB(w - 1 - x, h - 1 - y)) !=
  91                                                          Color.white.getRGB()) {
  92                         throw new RuntimeException("Wrong rectangle");
  93                     }
  94                 }
  95             }
  96         }
  97     }
  98 }