1 /* 2 * JVerticalLabel.java 3 * 4 * Copyright (C) 2006-2014 Andrew Rambaut 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 21 package figtree.ui.components; 22 23 import javax.swing.*; 24 25 /** 26 * 27 * @version $Id$ 28 * 29 * $HeadURL$ 30 * 31 * $LastChangedBy$ 32 * $LastChangedDate$ 33 * $LastChangedRevision$ 34 */ 35 public class JVerticalLabel extends JLabel { 36 private boolean clockwise; 37 JVerticalLabel(boolean clockwise)38 public JVerticalLabel(boolean clockwise) { 39 super(); 40 this.clockwise = clockwise; 41 } 42 JVerticalLabel(Icon image, boolean clockwise)43 public JVerticalLabel(Icon image, boolean clockwise) { 44 super(image); 45 this.clockwise = clockwise; 46 } 47 JVerticalLabel(Icon image, int horizontalAlignment, boolean clockwise)48 public JVerticalLabel(Icon image, int horizontalAlignment, boolean clockwise) { 49 super(image, horizontalAlignment); 50 this.clockwise = clockwise; 51 } 52 JVerticalLabel(String text, boolean clockwise)53 public JVerticalLabel(String text, boolean clockwise) { 54 super(text); 55 this.clockwise = clockwise; 56 } 57 JVerticalLabel(String text, Icon image, int horizontalAlignment, boolean clockwise)58 public JVerticalLabel(String text, Icon image, int horizontalAlignment, boolean clockwise) { 59 super(text, image, horizontalAlignment); 60 this.clockwise = clockwise; 61 } 62 JVerticalLabel(String text, int horizontalAlignment, boolean clockwise)63 public JVerticalLabel(String text, int horizontalAlignment, boolean clockwise) { 64 super(text, horizontalAlignment); 65 this.clockwise = clockwise; 66 } 67 getPreferredSize()68 public java.awt.Dimension getPreferredSize() { 69 java.awt.Insets ins = getInsets(); 70 java.awt.FontMetrics fm = getFontMetrics(getFont()); 71 String text = getText(); 72 int h = fm.stringWidth(text), descent = fm.getDescent(), 73 ascent = fm.getAscent(); 74 return new java.awt.Dimension(ins.top + ascent + descent + ins.bottom, 75 ins.right + h + ins.left); 76 } 77 paint(java.awt.Graphics g)78 public void paint(java.awt.Graphics g) { 79 java.awt.Graphics2D g2d = (java.awt.Graphics2D) g.create(); 80 81 String text = getText(); 82 java.awt.Dimension size = getSize(); 83 java.awt.Insets ins = getInsets(); 84 85 java.awt.FontMetrics fm = g2d.getFontMetrics(getFont()); 86 int h = fm.stringWidth(text), x = ins.right; 87 88 switch (getHorizontalAlignment()) { 89 case SwingConstants.CENTER: 90 x = (size.height - h + ins.right - ins.left) / 2; 91 break; 92 case SwingConstants.TOP: 93 x = size.height - h - ins.left; 94 break; 95 } 96 int descent = fm.getDescent(), ascent = fm.getAscent(), 97 y = ins.top + ascent; 98 switch (getVerticalAlignment()) { 99 case SwingConstants.CENTER: 100 y = (size.width + ascent - descent + ins.top - ins.bottom) / 2; 101 break; 102 case SwingConstants.RIGHT: 103 y = size.width - descent - ins.bottom; 104 break; 105 } 106 107 java.awt.geom.AffineTransform trans; 108 109 if (clockwise) { 110 trans = new java.awt.geom.AffineTransform(0, 1, -1, 0, -size.height, 0); 111 } else { 112 trans = new java.awt.geom.AffineTransform(0, -1, 1, 0, 0, size.height); 113 } 114 g2d.transform(trans); 115 g2d.setPaintMode(); 116 if (isOpaque() && (getBackground() != null)) { 117 g2d.setColor(getBackground()); 118 g2d.fillRect(0, 0, size.height, size.width); 119 } 120 g2d.setFont(getFont()); 121 g2d.setColor(getForeground()); 122 g2d.drawString(text, x, y); 123 trans = null; 124 g2d = null; 125 } 126 } 127 128 129 130