I am trying to load an svg file using batik and display it, but both methods that I have tried have given me different exceptions. I have included both of them in the following code.
public static SVGGraphics2D viewSVG()
{
File file = findFile();
DOMImplementation imp = SVGDOMImplementation.getDOMImplementation();
//SAXSVGDocumentFactory
SAXSVGDocumentFactory fact = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
JSVGCanvas canvas = new JSVGCanvas();
SVGDocument doc = null;
SVGGraphics2D g = null;
try {
doc = fact.createSVGDocument(file.getAbsolutePath());
g = new SVGGraphics2D(doc);
} catch (IOException e1) {
// TODO Auto-generated catch block
doc = (SVGDocument) imp.createDocument(file.getAbsolutePath(), "svg", null);
e1.printStackTrace();
}
doc.getRootElement();
g.setSVGCanvasSize(new Dimension(1000, 1200));
JFrame frame = new JFrame(doc.getLocalName());
Element root = doc.getDocumentElement();
g.getRoot(root);
canvas.paint(g);
try {
Writer out = new FileWriter(System.getProperty("user.dir") + "/tst1.svg");
//g.stream(out, true);
g.stream(file.getAbsolutePath());
canvas.paint(g);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.getContentPane().add(canvas);
System.out.println(doc.getDocumentElement().getClass());
System.out.println(doc.getRootElement());
canvas.setSVGDocument(doc);
frame.pack();
frame.setVisible(true);
return g;
}
/*
* @param find = JFileChooser object to allow user to navigate file explorer
* @param result = integer indicating if the selection was valid
* @param@return file = file selected by user
* uses JFileChooser to allow user to select a file and then return the file if it is valid or null if it is not
*/
public static File findFile()
{
JFileChooser find = new JFileChooser();
find.setCurrentDirectory(new File(System.getProperty("user.dir")));
int result = find.showOpenDialog(find);
if(JFileChooser.APPROVE_OPTION == result)
{
File file = find.getSelectedFile();
return file;
}
else
{
return null;
}
}
java.io.IOException: Unable to make sense of URL for connection
at org.apache.batik.util.ParsedURLData.openStreamInternal(ParsedURLData.java:515)
at org.apache.batik.util.ParsedURLData.openStream(ParsedURLData.java:476)
at org.apache.batik.util.ParsedURL.openStream(ParsedURL.java:440)
at org.apache.batik.anim.dom.SAXSVGDocumentFactory.createDocument(SAXSVGDocumentFactory.java:159)
at org.apache.batik.anim.dom.SAXSVGDocumentFactory.createSVGDocument(SAXSVGDocumentFactory.java:124)
at calendarmaker.Objects.Pane.viewSVG(Pane.java:118)
java.lang.ClassCastException: class org.apache.batik.dom.GenericElementNS cannot be cast to class org.w3c.dom.svg.SVGSVGElement (org.apache.batik.dom.GenericElementNS and org.w3c.dom.svg.SVGSVGElement are in unnamed module of loader 'app')
at org.apache.batik.anim.dom.SVGOMDocument.getRootElement(SVGOMDocument.java:234)
at calendarmaker.Objects.Pane.viewSVG(Pane.java:125)`
Can I get some help please? I don’t know what I could be doing wrong as I can view the svg file just fine by clicking on it but I can’t seem to load and display it with my code.
I tried to follow different tutorials for loading svg files with batik but none of them worked. I was expecting the svg file to load and then paint it onto a canvas on a jframe. There might be some stray code in there from when I was trying things, so I apologize in advance.