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