/*
* VerticalLabelDemo.java
*/
//package paint;
import java.awt.*;
import javax.swing.*;
public class VerticalLabelDemo extends JFrame {
public VerticalLabelDemo() {
super("Vertical Label Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 325);
setLocationRelativeTo(null);
VerticalLabel label = new VerticalLabel("Dies ist ein Beispiel eines senkrechten Labels");
label.setPreferredSize(new Dimension(50,300));
label.setForeground( Color.BLUE );
JPanel panel = new JPanel();
panel.add(label);
add(panel, BorderLayout.WEST);
}
public static void main(String args[]) {new VerticalLabelDemo().setVisible(true);}
}
class VerticalLabel extends JLabel{
private static final double ROTATION = -(Math.PI / 2);
private String text = "";
private Font font = UIManager.getFont("Label.font");
public VerticalLabel(String text) {
this.text = text;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Rectangle viewRect = getBounds();
int halfWidth = viewRect.width / 2;
int halfHeight = viewRect.height / 2;
Graphics2D graphics2D = (Graphics2D) g;
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setFont(font);
Rectangle textRect = new Rectangle();
graphics2D.rotate(ROTATION, halfWidth, halfHeight);
SwingUtilities.layoutCompoundLabel(this,
graphics2D.getFontMetrics(), text, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, new Rectangle(), textRect, 0);
graphics2D.setColor(getForeground());
graphics2D.drawString(text, -textRect.width*2, halfHeight);
}
}