Class SVGDocument

  • All Implemented Interfaces:
    java.lang.Cloneable, org.w3c.dom.Document, org.w3c.dom.Node

    public class SVGDocument
    extends com.jsevy.jxml.XMLDocument
    Class representing a Scalable Vector Graphics document, which owns an svgGraphics on which drawing commands can be made. The document's toSVGString method can then be called to generate the SVG text corresponding to the drawing done on the Graphics object. The typical workflow is as follows:
     
     // Create an svg document and get its associated SVGGraphics instance
     SVGDocument svgDocument = new SVGDocument(); 
     SVGGraphics svgGraphics = svgDocument.getGraphics(); 
     
     // Do drawing commands as on any other Graphics. If you have a paint(Graphics) method, 
     // you can just use it with the SVGGraphics instance since it's a subclass of Graphics. 
     graphics.setColor(Color.RED);  
     graphics.setStroke(new BasicStroke(3));
     graphics.drawLine(0, 0, 1000, 500); 
     graphics.drawRect(1000, 500, 150, 150); 
     graphics.drawRoundRect(20, 200, 130, 100, 20, 10); 
     
     // Get the flat SVG output as a string - it's just XML text - and  save  in a file for use 
     // with LibreOffice Draw or any other draw package that accepts flat SVG (.svg) files
     String stringOutput = svgDocument.toSVGString(); 
     String filePath = "path/to/file.svg"; 
     FileWriter fileWriter = new FileWriter(filePath); 
     fileWriter.write(svgText); 
     fileWriter.flush(); 
     fileWriter.close();
     
     
    Author:
    jsevy
    • Field Summary

      • Fields inherited from interface org.w3c.dom.Node

        ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE
    • Constructor Summary

      Constructors 
      Constructor Description
      SVGDocument()
      Create a new SVGDocument
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addEntity​(com.jsevy.jsvg.SVGEntity entity)
      Utility method used by the associated SVGGraphics object.
      SVGGraphics getGraphics()
      Get the SVGGraphics associated with this document for use with standard Graphics drawing operations to generate an svg text representation.
      void setDocumentSize​(double documentWidth, double documentHeight)
      Set the document width and height, which equates to the overall viewBox size for the SVG drawing and which will be added as an attribute to the top-level svg element.
      void setScaleFactor​(double javaUnitsPerUnit)
      Set the scale factor for dimensions in the Java drawing - how many Java units/pixels correspond to one unit in the SVG output.
      void setUnits​(int unitsCode)
      Set the units for interpreting the dimension values in the SVG file.
      java.lang.String toSVGString()
      Return the SVG text associated with this SVG document.
      • Methods inherited from class com.jsevy.jxml.XMLDocument

        adoptNode, cloneNode, createAttribute, createAttributeNS, createCDATASection, createComment, createDocumentFragment, createElement, createElementNS, createEntityReference, createProcessingInstruction, createTextNode, getDoctype, getDocumentElement, getDocumentURI, getDomConfig, getElementById, getElementsByTagName, getElementsByTagNameNS, getImplementation, getInputEncoding, getNodeType, getStrictErrorChecking, getTextContent, getXmlEncoding, getXmlStandalone, getXmlVersion, importNode, normalizeDocument, renameNode, setDoctype, setDocumentElement, setDocumentURI, setInputEncoding, setStrictErrorChecking, setXmlEncoding, setXmlStandalone, setXmlVersion
      • Methods inherited from class com.jsevy.jxml.XMLNode

        appendChild, clone, compareDocumentPosition, equals, getAttributes, getBaseURI, getChildNodes, getDescendants, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getUserData, hasAttributes, hasChildNodes, insertAfter, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, prependChild, removeChild, replaceChild, setNamespaceURI, setNodeName, setNodeValue, setOwnerDocument, setParentNode, setPrefix, setTextContent, setUserData
      • Methods inherited from class java.lang.Object

        getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.w3c.dom.Node

        appendChild, compareDocumentPosition, getAttributes, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getUserData, hasAttributes, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setNodeValue, setPrefix, setTextContent, setUserData
    • Constructor Detail

      • SVGDocument

        public SVGDocument()
        Create a new SVGDocument
    • Method Detail

      • setUnits

        public void setUnits​(int unitsCode)
        Set the units for interpreting the dimension values in the SVG file.
        Parameters:
        unitsCode - SVG code indicating units: 0 = Unitless 1 = Inches 2 = Feet 3 = Miles 4 = Millimeter 5 = Centimeters 6 = Meters 7 = Kilometers 8 = Microinches 9 = Mils 10 = Yards
      • setDocumentSize

        public void setDocumentSize​(double documentWidth,
                                    double documentHeight)
        Set the document width and height, which equates to the overall viewBox size for the SVG drawing and which will be added as an attribute to the top-level svg element. Note that the units used are those set with a call to setUnits.
        Parameters:
        documentWidth - Document/viewBox width, in units set by call to setUnits()
        documentHeight - Document/viewBox height, in units set by call to setUnits()
      • setScaleFactor

        public void setScaleFactor​(double javaUnitsPerUnit)
        Set the scale factor for dimensions in the Java drawing - how many Java units/pixels correspond to one unit in the SVG output. Note that the units can be set with a call to setUnits
        Parameters:
        javaUnitsPerUnit - How many Java units/pixels correspond to one SVG unit
      • getGraphics

        public SVGGraphics getGraphics()
        Get the SVGGraphics associated with this document for use with standard Graphics drawing operations to generate an svg text representation.
        Returns:
        The SVGGraphics associated with this document, on which standard Java Graphics drawing calls can be made
      • toSVGString

        public java.lang.String toSVGString()
        Return the SVG text associated with this SVG document. This includes the header and entities populated with content generated by graphics calls on the associated SVGGraphics.
        Returns:
        The SVG text associated with this document.
        Throws:
        javax.xml.transform.TransformerException
      • addEntity

        public void addEntity​(com.jsevy.jsvg.SVGEntity entity)
        Utility method used by the associated SVGGraphics object.
        Parameters:
        entity - an svgEntity instance