Skip to content

Commit 302ce64

Browse files
committed
8353070: Clean up and open source couple AWT Graphics related tests (Part 1)
Backport-of: 5660922
1 parent 90a37b0 commit 302ce64

File tree

4 files changed

+407
-0
lines changed

4 files changed

+407
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2006, 2025, 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 4094059
27+
* @summary drawing to a subclass of canvas didn't draw to the correct location.
28+
* @key headful
29+
* @run main LineLocationTest
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.Canvas;
34+
import java.awt.Color;
35+
import java.awt.Dimension;
36+
import java.awt.EventQueue;
37+
import java.awt.Frame;
38+
import java.awt.Graphics;
39+
import java.awt.Panel;
40+
import java.awt.Point;
41+
import java.awt.Rectangle;
42+
import java.awt.Robot;
43+
import java.awt.image.BufferedImage;
44+
import java.lang.reflect.InvocationTargetException;
45+
46+
public class LineLocationTest extends Frame {
47+
private DrawScreen screen;
48+
49+
public void initialize() {
50+
setSize(400, 400);
51+
setLocationRelativeTo(null);
52+
setTitle("Line Location Test");
53+
Panel p = new Panel();
54+
screen = new DrawScreen();
55+
p.add(screen);
56+
p.setLocation(50, 50);
57+
p.setSize(300, 300);
58+
add(p);
59+
setBackground(Color.white);
60+
setForeground(Color.blue);
61+
setVisible(true);
62+
}
63+
64+
public void requestCoordinates(Rectangle r) {
65+
Point location = screen.getLocationOnScreen();
66+
Dimension size = screen.getSize();
67+
r.setBounds(location.x, location.y, size.width, size.height);
68+
}
69+
70+
public static void main(String[] args) throws InterruptedException,
71+
InvocationTargetException, AWTException {
72+
LineLocationTest me = new LineLocationTest();
73+
EventQueue.invokeAndWait(me::initialize);
74+
try {
75+
Robot robot = new Robot();
76+
robot.delay(1000);
77+
Rectangle coords = new Rectangle();
78+
EventQueue.invokeAndWait(() -> {
79+
me.requestCoordinates(coords);
80+
});
81+
BufferedImage capture = robot.createScreenCapture(coords);
82+
robot.delay(2000);
83+
for (int y = 0; y < capture.getHeight(); y++) {
84+
for (int x = 0; x < capture.getWidth(); x++) {
85+
int blue = Color.blue.getRGB();
86+
if (capture.getRGB(x, y) == blue) {
87+
throw new RuntimeException("Blue detected at " + x + ", " + y);
88+
}
89+
}
90+
}
91+
} finally {
92+
EventQueue.invokeAndWait(me::dispose);
93+
}
94+
}
95+
}
96+
97+
class DrawScreen extends Canvas {
98+
public Dimension getPreferredSize() {
99+
return new Dimension(300, 300);
100+
}
101+
102+
public void paint(Graphics g) {
103+
g.setColor(Color.blue);
104+
g.drawLine(5, -3145583, 50, -3145583);
105+
}
106+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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 4216180
27+
* @summary This test verifies that Graphics2D.setBackground and clearRect
28+
* performs correctly regardless of antialiasing hint.
29+
* @key headful
30+
* @run main NativeWin32Clear
31+
*/
32+
33+
import java.awt.AWTException;
34+
import java.awt.Color;
35+
import java.awt.Dimension;
36+
import java.awt.EventQueue;
37+
import java.awt.Frame;
38+
import java.awt.Graphics;
39+
import java.awt.Graphics2D;
40+
import java.awt.Insets;
41+
import java.awt.Point;
42+
import java.awt.Rectangle;
43+
import java.awt.RenderingHints;
44+
import java.awt.Robot;
45+
import java.awt.image.BufferedImage;
46+
import java.lang.reflect.InvocationTargetException;
47+
48+
public class NativeWin32Clear extends Frame {
49+
50+
public void initialize() {
51+
setLocationRelativeTo(null);
52+
setSize(300, 200);
53+
setBackground(Color.red);
54+
setVisible(true);
55+
}
56+
57+
public void paint(Graphics g) {
58+
Graphics2D g2 = (Graphics2D) g;
59+
Dimension d = getSize();
60+
g2.setBackground(Color.green);
61+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
62+
RenderingHints.VALUE_ANTIALIAS_ON);
63+
g2.clearRect(0, 0, d.width / 2, d.height);
64+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
65+
RenderingHints.VALUE_ANTIALIAS_OFF);
66+
g2.clearRect(d.width / 2, 0, d.width / 2, d.height);
67+
g2.setColor(Color.black);
68+
}
69+
70+
public void cleanup() {
71+
setVisible(false);
72+
dispose();
73+
}
74+
75+
public void requestCoordinates(Rectangle r) {
76+
Insets insets = getInsets();
77+
Point location = getLocationOnScreen();
78+
Dimension size = getSize();
79+
r.x = location.x + insets.left + 5;
80+
r.y = location.y + insets.top + 5;
81+
r.width = size.width - (insets.left + insets.right + 10);
82+
r.height = size.height - (insets.top + insets.bottom + 10);
83+
}
84+
85+
/*
86+
* Check color match within allowed deviation.
87+
* Prints first non-matching pixel coordinates and actual and expected values.
88+
* Returns true if image is filled with the provided color, false otherwise.
89+
*/
90+
private boolean checkColor(BufferedImage img, Color c, int delta) {
91+
int cRed = c.getRed();
92+
int cGreen = c.getGreen();
93+
int cBlue = c.getBlue();
94+
for (int y = 0; y < img.getHeight(); y++) {
95+
for (int x = 0; x < img.getWidth(); x++) {
96+
int rgb = img.getRGB(x, y);
97+
int red = (rgb & 0x00ff0000) >> 16;
98+
int green = (rgb & 0x0000ff00) >> 8;
99+
int blue = rgb & 0x000000ff;
100+
if (cRed > (red + delta) || cRed < (red - delta)
101+
|| cGreen > (green + delta) || cGreen < (green - delta)
102+
|| cBlue > (blue + delta) || cBlue < (blue - delta)) {
103+
System.err.println("Color at coordinates (" + x + ", " + y + ") does not match");
104+
System.err.println("Expected color: " + c.getRGB());
105+
System.err.println("Actual color: " + rgb);
106+
System.err.println("Allowed deviation: " + delta);
107+
return false;
108+
}
109+
}
110+
}
111+
return true;
112+
}
113+
114+
public static void main(String[] args) throws InterruptedException,
115+
InvocationTargetException, AWTException {
116+
NativeWin32Clear test = new NativeWin32Clear();
117+
try {
118+
EventQueue.invokeAndWait(test::initialize);
119+
Robot robot = new Robot();
120+
Rectangle coords = new Rectangle();
121+
EventQueue.invokeAndWait(() -> {
122+
test.requestCoordinates(coords);
123+
});
124+
robot.delay(2000);
125+
robot.mouseMove(coords.x - 50, coords.y - 50);
126+
robot.waitForIdle();
127+
BufferedImage capture = robot.createScreenCapture(coords);
128+
robot.delay(2000);
129+
if (!test.checkColor(capture, Color.green, 5)) {
130+
throw new RuntimeException("Incorrect color encountered, check error log for details");
131+
}
132+
} finally {
133+
EventQueue.invokeAndWait(test::cleanup);
134+
}
135+
}
136+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2001, 2025, 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 4465509 4453725 4489667
27+
* @summary verify that fillPolygon completely fills area defined by drawPolygon
28+
* @key headful
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual PolygonFillTest
32+
*/
33+
34+
import java.awt.Color;
35+
import java.awt.Frame;
36+
import java.awt.Graphics;
37+
import java.awt.Image;
38+
import java.awt.Polygon;
39+
import java.lang.reflect.InvocationTargetException;
40+
41+
public class PolygonFillTest extends Frame {
42+
Polygon poly;
43+
static String INSTRUCTIONS = """
44+
There should be two hourglass shapes drawn inside the window
45+
called "Polygon Fill Test". The outline should be blue
46+
and the interior should be green and there should be no gaps
47+
between the filled interior and the outline nor should the green
48+
filler spill outside the blue outline. You may need
49+
to use a screen magnifier to inspect the smaller shape
50+
on the left to verify that there are no gaps.
51+
52+
If both polygons painted correctly press "Pass" otherwise press "Fail".
53+
""";
54+
55+
public PolygonFillTest() {
56+
poly = new Polygon();
57+
poly.addPoint(0, 0);
58+
poly.addPoint(10, 10);
59+
poly.addPoint(0, 10);
60+
poly.addPoint(10, 0);
61+
setSize(300, 300);
62+
setTitle("Polygon Fill Test");
63+
}
64+
65+
public void paint(Graphics g) {
66+
int w = getWidth();
67+
int h = getHeight();
68+
Image img = createImage(20, 20);
69+
Graphics g2 = img.getGraphics();
70+
drawPolys(g2, 20, 20, 5, 5);
71+
g2.dispose();
72+
drawPolys(g, w, h, (w / 4) - 5, (h / 2) - 5);
73+
g.drawImage(img, (3 * w / 4) - 40, (h / 2) - 40, 80, 80, null);
74+
}
75+
76+
public void drawPolys(Graphics g, int w, int h, int x, int y) {
77+
g.setColor(Color.white);
78+
g.fillRect(0, 0, w, h);
79+
g.translate(x, y);
80+
g.setColor(Color.green);
81+
g.fillPolygon(poly);
82+
g.setColor(Color.blue);
83+
g.drawPolygon(poly);
84+
g.translate(-x, -y);
85+
}
86+
87+
public static void main(String[] args) throws InterruptedException,
88+
InvocationTargetException {
89+
PassFailJFrame.builder()
90+
.title("Polygon Fill Instructions")
91+
.instructions(INSTRUCTIONS)
92+
.testUI(PolygonFillTest::new)
93+
.build()
94+
.awaitAndCheck();
95+
}
96+
}

0 commit comments

Comments
 (0)