Skip to content

4197755: Arc2D.getBounds() returns an unnecessarily large bounding box #26715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/java.desktop/share/classes/java/awt/geom/Arc2D.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,7 @@

package java.awt.geom;

import java.awt.Rectangle;
import java.io.IOException;
import java.io.Serial;
import java.io.Serializable;
Expand Down Expand Up @@ -1052,19 +1053,7 @@ public void setFrame(double x, double y, double w, double h) {
}

/**
* Returns the high-precision framing rectangle of the arc. The framing
* rectangle contains only the part of this {@code Arc2D} that is
* in between the starting and ending angles and contains the pie
* wedge, if this {@code Arc2D} has a {@code PIE} closure type.
* <p>
* This method differs from the
* {@link RectangularShape#getBounds() getBounds} in that the
* {@code getBounds} method only returns the bounds of the
* enclosing ellipse of this {@code Arc2D} without considering
* the starting and ending angles of this {@code Arc2D}.
*
* @return the {@code Rectangle2D} that represents the arc's
* framing rectangle.
* {@inheritDoc}
* @since 1.2
*/
public Rectangle2D getBounds2D() {
Expand Down Expand Up @@ -1110,6 +1099,15 @@ public Rectangle2D getBounds2D() {
return makeBounds(x1, y1, x2, y2);
}

/**
* {@inheritDoc}
* @since 1.2
*/
@Override
public Rectangle getBounds() {
return getBounds2D().getBounds();
}

/**
* Constructs a {@code Rectangle2D} of the appropriate precision
* to hold the parameters calculated to be the framing rectangle
Expand Down
88 changes: 88 additions & 0 deletions test/jdk/java/awt/geom/Arc2D/Arc2DGetBoundsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 4197755
* @summary Verifies that Arc2D.getBounds() is similar to Arc2D.getBounds2D()
*/

import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

public class Arc2DGetBoundsTest {
public static void main(String[] args) {
// Imagine a circle that represents a compass.
// This arc represents the northern / top quarter.
Arc2D arc = new Arc2D.Double(0, 0, 1000, 1000, 45, 90, Arc2D.PIE);

// Create 8 pie slices, and place a dot in the center of each
List<Point2D> samples = new ArrayList<>();
for (int segment = 0; segment < 8; segment++) {
double theta = -(segment + .5) / 8.0 * 2 * Math.PI;
Point2D p = new Point2D.Double(
500 + 100 * Math.cos(theta),
500 + 100 * Math.sin(theta)
);
samples.add(p);
}

// these assertions have never been known to fail:
assertTrue(!arc.contains(samples.get(0)));
assertTrue(arc.contains(samples.get(1)));
assertTrue(arc.contains(samples.get(2)));
assertTrue(!arc.contains(samples.get(3)));
assertTrue(!arc.contains(samples.get(4)));
assertTrue(!arc.contains(samples.get(5)));
assertTrue(!arc.contains(samples.get(6)));
assertTrue(!arc.contains(samples.get(7)));

assertTrue(arc.getBounds2D().contains(samples.get(0)));
assertTrue(arc.getBounds2D().contains(samples.get(1)));
assertTrue(arc.getBounds2D().contains(samples.get(2)));
assertTrue(arc.getBounds2D().contains(samples.get(3)));
assertTrue(!arc.getBounds2D().contains(samples.get(4)));
assertTrue(!arc.getBounds2D().contains(samples.get(5)));
assertTrue(!arc.getBounds2D().contains(samples.get(6)));
assertTrue(!arc.getBounds2D().contains(samples.get(7)));


assertTrue(arc.getBounds().contains(samples.get(0)));
assertTrue(arc.getBounds().contains(samples.get(1)));
assertTrue(arc.getBounds().contains(samples.get(2)));
assertTrue(arc.getBounds().contains(samples.get(3)));

// these are the assertions that failed before resolving 4197755
assertTrue(!arc.getBounds().contains(samples.get(4)));
assertTrue(!arc.getBounds().contains(samples.get(5)));
assertTrue(!arc.getBounds().contains(samples.get(6)));
assertTrue(!arc.getBounds().contains(samples.get(7)));
}

private static void assertTrue(boolean b) {
if (!b)
throw new Error();
}
}