Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 
  */
 package org.apache.batik.svggen;
 
 import java.awt.Paint;
 
Utility class that converts a Java GradientPaint into an SVG linear gradient element

Author(s):
Vincent Hardy
Version:
$Id: SVGLinearGradient.java 475477 2006-11-15 22:44:28Z cam $
 
 public class SVGLinearGradient extends AbstractSVGConverter {
    

Parameters:
generatorContext used to build Elements
 
     public SVGLinearGradient(SVGGeneratorContext generatorContext) {
         super(generatorContext);
     }

    
Converts part or all of the input GraphicContext into a set of attribute/value pairs and related definitions

Parameters:
gc GraphicContext to be converted
Returns:
descriptor of the attributes required to represent some or all of the GraphicContext state, along with the related definitions
See also:
SVGDescriptor
 
     public SVGDescriptor toSVG(GraphicContext gc) {
         Paint paint = gc.getPaint();
         return toSVG((GradientPaint)paint);
     }

    

Parameters:
gradient the GradientPaint to be converted
Returns:
a description of the SVG paint and opacity corresponding to the gradient Paint. The definiton of the linearGradient is put in the linearGradientDefsMap
 
     public SVGPaintDescriptor toSVG(GradientPaint gradient) {
         // Reuse definition if gradient has already been converted
         SVGPaintDescriptor gradientDesc =
             (SVGPaintDescriptor).get(gradient);
 
         Document domFactory = .;
 
         if (gradientDesc == null) {
             Element gradientDef =
                 domFactory.createElementNS(,
                                            );
             gradientDef.setAttributeNS(null,
                                        );
 
             //
             // Process gradient vector
             //
             Point2D p1 = gradient.getPoint1();
             Point2D p2 = gradient.getPoint2();
             gradientDef.setAttributeNS(null,
                                        doubleString(p1.getX()));
             gradientDef.setAttributeNS(null,
                                        doubleString(p1.getY()));
             gradientDef.setAttributeNS(null,
                                        doubleString(p2.getX()));
             gradientDef.setAttributeNS(null,
                                        doubleString(p2.getY()));
 
             //
             // Spread method
             //
             String spreadMethod = ;
             if(gradient.isCyclic())
                 spreadMethod = ;
             gradientDef.setAttributeNS
                (nullspreadMethod);
            //
            // First gradient stop
            //
            Element gradientStop =
                domFactory.createElementNS();
            gradientStop.setAttributeNS(null,
                                      );
            SVGPaintDescriptor colorDesc = SVGColor.toSVG(gradient.getColor1(), );
            gradientStop.setAttributeNS(null,
                                      colorDesc.getPaintValue());
            gradientStop.setAttributeNS(null,
                                      colorDesc.getOpacityValue());
            gradientDef.appendChild(gradientStop);
            //
            // Second gradient stop
            //
            gradientStop =
                domFactory.createElementNS();
            gradientStop.setAttributeNS(null,
                                      );
            colorDesc = SVGColor.toSVG(gradient.getColor2(), );
            gradientStop.setAttributeNS(null,
                                        colorDesc.getPaintValue());
            gradientStop.setAttributeNS(null,
                                        colorDesc.getOpacityValue());
            gradientDef.appendChild(gradientStop);
            //
            // Gradient ID
            //
            gradientDef.
                setAttributeNS(null,
                               ..
                               generateID());
            //
            // Build Paint descriptor
            //
            StringBuffer paintAttrBuf = new StringBuffer();
            paintAttrBuf.append();
            paintAttrBuf.append(gradientDef.getAttributeNS(null));
            paintAttrBuf.append();
            gradientDesc = new SVGPaintDescriptor(paintAttrBuf.toString(),
                                                  ,
                                                  gradientDef);
            //
            // Update maps so that gradient can be reused if needed
            //
            .put(gradientgradientDesc);
            .add(gradientDef);
        }
        return gradientDesc;
    }
New to GrepCode? Check out our FAQ X