/* * Copyright (c) 2009, SQL Power Group Inc. * * This file is part of SQL Power Library. * * SQL Power Library is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * SQL Power Library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package ca.sqlpower.swingui; import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; /** * A panel that draws text vertically */ public class VerticalTextPanel extends JPanel { private String text; public VerticalTextPanel(String text) { this.text = text; } @Override public Dimension getPreferredSize() { FontMetrics fm = getFontMetrics(getFont()); int height = fm.getHeight(); int width = fm.stringWidth(text); return new Dimension(height, width); // yup, width and height are swapped on purpose } @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g.create(); g2.setColor(getBackground()); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setColor(getForeground()); g2.rotate(-Math.PI/2); FontMetrics fm = getFontMetrics(getFont()); int width = fm.stringWidth(text); g2.drawString(text, -width-10, getWidth()/2 + fm.getAscent()/2 ); } public String getText() { return text; } public void setText(String text) { this.text = text; } }