Creating a vertical JLabel

Code currently works only for 90 or -90 degrees only. It was written to support the answer to the question “How do I create vertically oriented JTable headers”.

Main.java:

import javax.swing.plaf.basic.*;
import javax.swing.border.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main extends JFrame
{
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
 
      getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
 
      JLabel normalLabel = new JLabel("Normal JLabel");
      normalLabel.setBorder(LineBorder.createBlackLineBorder());
 
      JLabel verticalLabel1 = new JLabel("Vertical JLabel, 90 degrees");
      verticalLabel1.setUI(new VerticalLabelUI(90));
      verticalLabel1.setBorder(LineBorder.createBlackLineBorder());
 
      JLabel verticalLabel2 = new JLabel("Vertical JLabel, -90 degrees");
      verticalLabel2.setUI(new VerticalLabelUI(-90));
      verticalLabel2.setBorder(LineBorder.createBlackLineBorder());
 
      getContentPane().add(normalLabel);
      getContentPane().add(verticalLabel1);
      getContentPane().add(verticalLabel2);
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}
 
class VerticalLabelUI extends BasicLabelUI
{
   protected float degrees;
   private static Rectangle paintIconR = new Rectangle();
   private static Rectangle paintTextR = new Rectangle();
   private static Rectangle paintViewR = new Rectangle();
   private static Insets paintViewInsets = new Insets(0, 0, 0, 0);
 
   VerticalLabelUI(float degrees) {
      this.degrees = degrees;
   }
 
   public Dimension getPreferredSize(JComponent c) {
      Dimension dim = super.getPreferredSize(c);
      return new Dimension( dim.height, dim.width );
   }
 
   public void paint(Graphics g, JComponent c) {
      JLabel label = (JLabel)c;
      String text = label.getText();
      Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
 
      if ((icon == null) && (text == null)) {
         return;
      }
 
      FontMetrics fm = g.getFontMetrics();
      paintViewInsets = c.getInsets(paintViewInsets);
 
      paintViewR.x = paintViewInsets.left;
      paintViewR.y = paintViewInsets.top;    	
      paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
      paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
 
      paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
      paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
 
      String clippedText = 
            layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
 
      Graphics2D g2d = (Graphics2D) g;
      // save old value
      AffineTransform affineTransformOld = g2d.getTransform();
 
      double radians = degrees * (Math.PI / 180); 
      g2d.rotate(radians);
      Dimension d = getPreferredSize(c);      
      if (degrees == 90) {
         g2d.translate(0,-d.width);
      } 
      else {
         g2d.translate(-d.height, 0);
      }
 
      if (icon != null) {
         icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
      }
 
      if (text != null) {
         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
         if (v != null) {
            v.paint(g, paintTextR);
         }
         else {
            int textX = paintTextR.x;
            int textY = paintTextR.y + fm.getAscent();
 
            if (label.isEnabled()) {
               paintEnabledText(label, g, clippedText, textX, textY);
            }
            else {
               paintDisabledText(label, g, clippedText, textX, textY);
            }
         }
      } 	
    	
      g2d.setTransform(affineTransformOld);
   }
}