title: Java document image scaling and rotation
body: <p>I have document images of varying dimensions and I want to be able to efficiently scale and rotate them in the following manner (standard "Rotate" and "Zoom" logic).  How do I do it?</p>
  
  <p>An image is H pixels high and W pixels wide.  Initially, it should scale to 600 pixels wide.  On each rotation, the panel's width and height should swap and the scaled image should rotate 90 degrees.  On each zoom, the image should scale by factor "scale".</p>
  
  <p>Here's what I've tried so far on <code>BufferedImage img</code>... the resulting BufferedImage scales and rotates but does not translate (to be centered atop the panel after a 90-degree rotation):</p>
  
  <pre><code>double scale = zoom * 600.0 / img.getWidth();
  rotation = (rotation + degrees) % 360;
  int scaledWidth = (int)(scale * img.getWidth());
  int scaledHeight = (int)(scale * img.getHeight());
  BufferedImage bufferedImage = new BufferedImage(scaledWidth, scaledHeight, img.getType());
  if (rotation % 180 == 0)
      bufferedImage = new BufferedImage(scaledWidth, scaledHeight, img.getType());
  else
      bufferedImage = new BufferedImage(scaledHeight, scaledWidth, img.getType());
  
  AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(rotation), scaledWidth/2, scaledHeight/2);
  transform.scale(scale, scale);
  AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
  scaledImage = operation.filter(img, bufferedImage);
  imagePanel.setPreferredSize(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));
  </code></pre>