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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused. Isn't this deleted doc, in fact the doc that you want on both methods now.
Although 'high precision' if it means float, not int return coords, might not be part of the other method spec.
Also there is a JCK test that fails with this change. Looks like it expects the looser bounds for getBounds().
So we'd definitely need a CSR.
And there'd be a practical compatibility issue as well for someone who relied on those looser bounds.
That's what worries me most.

Also (minor) from JBS I see this comment -
"It looks like the math in getBounds2D could be greatly simplified - at which
point it would make sense to have getBounds also return a tighter bounding
box."

which suggests to me that performance was part of the original reason for the looser bounds of getBounds()

Copy link
Contributor Author

@mickleness mickleness Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Everything that follows is very subjective; feel free to disagree.)

Isn't this deleted doc, in fact the doc that you want on both methods now..

The previous javadoc existed because it was explaining a very unusual distinction between Arc2D.getBounds() and Arc2D.getBounds2D() that does not exist in any other Shape. That distinction/difference is the crux of JDK-4197755. It was worth explaining precisely because it was unintuitive/weird.

IMO we don't need any additional javadoc around these methods now (with this PR), because now they are behaving like most developers assume they would. Most other Shape implementations already follow the same pattern, and they don't offer any accompanying method documentation. If I was redesigning the Shape interface today from scratch, I would probably go so far as to include:

    default public Rectangle getBounds() {
        return getBounds2D().getBounds();
    }

    default public Rectangle2D getBounds2D() {
        return Path2D.getBounds2D(getPathIterator(null));
    }

I'd argue this is the "normal" behavior that most developers expect. We should only override those methods if a specific alternative is faster and approximately as accurate.

(This case study is also weird because Arc2D extends RectangularShape. The shape of a slice of pie does not have a "is-a" relationship with the shape of its pie tin. And yet without this PR: Arc2D inherits getBounds() from RectangularShape.)

And there'd be a practical compatibility issue as well for someone who relied on those looser bounds.

I agree; that's always a risk. We took the same risk when we resolved JDK-8176501 ; is there room to argue this PR is riskier than JDK-8176501 's resolution?

If this PR is too controversial: I'm fine with closing it. I see you reopened JDK-4197755, which is the thing I feel most strongly about here. (That is: keeping the ticket database up-to-date and fixing 4197755's status is enough for me.)

I just thought if I was going to call out reopening 4197755: it'd be trivial to also submit this PR for consideration.

which suggests to me that performance was part of the original reason for the looser bounds of getBounds()

Hmm. Maybe. I'd just add: RectangularShape provides a getBounds() implementation, but it does NOT provide a getBounds2D() implementation. So I'd suggest getBounds() is looser simply because of legacy design decisions. I do think it's peculiar the Arc2D authors anticipated this complaint and didn't act on it, but I'll probably never know exactly how those decisions were made.

* {@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();
}
}