简体   繁体   English

蜡染在SVGGraphics2D中加载SVG文件

[英]Batik load SVG file in SVGGraphics2D

I use batik for drawing svg, and save it. 我使用蜡染画svg,并将其保存。 That work. 那工作。

But now i try to load my svg file and add somthing. 但是现在我尝试加载我的svg文件并添加一些东西。

I use this fonction for load my svg file 我使用此功能加载我的svg文件

private static SVGDocument loadSVGDocument(String uri) {
    String parser = XMLResourceDescriptor.getXMLParserClassName();
    SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
    SVGDocument svgDocument = null;
    try {
        //svgDocument = factory.createSVGDocument(IMAGE_SVG);
        svgDocument = factory.createSVGDocument(uri);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return svgDocument;
}

and this for have SVGGraphics2D 这有SVGGraphics2D

SVGDocument svgDocument = loadSVGDocument(TEST_SVG);
SVGGraphics2D g = new SVGGraphics2D(svgDocument);

When i debug my SVGDocument have all children and attribut null And when i generate image, is empty and size is 400x400 当我调试我的SVGDocument时,所有子节点都为null,并且当我生成图像时,它为空且大小为400x400

Where is problem when i load my SVG file? 加载SVG文件时哪里出问题了?

I every body I find a solution for my case 我每个人都找到解决方案

I compare my generation and my file. 我比较我的一代和我的文件。 First think i have svg root and one child g only. 首先认为我只有svg root和一个孩子g。 svg root don t have with and height svg根不具有和高度

I go search width and height 我去搜索宽度和高度

        Element elSVG = svgDocument.getRootElement();
    String width = elSVG.getAttribute("width");
    String height = elSVG.getAttribute("height");

and after root 和根之后

NodeList nodes = elSVG.getElementsByTagName("g");
    Node node = nodes.item(0);
    Element el = null;
    if(Node.ELEMENT_NODE == node.getNodeType()) {
        el = (Element)node;
    }

and for the end add everything to graphics 最后将所有内容添加到图形中

        SVGGraphics2D g = new SVGGraphics2D(svgDocument);
    g.setTopLevelGroup(el);
    g.setSVGCanvasSize(new Dimension(Integer.parseInt(width), Integer.parseInt(height)));

And that works for only one child to svg. 这仅适用于一个要svg的孩子。 For me is OK for now. 对我来说现在还可以。

And i try to add an other think like rectangle and that work to. 而且我尝试添加其他想法,例如矩形,并且可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM