• Create Account

### #Actualnkarasch

Posted 09 December 2012 - 06:20 AM

How can I rotate an image in Java? I think I have AffineTransform pretty figured out but I can't figure out how to make a single image rotate without the entire JPanel rotating.

public void draw(Graphics2D g2d) {
rotate.translate(location.x, location.y);
rotate.rotate(rotation);
rotate.translate(-location.x,-location.y);

g2d.setTransform(rotate);
g2d.drawImage(currentAnimation.getCurrentFrame(), (int) location.x, (int) location.y, null);
}


g2d is passed in from the paint method, it is obviously representing the entire contents of the panel. Should every image on the screen have it's own separate instance of graphics so I can only apply the AffineTranformation to it, without applying it to everything else?

Also, all of the coordinates are changing. The force applied by vectors rotate with the everything else. :\

Thanks. None of the search results could clarify this for me.

### #1nkarasch

Posted 09 December 2012 - 05:44 AM

How can I rotate an image in Java? I think I have AffineTransform pretty figured out but I can't figure out how to make a single image rotate without the entire JPanel rotating.

public void draw(Graphics2D g2d) {
rotate.translate(location.x, location.y);
rotate.rotate(rotation);
rotate.translate(-location.x,-location.y);

g2d.setTransform(rotate);
g2d.drawImage(currentAnimation.getCurrentFrame(), (int) location.x, (int) location.y, null);
}


g2d is passed in from the paint method, it is obviously representing the entire contents of the panel. Should every image on the screen have it's own separate instance of graphics so I can only apply the AffineTranformation to it, without applying it to everything else?

Thanks. None of the search results could clarify this for me.

PARTNERS