Reputation: 68
How do i rotate an image in the center if itself? this code works, but it rotates the image in the top-left corner of the screen:
AffineTransform oldtrans = new AffineTransform();
AffineTransform trans = new AffineTransform();
trans.setToIdentity();
trans.rotate(Math.toRadians(angle));
trans.translate(_x-(width/2), _y-(height/2));
g.setTransform(trans);
g.drawImage(this.getImage(), (int)_x, (int)_y, (int)width, (int)height, null);
trans.setToIdentity();
g.setTransform(oldtrans);
Please help!!!
Upvotes: 4
Views: 8167
Reputation: 11913
You should give two more arguments in your rotate() call:
affineTransform.rotate(Math.toRadians(angle), m_imageWidth/2, m_imageHeight/2);
Upvotes: 9
Reputation: 4182
You're halfway there, literally. What you have to do is two translations. Something along the lines of:
Hope this works for you.
Upvotes: 4