title: Problems rotating BufferedImage
body: <p>I have some problems with rotating images in Java using the AffineTransform class.</p>
  
  <p>I have the following method for creating a rotated (90 degrees) copy of an image:</p>
  
  <pre><code>private BufferedImage createRotatedCopy(BufferedImage img, Rotation rotation) {
      int w = img.getWidth();
      int h = img.getHeight();
  
      BufferedImage rot = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB);
  
      double theta;
      switch (rotation) {
          case CLOCKWISE:
              theta = Math.PI / 2;
              break;
          case COUNTERCLOCKWISE:
              theta = -Math.PI / 2;
              break;
          default:
              throw new AssertionError();
      }
  
      AffineTransform xform = AffineTransform.getRotateInstance(theta, w / 2, h / 2);
      Graphics2D g = (Graphics2D) rot.createGraphics();
      g.drawImage(img, xform, null);
      g.dispose();
  
      return rot;
  }
  </code></pre>
  
  <p>Rotation is a simple enum with the values NONE, CLOCKWISE and COUNTERCLOCKWISE.</p>
  
  <p>The symptoms of my problems are displayed here:</p>
  
  <p><a href="http://perp.se/so/rotate_problems.html" rel="noreferrer">http://perp.se/so/rotate_problems.html</a></p>
  
  <p>So, the rotation works OK, but the resulting images aren't anchored to the correct coordinates (or how one should put it). And since I don't really know what the heck I'm doing in the first place (my linear algebra is weak), I don't know how to solve this on my own.</p>
  
  <p>I've tried with some random fiddling with the AffineTransform instance, but it hasn't helped me (of course). I've tried googling (and searching SO), but all examples I've seen basically use the same approach as I do... which doesn't work for me.</p>
  
  <p>Thankful for advice.</p>