# Rotate image around a specified origin

I'm trying to rotate my Turret.png (small Gunhead with a laser in front) towards the mouses location on the screen. I tried just using my method for rotating projectiles towards the next step on their trajectory, but this produces unexpected results. Not only does the image not rotate about a fixed point, it also will clip based on the original images bounds.

My goal is for the turret to rotate about a fixed point on the gun head, so that the laser always points towards the mouse and the gun head is at a fixed position. Here is an example of how I'm trying to get it to rotate; in this example the rotation was 78 degrees done in Illustrator. The red dot represents the origin.

My current code is as follows,

This method updates my Gun Tower and tells the turret the angle to rotate to:

 public void setTargetAndDirection(int x, int y)
{
setTarget(x, y);
deltaX = getTargetX() - getX();
deltaY = getTargetY() - getY();
turret.setRotatedImage(turret.getActiveImg(), -getTargetAngle());

}


This is the method that actually handles the rotation:

      public void setOriginRotate(BufferedImage imageIn, float angle)
{
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();

int transparency = imageIn.getColorModel().getTransparency();
BufferedImage temp =  gc.createCompatibleImage(
imageIn.getWidth(), imageIn.getHeight(), transparency );
Graphics2D g2d = temp.createGraphics();

int x = xLoc;
int y = yLoc;

g2d.setColor(Color.RED);

AffineTransform at = AffineTransform.getTranslateInstance(x, y);
g2d.setTransform(at);
g2d.drawImage(imageIn, 0, 0, null);

g2d.dispose();

displayImg = temp;

dispSizeX = displayImg.getWidth() / sizeMod;
dispSizeY = displayImg.getHeight() / sizeMod;
}


Any help would be greatly appreciated.

Instead of drawing the rotated image out to another image where you then draw that image out to Graphics to be displayed... why don't you draw directly to that Graphics object? This way it saves ALOT more time and doesn't waste memory.

Graphics2D g2d = ...
AffineTransform backup = g2d.getTransform();
AffineTransform trans = new AffineTransform();
trans.rotate( sprite.angle, sprite.x, sprite.y ); // the points to rotate around (the center in my example, your left side for your problem)

g2d.transform( trans );
g2d.drawImage( image, sprite.x, sprite.y );  // the actual location of the sprite

g2d.setTransform( backup ); // restore previous transform


And that should be it!

• By the nature of my code's structure, your method would be very hard to implement. I now understand how to rotate on a fixed point, but my main problem is clipping, which I believe is caused by this BufferedImage temp = gc.createCompatibleImage( imageIn.getWidth(), imageIn.getHeight(), transparency ); – Caboose Sep 15 '13 at 6:03
• You're having a clipping problem because you're drawing out to an image a rotated image, that rotated image is poorly placed on the image and it gets clipped. Avoid writing to that image altogether! A game where you need to draw 50 objects would not be playable with your method of double (essentially triple) drawing images. – ClickerMonkey Sep 17 '13 at 15:27
• I've since seen the error of my ways, and completely revamped how I was handling the drawing. It's working almost perfectly now, I just have to change some things around to tweak where it appears to rotate from.. gonna be some math involved. Thanks for your help! – Caboose Sep 18 '13 at 14:38
• No problem, glad I could assist. – ClickerMonkey Sep 21 '13 at 0:39