001    /*
002     * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: TextObject.java,v 8afef6dd0d58 2007/09/14 08:29:58 maxberger $ */
018    
019    package net.sourceforge.jeuclid.layout;
020    
021    import java.awt.Color;
022    import java.awt.Graphics2D;
023    import java.awt.font.TextLayout;
024    import java.awt.geom.AffineTransform;
025    
026    /**
027     * @version $Revision: 8afef6dd0d58 $
028     */
029    public class TextObject implements GraphicsObject {
030    
031        private final TextLayout layout;
032    
033        private final Color color;
034    
035        private final float xoffset;
036    
037        private final float yoffset;
038    
039        private final AffineTransform trans;
040    
041        /**
042         * Default Constructor.
043         * 
044         * @param textLayout
045         *            Text Layout.
046         * @param xo
047         *            X-Offset
048         * @param textColor
049         *            text color.
050         */
051        public TextObject(final TextLayout textLayout, final float xo,
052                final Color textColor) {
053            assert textLayout != null;
054            this.layout = textLayout;
055            this.color = textColor;
056            this.xoffset = xo;
057            this.yoffset = 0.0f;
058            this.trans = null;
059        }
060    
061        /**
062         * Constructor for more complex texts (operators).
063         * 
064         * @param textLayout
065         *            Text Layout.
066         * @param textColor
067         *            text color.
068         * @param xo
069         *            X-Offset for drawing.
070         * @param yo
071         *            Y-Offset for drawing.
072         * @param transform
073         *            Transformation to apply before drawing.
074         */
075        public TextObject(final TextLayout textLayout, final float xo,
076                final float yo, final AffineTransform transform,
077                final Color textColor) {
078            assert textLayout != null;
079            this.layout = textLayout;
080            this.color = textColor;
081            this.xoffset = xo;
082            this.yoffset = yo;
083            this.trans = transform;
084        }
085    
086        /** {@inheritDoc} */
087        public void paint(final float x, final float y, final Graphics2D g) {
088            g.setColor(this.color);
089            final AffineTransform oldTrans = g.getTransform();
090            g.translate(x + this.xoffset, y + this.yoffset);
091            if (this.trans != null) {
092                g.transform(this.trans);
093            }
094            this.layout.draw(g, 0, 0);
095            g.setTransform(oldTrans);
096        }
097    }