Search code examples
javatranslate

java translate rectangle 2D


I want to translate a rectangle 2D using a double x and double y. It seems that rectangle2D does not support the translate method. What is the appropriate method to use?


Solution

  • If you need it to remain a Rectangle2D, you might have to write your own custom method that just adds to the x and y.

    You can also perform arbitrary transformations with an AffineTransform, but this will turn it into a generic Shape (since that transformation might make it no longer rectangular).

    AffineTransform at = AffineTransform.getTranslateInstance(tx, ty);
    Shape transformed = at.createTransformedShape(rectangle);