001    /*
002     * Rect.java
003     *
004     *
005     *  The Salamander Project - 2D and 3D graphics libraries in Java
006     *  Copyright (C) 2004 Mark McKay
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or (at your option) any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     *
022     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
023     *  projects can be found at http://www.kitfox.com
024     *
025     * Created on January 26, 2004, 5:25 PM
026     */
027    
028    package com.kitfox.svg;
029    
030    import com.kitfox.svg.xml.StyleAttribute;
031    import java.awt.Graphics2D;
032    import java.awt.Shape;
033    import java.awt.geom.Line2D;
034    import java.awt.geom.Rectangle2D;
035    
036    /**
037     * @author Mark McKay
038     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
039     */
040    public class Line extends ShapeElement {
041    
042        float x1 = 0f;
043        float y1 = 0f;
044        float x2 = 0f;
045        float y2 = 0f;
046    
047        Line2D.Float line;
048    //    RectangularShape rect;
049    
050        /** Creates a new instance of Rect */
051        public Line() {
052        }
053    
054        /*
055        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
056        {
057                    //Load style string
058            super.loaderStartElement(helper, attrs, parent);
059    
060            String x1 = attrs.getValue("x1");
061            String y1 = attrs.getValue("y1");
062            String x2 = attrs.getValue("x2");
063            String y2 = attrs.getValue("y2");
064    
065            this.x1 = XMLParseUtil.parseFloat(x1);
066            this.y1 = XMLParseUtil.parseFloat(y1);
067            this.x2 = XMLParseUtil.parseFloat(x2);
068            this.y2 = XMLParseUtil.parseFloat(y2);
069    
070            build();
071        }
072    */
073        protected void build() throws SVGException
074        {
075            super.build();
076            
077            StyleAttribute sty = new StyleAttribute();
078            
079            if (getPres(sty.setName("x1"))) x1 = sty.getFloatValueWithUnits();
080    
081            if (getPres(sty.setName("y1"))) y1 = sty.getFloatValueWithUnits();
082    
083            if (getPres(sty.setName("x2"))) x2 = sty.getFloatValueWithUnits();
084    
085            if (getPres(sty.setName("y2"))) y2 = sty.getFloatValueWithUnits();
086    
087            line = new Line2D.Float(x1, y1, x2, y2);
088        }
089        
090    
091        public void render(Graphics2D g) throws SVGException
092        {
093            beginLayer(g);
094            renderShape(g, line);
095            finishLayer(g);
096        }
097    
098        public Shape getShape()
099        {
100            return shapeToParent(line);
101        }
102    
103        public Rectangle2D getBoundingBox() throws SVGException
104        {
105            return boundsToParent(includeStrokeInBounds(line.getBounds2D()));
106        }
107    
108        /**
109         * Updates all attributes in this diagram associated with a time event.
110         * Ie, all attributes with track information.
111         * @return - true if this node has changed state as a result of the time
112         * update
113         */
114        public boolean updateTime(double curTime) throws SVGException
115        {
116    //        if (trackManager.getNumTracks() == 0) return false;
117            boolean changeState = super.updateTime(curTime);
118    
119            //Get current values for parameters
120            StyleAttribute sty = new StyleAttribute();
121            boolean shapeChange = false;
122            
123            if (getPres(sty.setName("x1")))
124            {
125                float newVal = sty.getFloatValueWithUnits();
126                if (newVal != x1)
127                {
128                    x1 = newVal;
129                    shapeChange = true;
130                }
131            }
132    
133            if (getPres(sty.setName("y1")))
134            {
135                float newVal = sty.getFloatValueWithUnits();
136                if (newVal != y1)
137                {
138                    y1 = newVal;
139                    shapeChange = true;
140                }
141            }
142    
143            if (getPres(sty.setName("x2")))
144            {
145                float newVal = sty.getFloatValueWithUnits();
146                if (newVal != x2)
147                {
148                    x2 = newVal;
149                    shapeChange = true;
150                }
151            }
152    
153            if (getPres(sty.setName("y2")))
154            {
155                float newVal = sty.getFloatValueWithUnits();
156                if (newVal != y2)
157                {
158                    y2 = newVal;
159                    shapeChange = true;
160                }
161            }
162    
163            if (shapeChange)
164            {
165                build();
166    //            line = new Line2D.Float(x1, y1, x2, y2);
167    //            return true;
168            }
169            
170            return changeState || shapeChange;
171        }
172    }