Java rotate rectangle around center


trs:

I want to rotate a rectangle around its center point, it should stay where it should be drawn and rotate in that space

Here is my code:

AffineTransform transform = new AffineTransform();

    transform.rotate(Math.toRadians(45),rectangle.width/2, rectangle.height/2);
    Shape transformed = transform.createTransformedShape(rectangle);
    g2.fill(transformed)

The rectangle is rotated but drawn on a different part of the screen, how can I correct it?

and W:

I haven't tried it, but it looks like you're not getting the correct middle of the rectangle. try:

AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(45), rectangle.getX() + rectangle.width/2, rectangle.getY() + rectangle.height/2);
g2.fill(transformed);

The difference is that you are adding the width to the starting X point and the height to the starting Y point, which is the middle of the rectangle.

Hope this helps.

Related


Java rotate rectangle around center

trs: I want to rotate a rectangle around its center point, it should stay where it should be drawn and rotate in that space Here is my code: AffineTransform transform = new AffineTransform(); transform.rotate(Math.toRadians(45),rectangle.width/2, rectangl

Java rotate rectangle around center

trs: I want to rotate a rectangle around its center point, it should stay where it should be drawn and rotate in that space Here is my code: AffineTransform transform = new AffineTransform(); transform.rotate(Math.toRadians(45),rectangle.width/2, rectangl

MapKit rotate rectangle around center

Igor Vojtenko I need to rotate a rectangle around its center, the rectangle is a polygon on the map kit, I have 4 points and a center. I rotate each point separately and then create new polygons. I use this code: if let rotation = rotation, let center = zo

Rotate rectangle around arc in Java swing

Anish Shah I want to move this little rectangle around the circumference of the circle so that it looks and moves like a canon. code private void doDrawing(Graphics g){ g.setColor(Color.BLUE); g.fillArc(-CANON_RADIUS/2, this.getHeight()-CANON_RADIUS/2,

Rotate rectangle around arc in Java Swing

Anish Shah I want to move this little rectangle around the circumference of the circle so it looks and moves like a canon. code private void doDrawing(Graphics g){ g.setColor(Color.BLUE); g.fillArc(-CANON_RADIUS/2, this.getHeight()-CANON_RADIUS/2, CANO

Rotate rectangle around arc in Java Swing

Anish Shah I want to move this little rectangle around the circumference of the circle so it looks and moves like a canon. code private void doDrawing(Graphics g){ g.setColor(Color.BLUE); g.fillArc(-CANON_RADIUS/2, this.getHeight()-CANON_RADIUS/2, CANO

Rotate SVG rectangle around its center using vanilla JavaScript

Hasan A Yousef This is not a duplicate of another question. I found this discussion about using XML to rotate around a center, trying to use vanilla JavaScript to achieve the same rotation, rotate(45, 60, 60)but this approach didn't work for me. The method tha

Rotate SVG rectangle around its center using vanilla JavaScript

Hasan A Yousef This is not a duplicate of another question. I found this discussion about using XML to rotate around a center, trying to use vanilla JavaScript to achieve the same rotation, rotate(45, 60, 60)but this approach didn't work for me. The method tha

Java: tilted image rotate around its center

Igor Tupitsyn: I'm trying to rotate a buffered image (flat icon on a map) around its center in Java using the help here: Rotate BufferedImage instance When I use this code: AffineTransform at = new AffineTransform(); at.rotate(Math.toRadians(planeHeading),ori

Java: tilted image rotate around its center

Igor Tupitsyn: I'm trying to rotate a buffered image (flat icon on a map) around its center in Java using the help here: Rotate BufferedImage instance When I use this code: AffineTransform at = new AffineTransform(); at.rotate(Math.toRadians(planeHeading),ori

Java: tilted image rotate around its center

Igor Tupitsyn: I'm trying to rotate a buffered image (flat icon on a map) around its center in Java using the help here: Rotate BufferedImage instance When I use this code: AffineTransform at = new AffineTransform(); at.rotate(Math.toRadians(planeHeading),ori

CALayer rotate around center

Alfred West I want to do something like a circular progress bar, draw a bezier curve in a UIView, but it seems to be rotated by PI/2. So I need to rotate it by PI/2. The problem is, it's spinning around a very weird point and I don't know what to do with it. I

Rotate NSView around center

gvalmon I have custom NSView that should rotate around center using CoreAnimation (or any other means available in osx sdk). I've tried many solutions, but the nsview rotates around the bottom left corner instead of the center and always seems to ignore the an

CALayer rotate around center

Alfred West I want to do something like a circular progress bar, draw a bezier curve in a UIView, but it seems to be rotated by PI/2. So I need to rotate it by PI/2. The problem is, it's spinning around a very weird point and I don't know what to do with it. I

Rotate icon around center

chicken eggs I want to rotate this Font Awesome icon around its center, but my current code doesn't do that. I think its anchor is in the top right corner. How can I change it to center? I also want the icon to move to the center of the screen when clicked whi

CSS rotate around center

bad All right. I've done a lot of research, but I can't come up with anything. Thanks for your help after overthinking the problem. I want to be able to rotate a <div>around its center so that its back is visible. I was able to do it - rotateYno problem. But w

CALayer rotate around center

Alfred West I want to do something like a circular progress bar, draw a bezier curve in a UIView, but it seems to be rotated by PI/2. So I need to rotate it by PI/2. The problem is, it's spinning around a very weird point and I don't know what to do with it. I

Rotate icon around center

chicken eggs I want to rotate this Font Awesome icon around its center, but my current code doesn't do that. I think its anchor is in the top right corner. How can I change it to center? I also want the icon to move to the center of the screen when clicked whi

Rotate NSView around center

gvalmon I have custom NSView that should rotate around center using CoreAnimation (or any other means available in osx sdk). I've tried many solutions, but the nsview rotates around the bottom left corner instead of the center and always seems to ignore the an

Rotate NSView around center

gvalmon I have custom NSView that should rotate around center using CoreAnimation (or any other means available in osx sdk). I've tried many solutions, but the nsview rotates around the bottom left corner instead of the center and always seems to ignore the an

CALayer rotate around center

Alfred West I want to do something like a circular progress bar, draw a bezier curve in a UIView, but it seems to be rotated by PI/2. So I need to rotate it by PI/2. The problem is, it's spinning around a very weird point and I don't know what to do with it. I

CSS rotate around center

bad Ok. I've done a lot of research, but I can't come up with anything. Thanks for your help after overthinking the problem. I want to be able to rotate a <div>around its center so that its back is visible. I was able to do it - rotateYno problem. But when in

Rotate NSView around center

gvalmon I have custom NSView that should rotate around center using CoreAnimation (or any other means available in osx sdk). I've tried many solutions, but the nsview rotates around the bottom left corner instead of the center, and always seems to ignore the a

Rotate icon around center

chicken eggs I want to rotate this Font Awesome icon around its center, but my current code doesn't do that. I think its anchor is in the top right corner. How can I change it to center? I also want the icon to move to the center of the screen when clicked whi

Rotate PDF around its center using PDFBox in Java

Sagar PDDocument document = PDDocument.load(new File(input)); PDPage page = document.getDocumentCatalog().getPages().get(0); PDPageContentStream cs = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.PREPEND, false, false); cs.transform(Ma

Rotate PDF around its center using PDFBox in Java

Sagar PDDocument document = PDDocument.load(new File(input)); PDPage page = document.getDocumentCatalog().getPages().get(0); PDPageContentStream cs = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.PREPEND, false, false); cs.transform(Ma

Android Java - postRotate doesn't rotate around center?

vile I believe this is more of a logic question than a Java question, sorry. My intention is simple, I want the ship to move and rotate in a matrix, and the bitmap ship1 is the central axis of the rotation. The code works fine, except the pivot is offset by a