1 package com.genimen.djeneric.ui; 2 3 import java.awt.Color ; 4 import java.awt.Component ; 5 import java.awt.Font ; 6 import java.awt.FontMetrics ; 7 import java.awt.Graphics ; 8 import java.awt.Graphics2D ; 9 import java.awt.geom.AffineTransform ; 10 11 import javax.swing.Icon ; 12 import javax.swing.JLabel ; 13 import javax.swing.SwingConstants ; 14 import javax.swing.SwingUtilities ; 15 import javax.swing.UIManager ; 16 17 20 public class DjVerticalTextIcon implements Icon , SwingConstants 21 { 22 57 private Font font = UIManager.getFont("Label.font"); 58 FontMetrics metrics = new JLabel ().getFontMetrics(font); 59 60 private String text; 61 private int width, height; 62 private boolean clockwize; 63 64 public DjVerticalTextIcon(String text, boolean clockwize) 65 { 66 this.text = text; 67 width = SwingUtilities.computeStringWidth(metrics, text); 68 height = metrics.getHeight(); 69 this.clockwize = clockwize; 70 } 71 72 public void paintIcon(Component c, Graphics g, int x, int y) 73 { 74 Graphics2D g2 = (Graphics2D ) g; 75 Font oldFont = g.getFont(); 76 Color oldColor = g.getColor(); 77 AffineTransform oldTransform = g2.getTransform(); 78 79 g.setFont(font); 80 g.setColor(Color.black); 81 if (clockwize) 82 { 83 g2.translate(x + getIconWidth(), y); 84 g2.rotate(Math.PI / 2); 85 } 86 else 87 { 88 g2.translate(x, y + getIconHeight()); 89 g2.rotate(-Math.PI / 2); 90 } 91 g.drawString(text, 0, metrics.getLeading() + metrics.getAscent()); 92 93 g.setFont(oldFont); 94 g.setColor(oldColor); 95 g2.setTransform(oldTransform); 96 } 97 98 public int getIconWidth() 99 { 100 return height; 101 } 102 103 public int getIconHeight() 104 { 105 return width; 106 } 107 108 } | Popular Tags |