SVG Salamander example?

后端 未结 2 1498
旧时难觅i
旧时难觅i 2020-12-31 23:03

I\'m toying with Java and SVG Salamander but can\'t quite get how to render a simple SVG file into a JPanel.

Could someone give me a brief example? Trie

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 23:44

    First, you need to somehow create the diagram (com.kitfox.svg.SVGDiagram).

    File f = new File(mysvgfile);
    SVGUniverse svgUniverse = new SVGUniverse();
    SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(f.toURL()));
    

    Now, when you want to render your file - typically from the panel's paintComponent() method - you only need to do (with g being the Graphics2D instance):

    diagram.render(g);
    

    And (as usual?), if you want to draw it in some modified way:

    AffineTransform oldTransform = g.getTransform();
    g.scale(...);
    g.translate(...);
    ...
    diagram.render(g);
    g.setTransform(oldTransform);
    

提交回复
热议问题