设置使用SVGGraphics2D创建的元素的属性

时间:2019-05-23 00:01:02

标签: svg batik

如何在用SVGGraphics2D创建的SVG元素上设置属性(例如“ id”)?例如:我正在使用以下代码创建多边形:

我已经尝试了以下方法,但是不起作用:

    public static final String SVG_NS = "http://www.w3.org/2000/svg";

                // Test: add id attribute to all SVG elements

        Element root = document.getDocumentElement();
        Node rootNode = (Node)g2d.getRoot(root);

        NodeList nl = rootNode.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n instanceof GenericElementNS) {
                Element el = (Element)n;

                el.setAttributeNS(SVG_NS, "id", "P001");
            }
        }

没有错误,但不会在输出中生成任何属性!

// Code to generate a polygon using SVGGraphics2D
        Polygon p = new Polygon();

        p.addPoint(551, 194);
        p.addPoint(569, 194);
        p.addPoint(569, 250);
        p.addPoint(551, 250);


        g2d.drawPolygon(p);

           // Here I want to set the attribute 'id="P001"' to the above element```

0 个答案:

没有答案
相关问题