001    /*
002     * $Id: TextPainter.java,v 1.5 2006/04/26 02:14:26 joshy Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    
022    package org.jdesktop.swingx.painter;
023    
024    import java.awt.Color;
025    import java.awt.Font;
026    import java.awt.FontMetrics;
027    import java.awt.Graphics2D;
028    import java.awt.Paint;
029    import java.awt.geom.Point2D;
030    import javax.swing.JComponent;
031    import org.jdesktop.swingx.util.Resize;
032    
033    /**
034     * "Paints" text at the given location. The location should be a point where
035     * the x and y values are in the range of 0 to 1. Similar to the CSS background
036     * positioning algorithm, each value will be scaled along each axis of the component
037     * this TextPainter is painting, and then used to position the text. A value of
038     * 0,0 would put the text in the upper lefthand corner. 0.5,0.5 would position the
039     * text in the center and 1.0,1.0 would be at the lower righthand corner. For a more
040     * complete defintion of the positioning algorithm see the 
041     * <a href="http://www.w3.org/TR/CSS21/colors.html#propdef-background-position">CSS 2.1 spec</a>.
042     *
043     * @author rbair
044     */
045    public class TextPainter extends AbstractPainter {
046        private Resize resize;
047        private String text = "";
048        private Font font;
049        private Paint paint;
050        private Point2D location = new Point2D.Double(.0, .0);
051        
052        /** Creates a new instance of TextPainter */
053        public TextPainter() {
054        }
055        
056        public TextPainter(String text) {
057            this(text, new Font("Dialog", Font.PLAIN, 12));
058        }
059        
060        public TextPainter(String text, Font font) {
061            this(text, font, Color.BLACK);
062        }
063        
064        public TextPainter(String text, Font font, Paint paint) {
065            this.text = text;
066            this.font = font;
067            this.paint = paint;
068        }
069        
070        public void setFont(Font f) {
071            Font old = getFont();
072            this.font = f;
073            firePropertyChange("font", old, getFont());
074        }
075        
076        public Font getFont() {
077            return font;
078        }
079        
080        public void setText(String text) {
081            String old = getText();
082            this.text = text == null ? "" : text;
083            firePropertyChange("text", old, getText());
084        }
085        
086        public String getText() {
087            return text;
088        }
089        
090        public void setPaint(Paint paint) {
091            Paint old = getPaint();
092            this.paint = paint;
093            firePropertyChange("paint", old, getPaint());
094        }
095        
096        public Paint getPaint() {
097            return paint;
098        }
099        
100        public void setLocation(Point2D location) {
101            Point2D old = getLocation();
102            this.location = location == null ? new Point2D.Double(.0, .0) : location;
103            firePropertyChange("location", old, getLocation());
104        }
105        
106        public Point2D getLocation() {
107            return location;
108        }
109    
110        protected void paintBackground(Graphics2D g, JComponent component) {
111            Font font = getFont();
112            if (font != null) {
113                g.setFont(font);
114            }
115            
116            Paint paint = getPaint();
117            if (paint != null) {
118                g.setPaint(paint);
119            }
120            
121            FontMetrics metrics = g.getFontMetrics(g.getFont());
122            String text = getText();
123            
124            Point2D location = getLocation();
125            int tw = metrics.stringWidth(text);
126            int th = metrics.getHeight();
127            double x = location.getX() * (component.getWidth()-tw);
128            double y = location.getY() * (component.getHeight()-th);
129            y += metrics.getAscent();
130            
131            //double stringWidth = SwingUtilities.computeStringWidth(metrics, text);
132            //x -= stringWidth/2;
133            g.drawString(text, (float)x, (float)y);
134        }
135    }