Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java rotate an image in the center

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!!!

like image 930
Narayan Avatar asked Oct 16 '25 19:10

Narayan


1 Answers

You should give two more arguments in your rotate() call:

affineTransform.rotate(Math.toRadians(angle), m_imageWidth/2, m_imageHeight/2); 
like image 192
Hidde Avatar answered Oct 19 '25 08:10

Hidde