SVG Salamander example?

2020-06-18 08:08发布

问题:

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? Tried to follow the loose tutorial in the official site, but could not find a simple code to get a better understanding.

回答1:

Here is some example code

http://svgsalamander.java.net/docs/exampleCode/SVGIODemo.html



回答2:

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);