[SOLVED] Help Java2D Moving Bounds: Why won't this work?

Why doesn’t this code output “45, 67”?

import java.awt.geom.Ellipse2D;

public class BoundsTest {
  public static void main(String[] args) {
    Ellipse2D e = new Ellipse2D.Float(0, 0, 20, 20);
    e.getBounds().setLocation(45, 67);
    System.out.println(e.getBounds().x + ", " + e.getBounds().y);
  }
}

Because getBounds() returns a new Rectangle, so changing it doesn’t change the ellipse. Try using one of the setFrame(…) methods instead.

I love that Ellipse2D extends RectangularShape - the only place on Earth where a circle is a square! ;D (yes, I know what they mean)

The Rectangle returned by getBounds is a new object, so changing its shape doesn’t affect the shape you got it from. If you want to apply your transformations to the bounds, use the setFrame method, like this:


import java.awt.geom.Ellipse2D;
import java.awt.Rectangle;

public class BoundsTest {
  public static void main(String[] args) {
    Ellipse2D e = new Ellipse2D.Float(0, 0, 20, 20);
    Rectangle r = e.getBounds();
    r.setLocation(45, 67);
    e.setFrame(r);
    System.out.println(e.getBounds().x + ", " + e.getBounds().y);
  }
}

I see, thanks.