Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/GdiRendering/InsetClipping.java
38838 views
/*1* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4873505 658888426* @author cheth27* @summary verifies that drawImage behaves the bounds of a complex28* clip shape. This was a problem with our GDI renderer on Windows, where29* we would ignore the window insets.30* @run main InsetClipping31*/3233/**34* This test works by setting up a clip area that equals the visible area35* of the Frame. When we perform any rendering operation to that window,36* we should not see the results of the operation because they should be37* clipped out. We create an Image with one color (red) and use a38* different background fill color (blue). We fill the area with the39* background color, then set the clip, then draw the image; if we detect40* the image color at pixel (0, 0) then we did not clip correctly and the41* test fails.42*/4344import java.awt.*;45import java.awt.geom.*;46import java.awt.image.*;474849public class InsetClipping extends Frame {50BufferedImage image;51Area area;52static boolean painted = false;53static Color imageColor = Color.red;54static Color fillColor = Color.blue;5556public InsetClipping() {5758image = new BufferedImage( 300, 300,BufferedImage.TYPE_INT_RGB);59Graphics g2 = image.createGraphics();60g2.setColor(imageColor);61g2.fillRect(0,0, 300,300);62}6364public void paint(Graphics g) {65Insets insets = getInsets();66area = new Area( new Rectangle(0,0, getWidth(), getHeight()));67area.subtract(new Area(new Rectangle(insets.left, insets.top,68getWidth() - insets.right,69getHeight() - insets.bottom)));70g.setColor(fillColor);71g.fillRect(0, 0, getWidth(), getHeight());72g.setClip(area);73g.drawImage(image, 0, 0, null);74painted = true;75}7677public static void main(String args[]) {78InsetClipping clipTest = new InsetClipping();79clipTest.setSize(300, 300);80clipTest.setVisible(true);81while (!painted) {82try {83Thread.sleep(100);84} catch (Exception e) {}85}86try {87Thread.sleep(2000);88} catch (InterruptedException ex) {}89try {90Robot robot = new Robot();91Point clientLoc = clipTest.getLocationOnScreen();92Insets insets = clipTest.getInsets();93clientLoc.x += insets.left;94clientLoc.y += insets.top;95BufferedImage clientPixels =96robot.createScreenCapture(new Rectangle(clientLoc.x,97clientLoc.y,98clientLoc.x + 2,99clientLoc.y + 2));100try {101Thread.sleep(2000);102} catch (Exception e) {}103int pixelVal = clientPixels.getRGB(0, 0);104clipTest.dispose();105if ((new Color(pixelVal)).equals(fillColor)) {106System.out.println("Passed");107} else {108throw new Error("Failed: incorrect color in pixel (0, 0)");109}110} catch (Exception e) {111System.out.println("Problems creating Robot");112}113}114}115116117