887
|
1 |
/*
|
|
2 |
* Copyright 2003-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any 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 |
Robot robot = new Robot();
|
|
89 |
Point clientLoc = clipTest.getLocationOnScreen();
|
|
90 |
Insets insets = clipTest.getInsets();
|
|
91 |
clientLoc.x += insets.left;
|
|
92 |
clientLoc.y += insets.top;
|
|
93 |
BufferedImage clientPixels =
|
|
94 |
robot.createScreenCapture(new Rectangle(clientLoc.x,
|
|
95 |
clientLoc.y,
|
|
96 |
clientLoc.x + 2,
|
|
97 |
clientLoc.y + 2));
|
|
98 |
try {
|
|
99 |
Thread.sleep(2000);
|
|
100 |
} catch (Exception e) {}
|
|
101 |
int pixelVal = clientPixels.getRGB(0, 0);
|
|
102 |
clipTest.dispose();
|
|
103 |
if ((new Color(pixelVal)).equals(fillColor)) {
|
|
104 |
System.out.println("Passed");
|
|
105 |
} else {
|
|
106 |
throw new Error("Failed: incorrect color in pixel (0, 0)");
|
|
107 |
}
|
|
108 |
} catch (Exception e) {
|
|
109 |
System.out.println("Problems creating Robot");
|
|
110 |
}
|
|
111 |
}
|
|
112 |
}
|