title: Rotate a Swing JLabel
body: <p>I am currently trying to implement a Swing component, inheriting from <code>JLabel</code> which should simply represent a label that can be oriented vertically.</p>
  
  <p>Beginning with this:</p>
  
  <pre><code>public class RotatedLabel extends JLabel {
  
      public enum Direction {
          HORIZONTAL,
          VERTICAL_UP,
          VERTICAL_DOWN
      }
  
  private Direction direction;
  </code></pre>
  
  <p>I thought it's be a nice idea to just alter the results from <code>getPreferredSize()</code>:</p>
  
  <pre><code>@Override
  public Dimension getPreferredSize() {
      // swap size for vertical alignments
      switch (getDirection()) {
          case VERTICAL_UP:
          case VERTICAL_DOWN:
              return new Dimension(super.getPreferredSize().height, super
                  .getPreferredSize().width);
          default:
              return super.getPreferredSize();
      }
  }
  </code></pre>
  
  <p>and then simply transform the <code>Graphics</code> object before I offload painting to the original <code>JLabel</code>:</p>
  
  <pre><code>@Override
  protected void paintComponent(Graphics g) {
      Graphics2D gr = (Graphics2D) g.create();
  
      switch (getDirection()) {
          case VERTICAL_UP:
              gr.translate(0, getPreferredSize().getHeight());
              gr.transform(AffineTransform.getQuadrantRotateInstance(-1));
              break;
          case VERTICAL_DOWN:
              // TODO
              break;
          default:
      }
  
      super.paintComponent(gr);
  }
  </code></pre>
  
  <p>It seems to work—somehow—in that the text is now displayed vertically. However, placement and size are off.</p>
  
  <p>Actually, the width of the background (orange in this case) is identical with the height of the surrounding <code>JFrame</code> which is ... not quite what I had in mind.</p>
  
  <p>Any ideas how to solve that in a proper way? Is delegating rendering to superclasses even encouraged?</p>