title: Rotate Buffered Image (diagonal Text)
body: <p>(Sorry for the links, I'm new and I cannot post images)</p>
  
  <p>I want to accomplish the following : create a table with the legends on the top, and in a diagonal way. </p>
  
  <p><img src="https://i.stack.imgur.com/QqnuB.png" alt="http://i.stack.imgur.com/HqtJL.png"></p>
  
  <p>but I'm having some problems, I have the following image, and I'm trying to rotate it 45º (the result it's at the right),</p>
  
  <p><img src="https://i.stack.imgur.com/KOlIK.png" alt="http://i.stack.imgur.com/KOlIK.png"></p>
  
  <p>Here is my code:</p>
  
  <pre><code>//just some labels
  ArrayList&lt;String&gt; labels = new ArrayList&lt;String&gt;();
  labels.add("Juan");
  labels.add("QWERTYYY");
  labels.add("ANA");
  
  // margin
  int margin=3;
  
  //diagonal = 45º
  // value to shift each label 
  int diagonalShift = (int)(cellSizeWidth / Math.sqrt(2d));
  
  // height, width represent the size of the final image
  // heightSub, widthSub represent the size of the image to be rotated taking into account the shift for each label
  int widthSub = height + (diagonalShift * labels.size());
  int heightSub = width;
  
  
  // image to Display 
  BufferedImage image = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
  Graphics2D imageGraphics = (Graphics2D) image.getGraphics();
  
  // tempImage: subImage to rotate and place in image
  BufferedImage tempImage = new BufferedImage(widthSub, heightSub, BufferedImage.TYPE_INT_RGB);
  Graphics2D tempImageGraphics = (Graphics2D) tempImage.getGraphics();
  tempImageGraphics.setColor(Color.BLUE);
  tempImageGraphics.drawRect(0, 0, widthSub-1, heightSub-1);
  
  // I'd like to use antialias, but it's giving bad results
  // tempImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  
  
  // drawing labels
  // as we're designing a table cellSizeWidth and CellSizeHeight represent the dimensions for each cell
  tempImageGraphics.setColor(Color.WHITE);
  for (int i = 0; i &lt; labels.size(); i++) {
      String label = labels.get(i);
      tempImageGraphics.drawString(label,
                                   margin + (i * diagonalShift),
                                   (int) (i * cellSizeWidth) + fontSize + centerDistance);
  }
  </code></pre>
  
  <p>I tried the following:</p>
  
  <pre><code>//rotating
  AffineTransform fontAfineTransform = new AffineTransform();
  // fontAfineTransform.rotate(verticalTextDirection.rotationAngle());
  </code></pre>
  
  <p>which gives as result the image at the right in the second Image <a href="https://i.stack.imgur.com/KOlIK.png" rel="nofollow noreferrer">2</a></p>
  
  <p>so I need to apply a translation to get it to the right position</p>
  
  <pre><code>// Math.sqrt(2d) because I'm working with 45º and the height becomes the hypotenuse
  // fontAfineTransform.translate(-height/Math.sqrt(2d),height/Math.sqrt(2d));
  
  
  //drawing into image
  imageGraphics.drawImage(tempImage, fontAfineTransform, null);
  </code></pre>
  
  <p>can someone please explain how the affineTransform works, or how can I get the text to be in  a diagonal way.</p>
  
  <p>Thanks</p>