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;
 
 
Utility class that converts a Rectangle2D or RoundRectangle2D object into an SVG element.

Author(s):
Vincent Hardy
Version:
$Id: SVGRectangle.java 475477 2006-11-15 22:44:28Z cam $
 
 public class SVGRectangle extends SVGGraphicObjectConverter {
    
Line converter used for degenerate cases
 
     private SVGLine svgLine;

    

Parameters:
generatorContext used to build Elements
 
     public SVGRectangle(SVGGeneratorContext generatorContext) {
         super(generatorContext);
          = new SVGLine(generatorContext);
     }

    

Parameters:
rect rectangle object to convert to SVG
 
     public Element toSVG(Rectangle2D rect) {
         return toSVG((RectangularShape)rect);
     }


    
In the Java 2D API, arc width/height are used as absolute values.

Parameters:
rect rectangle object to convert to SVG
 
     public Element toSVG(RoundRectangle2D rect) {
         Element svgRect = toSVG((RectangularShape)rect);
         if(svgRect != null && svgRect.getTagName() == ){
             svgRect.setAttributeNS(null,
                                    doubleString(Math.abs(rect.getArcWidth()/2)));
             svgRect.setAttributeNS(null,
                                    doubleString(Math.abs(rect.getArcHeight()/2)));
         }
 
         return svgRect;
     }


    

Parameters:
rect rectangle object to convert to SVG
 
     private Element toSVG(RectangularShape rect) {
         if(rect.getWidth() > 0 && rect.getHeight() > 0){
             Element svgRect =
                                                             );
             svgRect.setAttributeNS(nulldoubleString(rect.getX()));
             svgRect.setAttributeNS(nulldoubleString(rect.getY()));
             svgRect.setAttributeNS(null,
                                    doubleString(rect.getWidth()));
             svgRect.setAttributeNS(null,
                                    doubleString(rect.getHeight()));
             
             return svgRect;
         }
         else{
             // Handle degenerate cases
             if(rect.getWidth() == 0 && rect.getHeight() > 0){
                 // Degenerate to a line
                 Line2D line = new Line2D.Double(rect.getX(), rect.getY(), rect.getX(), 
                                                 rect.getY() + rect.getHeight());
                 return .toSVG(line);
            }
            else if(rect.getWidth() > 0 && rect.getHeight() == 0){
                // Degenerate to a line
                Line2D line = new Line2D.Double(rect.getX(), rect.getY(),
                                                rect.getX() + rect.getWidth(),
                                                rect.getY());
                return .toSVG(line);
            }
            return null;
        }
    }
New to GrepCode? Check out our FAQ X