Skip to content

Commit a98d3a7

Browse files
Jeremy Woodprrace
authored andcommitted
4197755: Arc2D.getBounds() returns an unnecessarily large bounding box
Reviewed-by: prr, psadhukhan
1 parent c78a2a8 commit a98d3a7

File tree

2 files changed

+100
-14
lines changed

2 files changed

+100
-14
lines changed

src/java.desktop/share/classes/java/awt/geom/Arc2D.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525

2626
package java.awt.geom;
2727

28+
import java.awt.Rectangle;
2829
import java.io.IOException;
2930
import java.io.Serial;
3031
import java.io.Serializable;
@@ -1052,19 +1053,7 @@ public void setFrame(double x, double y, double w, double h) {
10521053
}
10531054

10541055
/**
1055-
* Returns the high-precision framing rectangle of the arc. The framing
1056-
* rectangle contains only the part of this {@code Arc2D} that is
1057-
* in between the starting and ending angles and contains the pie
1058-
* wedge, if this {@code Arc2D} has a {@code PIE} closure type.
1059-
* <p>
1060-
* This method differs from the
1061-
* {@link RectangularShape#getBounds() getBounds} in that the
1062-
* {@code getBounds} method only returns the bounds of the
1063-
* enclosing ellipse of this {@code Arc2D} without considering
1064-
* the starting and ending angles of this {@code Arc2D}.
1065-
*
1066-
* @return the {@code Rectangle2D} that represents the arc's
1067-
* framing rectangle.
1056+
* {@inheritDoc java.awt.Shape}
10681057
* @since 1.2
10691058
*/
10701059
public Rectangle2D getBounds2D() {
@@ -1110,6 +1099,15 @@ public Rectangle2D getBounds2D() {
11101099
return makeBounds(x1, y1, x2, y2);
11111100
}
11121101

1102+
/**
1103+
* {@inheritDoc java.awt.Shape}
1104+
* @since 1.2
1105+
*/
1106+
@Override
1107+
public Rectangle getBounds() {
1108+
return getBounds2D().getBounds();
1109+
}
1110+
11131111
/**
11141112
* Constructs a {@code Rectangle2D} of the appropriate precision
11151113
* to hold the parameters calculated to be the framing rectangle
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 1999, 2026, 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 4197755
27+
* @summary Verifies that Arc2D.getBounds() is similar to Arc2D.getBounds2D()
28+
*/
29+
30+
import java.awt.geom.Arc2D;
31+
import java.awt.geom.Point2D;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class Arc2DGetBoundsTest {
36+
public static void main(String[] args) {
37+
// Imagine a circle that represents a compass.
38+
// This arc represents the northern / top quarter.
39+
Arc2D arc = new Arc2D.Double(0, 0, 1000, 1000, 45, 90, Arc2D.PIE);
40+
41+
// Create 8 pie slices, and place a dot in the center of each
42+
List<Point2D> samples = new ArrayList<>();
43+
for (int segment = 0; segment < 8; segment++) {
44+
double theta = -(segment + .5) / 8.0 * 2 * Math.PI;
45+
Point2D p = new Point2D.Double(
46+
500 + 100 * Math.cos(theta),
47+
500 + 100 * Math.sin(theta)
48+
);
49+
samples.add(p);
50+
}
51+
52+
// these assertions have never been known to fail:
53+
assertTrue(!arc.contains(samples.get(0)));
54+
assertTrue(arc.contains(samples.get(1)));
55+
assertTrue(arc.contains(samples.get(2)));
56+
assertTrue(!arc.contains(samples.get(3)));
57+
assertTrue(!arc.contains(samples.get(4)));
58+
assertTrue(!arc.contains(samples.get(5)));
59+
assertTrue(!arc.contains(samples.get(6)));
60+
assertTrue(!arc.contains(samples.get(7)));
61+
62+
assertTrue(arc.getBounds2D().contains(samples.get(0)));
63+
assertTrue(arc.getBounds2D().contains(samples.get(1)));
64+
assertTrue(arc.getBounds2D().contains(samples.get(2)));
65+
assertTrue(arc.getBounds2D().contains(samples.get(3)));
66+
assertTrue(!arc.getBounds2D().contains(samples.get(4)));
67+
assertTrue(!arc.getBounds2D().contains(samples.get(5)));
68+
assertTrue(!arc.getBounds2D().contains(samples.get(6)));
69+
assertTrue(!arc.getBounds2D().contains(samples.get(7)));
70+
71+
72+
assertTrue(arc.getBounds().contains(samples.get(0)));
73+
assertTrue(arc.getBounds().contains(samples.get(1)));
74+
assertTrue(arc.getBounds().contains(samples.get(2)));
75+
assertTrue(arc.getBounds().contains(samples.get(3)));
76+
77+
// these are the assertions that failed before resolving 4197755
78+
assertTrue(!arc.getBounds().contains(samples.get(4)));
79+
assertTrue(!arc.getBounds().contains(samples.get(5)));
80+
assertTrue(!arc.getBounds().contains(samples.get(6)));
81+
assertTrue(!arc.getBounds().contains(samples.get(7)));
82+
}
83+
84+
private static void assertTrue(boolean b) {
85+
if (!b)
86+
throw new Error();
87+
}
88+
}

0 commit comments

Comments
 (0)