Path: blob/master/test/jdk/sun/java2d/GdiRendering/InsetClipping.java
41149 views
/*1* Copyright (c) 2003, 2020, 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* @key headful26* @bug 4873505 658888427* @author cheth28* @summary verifies that drawImage behaves the bounds of a complex29* clip shape. This was a problem with our GDI renderer on Windows, where30* we would ignore the window insets.31* @run main InsetClipping32*/3334/**35* This test works by setting up a clip area that equals the visible area36* 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 be38* clipped out. We create an Image with one color (red) and use a39* different background fill color (blue). We fill the area with the40* background color, then set the clip, then draw the image; if we detect41* the image color at pixel (0, 0) then we did not clip correctly and the42* test fails.43*/4445import java.awt.Color;46import java.awt.Frame;47import java.awt.Graphics;48import java.awt.Insets;49import java.awt.Point;50import java.awt.Rectangle;51import java.awt.Robot;52import java.awt.geom.Area;53import java.awt.image.BufferedImage;5455public class InsetClipping extends Frame {56BufferedImage image;57Area area;58static boolean painted = false;59static Color imageColor = Color.red;60static Color fillColor = Color.blue;6162public InsetClipping() {6364image = new BufferedImage( 300, 300,BufferedImage.TYPE_INT_RGB);65Graphics g2 = image.createGraphics();66g2.setColor(imageColor);67g2.fillRect(0,0, 300,300);68}6970public void paint(Graphics g) {71Insets insets = getInsets();72area = new Area( new Rectangle(0,0, getWidth(), getHeight()));73area.subtract(new Area(new Rectangle(insets.left, insets.top,74getWidth() - insets.right,75getHeight() - insets.bottom)));76g.setColor(fillColor);77g.fillRect(0, 0, getWidth(), getHeight());78g.setClip(area);79g.drawImage(image, 0, 0, null);80painted = true;81}8283public static void main(String args[]) {84InsetClipping clipTest = new InsetClipping();85clipTest.setSize(300, 300);86clipTest.setLocationRelativeTo(null);87clipTest.setVisible(true);88while (!painted) {89try {90Thread.sleep(100);91} catch (Exception e) {}92}93try {94Thread.sleep(2000);95} catch (InterruptedException ex) {}96try {97Robot robot = new Robot();98Point clientLoc = clipTest.getLocationOnScreen();99Insets insets = clipTest.getInsets();100clientLoc.x += insets.left;101clientLoc.y += insets.top;102BufferedImage clientPixels =103robot.createScreenCapture(new Rectangle(clientLoc.x,104clientLoc.y,105clientLoc.x + 2,106clientLoc.y + 2));107try {108Thread.sleep(2000);109} catch (Exception e) {}110int pixelVal = clientPixels.getRGB(2, 2);111clipTest.dispose();112if ((new Color(pixelVal)).equals(fillColor)) {113System.out.println("Passed");114} else {115throw new Error("Failed: incorrect color in pixel (2, 2)");116}117} catch (Exception e) {118System.out.println("Problems creating Robot");119}120}121}122123124