Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating the Rectangle object in java

Tags:

java

rotation

Is it possible to rotate the Rectangle object to a certain degree around it's axis? Is it as easy as Rectangle rect = new Rectangle(x,y,w,h,r)?

If it is not possible to rotate the object, what would be a way I could get similar results?

Edit: for clarity here is my dilemma, I have images that rotate but when they colide with other images the collisions only work at 90 and 180 degree rotations because their hit box Rectangle objects don't rotate.

like image 558
Chris Avatar asked Apr 27 '26 04:04

Chris


1 Answers

Edit: for clarity here is my dilemma, I have images that rotate but when they colide with other images the collisions only work at 90 and 180 degree rotations because their hit box Rectangle objects don't rotate.

You can rotate a Shape-derived object, such as a Rectangle2D by using the AffineTransform method, createTransformedShape(...).

Rectangle2D myRect = new Rectangle2D.Double(100, 100, 200, 200);
AffineTransform at = AffineTransform.getRotateInstance(Math.PI / 4, 150, 150);
Shape rotatedRect = at.createTransformedShape(myRect);

Note: code not compiled nor tested.

like image 103
Hovercraft Full Of Eels Avatar answered Apr 28 '26 17:04

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!