001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.font;
020:
021: import java.awt.Color;
022: import java.awt.Dimension;
023: import java.awt.Font;
024: import java.awt.FontMetrics;
025: import java.awt.Graphics;
026: import java.awt.Graphics2D;
027: import java.awt.Insets;
028:
029: import javax.swing.JComponent;
030:
031: /**
032: * Displays the current font
033: *
034: * @author Jeff Tassin
035: */
036: public class FontSampleComponent extends JComponent {
037: private static final String TEXT_SAMPLE = "AaBbYyZz 012345...";
038: /**
039: * The current font
040: */
041: private Font m_font;
042:
043: /**
044: * The preferred size
045: */
046: private Dimension m_pref_size;
047:
048: /**
049: * ctor
050: */
051: public FontSampleComponent(Font f) {
052: m_font = f;
053: if (f == null) {
054: m_font = javax.swing.UIManager.getFont("Table.font");
055: }
056: setOpaque(true);
057: setBorder(javax.swing.BorderFactory
058: .createLineBorder(Color.black));
059: }
060:
061: /**
062: * @return the preferred size for this component
063: */
064: public Dimension getPreferredSize() {
065: if (m_pref_size == null) {
066: m_pref_size = new Dimension(50, 20);
067: }
068: return m_pref_size;
069: }
070:
071: /**
072: * Paints the line
073: */
074: public void paintComponent(Graphics g) {
075: if (m_font == null)
076: return;
077:
078: Graphics2D g2 = (Graphics2D) g;
079: Font old_font = g2.getFont();
080: Color old_color = g2.getColor();
081:
082: Insets insets = getInsets();
083: int height = getHeight() - insets.top - insets.bottom;
084: int width = getWidth() - insets.left - insets.right;
085:
086: g2.setColor(Color.white);
087: g2.fillRect(insets.left, insets.top, width, height);
088:
089: g2.setColor(Color.black);
090: g2.setFont(m_font);
091:
092: FontMetrics fm = g.getFontMetrics();
093:
094: int line_height = fm.getHeight();
095: int y = height - (height - line_height) / 2 - fm.getDescent();
096: if (y < 0)
097: y = line_height;
098:
099: int str_width = fm.stringWidth(TEXT_SAMPLE);
100: int x = (width - str_width) / 2;
101: if (x < 0)
102: x = 0;
103:
104: g.drawString(TEXT_SAMPLE, x, y);
105:
106: g2.setColor(old_color);
107: g2.setFont(old_font);
108: }
109:
110: /**
111: * Sets the current font
112: */
113: public void setFont(Font f) {
114: m_font = f;
115: revalidate();
116: repaint();
117: }
118: }
|