1 package org.telscenter.pas.common.ui.button; 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.Toolkit; 10 import java.awt.geom.AffineTransform; 11 12 import javax.swing.Icon; 13 import javax.swing.SwingConstants; 14 import javax.swing.SwingUtilities; 15 16 public class VerticalTextIcon implements Icon, SwingConstants{ 17 private Font font; 18 private FontMetrics fm; 19 20 private String text; 21 private int width, height; 22 private boolean clockwize; 23 private Color fontColor; 24 25 public VerticalTextIcon(String text, boolean clockwize,Color fontColor, Font font){ 26 this.fontColor = fontColor; 27 this.font = font; 28 this.fm = Toolkit.getDefaultToolkit().getFontMetrics(font); 29 this.text = text; 30 width = SwingUtilities.computeStringWidth(fm, text); 31 height = fm.getHeight(); 32 this.clockwize = clockwize; 33 } 34 35 public void paintIcon(Component c, Graphics g, int x, int y){ 36 Graphics2D g2 = (Graphics2D)g; 37 Font oldFont = g.getFont(); 38 Color oldColor = g.getColor(); 39 AffineTransform oldTransform = g2.getTransform(); 40 41 g.setFont(font); 42 g.setColor(fontColor); 43 if(clockwize){ 44 g2.translate(x+getIconWidth(), y); 45 g2.rotate(Math.PI/2); 46 }else{ 47 g2.translate(x, y+getIconHeight()); 48 g2.rotate(-Math.PI/2); 49 } 50 g.drawString(text, 0, fm.getLeading()+fm.getAscent()); 51 52 g.setFont(oldFont); 53 g.setColor(oldColor); 54 g2.setTransform(oldTransform); 55 } 56 57 public int getIconWidth(){ 58 return height; 59 } 60 61 public int getIconHeight(){ 62 return width; 63 } 64 }