001: /*
002: * VerticalTextIcon.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Font;
027: import java.awt.FontMetrics;
028: import java.awt.Graphics;
029: import java.awt.Graphics2D;
030: import java.awt.Toolkit;
031: import java.awt.geom.AffineTransform;
032: import javax.swing.Icon;
033: import javax.swing.SwingConstants;
034: import javax.swing.SwingUtilities;
035: import javax.swing.UIManager;
036:
037: /* ----------------------------------------------------------
038: * CVS NOTE: Changes to the CVS repository prior to the
039: * release of version 3.0.0beta1 has meant a
040: * resetting of CVS revision numbers.
041: * ----------------------------------------------------------
042: */
043:
044: /**
045: * Vertical text icon derived from the original by
046: * Santhosh Kumar http://jroller.com/pages/santhosh.
047: *
048: * @author Santhosh Kumar
049: * @author Takis Diakoumis
050: * @version $Revision: 1.4 $
051: * @date $Date: 2006/05/14 06:56:07 $
052: */
053: public class VerticalTextIcon implements Icon, SwingConstants {
054:
055: private Font font = UIManager.getFont("Label.font");
056: private FontMetrics fm = Toolkit.getDefaultToolkit()
057: .getFontMetrics(font);
058:
059: private String text;
060: private int width, height;
061: private boolean clockwize;
062:
063: public VerticalTextIcon(String text, boolean clockwize) {
064: this .text = text;
065: width = SwingUtilities.computeStringWidth(fm, text);
066: height = fm.getHeight();
067: this .clockwize = clockwize;
068: }
069:
070: public void paintIcon(Component c, Graphics g, int x, int y) {
071: Graphics2D g2 = (Graphics2D) g;
072: Font oldFont = g.getFont();
073: Color oldColor = g.getColor();
074: AffineTransform oldTransform = g2.getTransform();
075:
076: g.setFont(font);
077: g.setColor(Color.black);
078:
079: if (clockwize) {
080: g2.translate(x + getIconWidth(), y);
081: g2.rotate(Math.PI / 2);
082: } else {
083: g2.translate(x, y + getIconHeight());
084: g2.rotate(-Math.PI / 2);
085: }
086:
087: g.drawString(text, 0, fm.getLeading() + fm.getAscent());
088:
089: g.setFont(oldFont);
090: g.setColor(oldColor);
091: g2.setTransform(oldTransform);
092: }
093:
094: public int getIconWidth() {
095: return height;
096: }
097:
098: public int getIconHeight() {
099: return width;
100: }
101: }
|