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. enter image description here 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.

enter image description here

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()

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

      int x = xLoc;
      int y = yLoc;


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


      displayImg = temp;

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

Any help would be greatly appreciated.


1 Answer 1


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!

  • 1
    \$\begingroup\$ 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 ); \$\endgroup\$
    – Caboose
    Sep 15, 2013 at 6:03
  • \$\begingroup\$ 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. \$\endgroup\$ Sep 17, 2013 at 15:27
  • \$\begingroup\$ 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! \$\endgroup\$
    – Caboose
    Sep 18, 2013 at 14:38
  • \$\begingroup\$ No problem, glad I could assist. \$\endgroup\$ Sep 21, 2013 at 0:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .