Some more code and context would be nice, but based on the current question: You could create a transformed shape. Particularly, Rectangle
and Rectangle2D
implement the Shape
interface. And you can create an AffineTransform
that represents the rotation that you are currently doing to your Graphics
. So the relevant part of the code should roughly look like
Rectangle2D rectangle = ...
AffineTransform at = AffineTransform.getRotateInstance(
angle, centerx, centery);
Shape rotatedRectangle = at.createTransformedShape(rectangle);
g2d.draw(rotatedRectangle);
You mentioned "collision" in the title. If you intend to use this rectangle in some sort of collision detection, you should note that it is not directly possible to intersect two arbitrary Shape
objects. Particulary, you can not intersect the Shape rotatedRectangle
with another Shape otherRotatedRectangle
, but only with a Rectangle otherRectangle
. If this is an issue, you have several options, but this would rather fit into a dedicated question.