1 /*
   2  * Copyright (c) 2003, 2010, 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  * @test
  26  * @bug 4873505 6588884
  27  * @author cheth
  28  * @summary verifies that drawImage behaves the bounds of a complex
  29  * clip shape.  This was a problem with our GDI renderer on Windows, where
  30  * we would ignore the window insets.
  31  * @run main InsetClipping
  32 */
  33 
  34 /**
  35  * This test works by setting up a clip area that equals the visible area
  36  * of the Frame.  When we perform any rendering operation to that window,
  37  * we should not see the results of the operation because they should be
  38  * clipped out.  We create an Image with one color (red) and use a
  39  * different background fill color (blue).  We fill the area with the
  40  * background color, then set the clip, then draw the image; if we detect
  41  * the image color at pixel (0, 0) then we did not clip correctly and the
  42  * test fails.
  43  */
  44 
  45 import java.awt.*;
  46 import java.awt.geom.*;
  47 import java.awt.image.*;
  48 
  49 
  50 public class InsetClipping extends Frame {
  51     BufferedImage image;
  52     Area area;
  53     static boolean painted = false;
  54     static Color imageColor = Color.red;
  55     static Color fillColor = Color.blue;
  56 
  57     public InsetClipping() {
  58 
  59         image  = new BufferedImage( 300, 300,BufferedImage.TYPE_INT_RGB);
  60         Graphics g2 = image.createGraphics();
  61         g2.setColor(imageColor);
  62         g2.fillRect(0,0, 300,300);
  63     }
  64 
  65     public void paint(Graphics g) {
  66         Insets insets = getInsets();
  67         area = new Area( new Rectangle(0,0, getWidth(), getHeight()));
  68         area.subtract(new Area(new Rectangle(insets.left, insets.top,
  69                                       getWidth() - insets.right,
  70                                       getHeight() - insets.bottom)));
  71         g.setColor(fillColor);
  72         g.fillRect(0, 0, getWidth(), getHeight());
  73         g.setClip(area);
  74         g.drawImage(image, 0, 0, null);
  75         painted = true;
  76     }
  77 
  78     public static void main(String args[]) {
  79         InsetClipping clipTest = new InsetClipping();
  80         clipTest.setSize(300, 300);
  81         clipTest.setVisible(true);
  82         while (!painted) {
  83             try {
  84                 Thread.sleep(100);
  85             } catch (Exception e) {}
  86         }
  87         try {
  88             Thread.sleep(2000);
  89         } catch (InterruptedException ex) {}
  90         try {
  91             Robot robot = new Robot();
  92             Point clientLoc = clipTest.getLocationOnScreen();
  93             Insets insets = clipTest.getInsets();
  94             clientLoc.x += insets.left;
  95             clientLoc.y += insets.top;
  96             BufferedImage clientPixels =
  97                 robot.createScreenCapture(new Rectangle(clientLoc.x,
  98                                                         clientLoc.y,
  99                                                         clientLoc.x + 2,
 100                                                         clientLoc.y + 2));
 101             try {
 102                 Thread.sleep(2000);
 103             } catch (Exception e) {}
 104             int pixelVal = clientPixels.getRGB(0, 0);
 105             clipTest.dispose();
 106             if ((new Color(pixelVal)).equals(fillColor)) {
 107                 System.out.println("Passed");
 108             } else {
 109                 throw new Error("Failed: incorrect color in pixel (0, 0)");
 110             }
 111         } catch (Exception e) {
 112             System.out.println("Problems creating Robot");
 113         }
 114     }
 115 }