001    /*
002     * RadialGradient.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, 1:55 AM
026     */
027    
028    package com.kitfox.svg;
029    
030    import com.kitfox.svg.xml.StyleAttribute;
031    import java.awt.geom.*;
032    import java.awt.*;
033    
034    import com.kitfox.svg.xml.*;
035    import org.xml.sax.*;
036    
037    //import org.apache.batik.ext.awt.*;
038    import com.kitfox.svg.batik.*;
039    
040    
041    /**
042     * @author Mark McKay
043     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
044     */
045    public class RadialGradient extends Gradient {
046    
047        float cx = 0.5f;
048        float cy = 0.5f;
049        float fx = 0.5f;
050        float fy = 0.5f;
051        float r = 0.5f;
052    
053        /** Creates a new instance of RadialGradient */
054        public RadialGradient() {
055        }
056    /*
057        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
058        {
059                    //Load style string
060            super.loaderStartElement(helper, attrs, parent);
061    
062            String cx = attrs.getValue("cx");
063            String cy = attrs.getValue("cy");
064            String fx = attrs.getValue("fx");
065            String fy = attrs.getValue("fy");
066            String r = attrs.getValue("r");
067    
068            if (cx != null) this.cx = (float)XMLParseUtil.parseRatio(cx);
069            if (cy != null) this.cy = (float)XMLParseUtil.parseRatio(cy);
070            if (fx != null) this.fx = (float)XMLParseUtil.parseRatio(fx);
071            if (fy != null) this.fy = (float)XMLParseUtil.parseRatio(fy);
072            if (r != null) this.r = (float)XMLParseUtil.parseRatio(r);
073        }
074        */
075    
076        /*
077        public void loaderEndElement(SVGLoaderHelper helper)
078        {
079            super.loaderEndElement(helper);
080            
081            build();
082        }
083         */
084        
085        protected void build() throws SVGException
086        {
087            super.build();
088            
089            StyleAttribute sty = new StyleAttribute();
090            
091            if (getPres(sty.setName("cx"))) cx = sty.getFloatValueWithUnits();
092            
093            if (getPres(sty.setName("cy"))) cy = sty.getFloatValueWithUnits();
094            
095            if (getPres(sty.setName("fx"))) fx = sty.getFloatValueWithUnits();
096            
097            if (getPres(sty.setName("fy"))) fy = sty.getFloatValueWithUnits();
098            
099            if (getPres(sty.setName("r"))) r = sty.getFloatValueWithUnits();
100        }
101        
102        public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
103        {
104            com.kitfox.svg.batik.MultipleGradientPaint.CycleMethodEnum method;
105            switch (spreadMethod)
106            {
107                default:
108                case SM_PAD:
109                    method = com.kitfox.svg.batik.MultipleGradientPaint.NO_CYCLE;
110                    break;
111                case SM_REPEAT:
112                    method = com.kitfox.svg.batik.MultipleGradientPaint.REPEAT;
113                    break;
114                case SM_REFLECT:
115                    method = com.kitfox.svg.batik.MultipleGradientPaint.REFLECT;
116                    break;
117            }
118    
119            com.kitfox.svg.batik.RadialGradientPaint paint;
120    
121            if (gradientUnits == GU_USER_SPACE_ON_USE)
122            {
123                paint = new com.kitfox.svg.batik.RadialGradientPaint(
124                    new Point2D.Float(cx, cy),
125                    r,
126                    new Point2D.Float(fx, fy),
127                    getStopFractions(),
128                    getStopColors(),
129                    method,
130                    com.kitfox.svg.batik.MultipleGradientPaint.SRGB,
131                    gradientTransform);
132            }
133            else
134            {
135                AffineTransform viewXform = new AffineTransform();
136                viewXform.translate(bounds.getX(), bounds.getY());
137                viewXform.scale(bounds.getWidth(), bounds.getHeight());
138    
139                viewXform.concatenate(gradientTransform);
140    
141                paint = new com.kitfox.svg.batik.RadialGradientPaint(
142                    new Point2D.Float(cx, cy),
143                    r,
144                    new Point2D.Float(fx, fy),
145                    getStopFractions(),
146                    getStopColors(),
147                    method,
148                    com.kitfox.svg.batik.MultipleGradientPaint.SRGB,
149                    viewXform);
150            }
151    
152            return paint;
153        }
154    
155        /**
156         * Updates all attributes in this diagram associated with a time event.
157         * Ie, all attributes with track information.
158         * @return - true if this node has changed state as a result of the time
159         * update
160         */
161        public boolean updateTime(double curTime) throws SVGException
162        {
163    //        if (trackManager.getNumTracks() == 0) return false;
164            boolean changeState = super.updateTime(curTime);
165    
166            //Get current values for parameters
167            StyleAttribute sty = new StyleAttribute();
168            boolean shapeChange = false;
169            
170            if (getPres(sty.setName("cx")))
171            {
172                float newVal = sty.getFloatValueWithUnits();
173                if (newVal != cx)
174                {
175                    cx = newVal;
176                    shapeChange = true;
177                }
178            }
179            
180            if (getPres(sty.setName("cy")))
181            {
182                float newVal = sty.getFloatValueWithUnits();
183                if (newVal != cy)
184                {
185                    cy = newVal;
186                    shapeChange = true;
187                }
188            }
189            
190            if (getPres(sty.setName("fx")))
191            {
192                float newVal = sty.getFloatValueWithUnits();
193                if (newVal != fx)
194                {
195                    fx = newVal;
196                    shapeChange = true;
197                }
198            }
199            
200            if (getPres(sty.setName("fy")))
201            {
202                float newVal = sty.getFloatValueWithUnits();
203                if (newVal != fy)
204                {
205                    fy = newVal;
206                    shapeChange = true;
207                }
208            }
209            
210            if (getPres(sty.setName("r")))
211            {
212                float newVal = sty.getFloatValueWithUnits();
213                if (newVal != r)
214                {
215                    r = newVal;
216                    shapeChange = true;
217                }
218            }
219            
220            return changeState;
221        }
222    }