我需要在Java中使用Apache Batik和SVG Salamander库的支持。 我编写了以下示例,该示例在JFrame中展示了一系列几何形状。 随后,使用Apache Batik库,将绘制的内容转换为SVG文件。
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(fg);
// draw Text
g2.drawString("Shapes", 250, 400);
// draw Line2D.Double
g2.setPaint(orange);
g2.draw(new Line2D.Double(50, 50, 200, 200));
// draw Point2D.Double
g2.setPaint(red);
g2.fill(new Ellipse2D.Double(400, 400, 5, 5));
// draw Rectangle2D.Double
g2.setPaint(magenta);
g2.draw(new Rectangle2D.Double(600, 600, 300, 150));
g2.setPaint(magenta);
g2.draw(new Rectangle2D.Double(300, 300, 300, 150));
// draw Ellipse2D.Double
g2.setPaint(blue);
g2.draw(new Ellipse2D.Double(400, 400, 200, 200));
// draw GeneralPath (polygon)
g2.setPaint(green);
int x1Points[] = {200, 500, 300};
int y1Points[] = {100, 100, 300};
GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
x1Points.length);
polygon.moveTo(x1Points[0], y1Points[0]);
for ( int index = 1; index < x1Points.length; index++ ) {
polygon.lineTo(x1Points[index], y1Points[index]);
};
polygon.closePath();
g2.draw(polygon);
}
public void loadSVG(String path){
System.out.println("In loadSVG");
File file = new File(path);
SVGUniverse universe = new SVGUniverse();
URI svgURI = null;
try {
//svgUri = universe.loadSVG(fis, file.toURI().toString());
svgURI = universe.loadSVG(file.toURL());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SVGPanel svgPanel = new SVGPanel();
svgPanel.setSvgURI(svgURI);
SVGDiagram diagram = universe.getDiagram(svgURI);
SVGRoot root = diagram.getRoot();
List<Object> l = root.getChildren(null);
//Create application window/container
JFrame frame = new JFrame("Shapes Example From SVG");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(1000, 1000));
frame.setLocation(1000, 0);
frame.add(svgPanel);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String s[]) {
JFrame f = new JFrame("Shapes Example");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
// Get a DOMImplementation.
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
ctx.setComment("Generated from ShapesDemo2D");
ctx.setEmbeddedFontsOn(true);
// Create an instance of the SVG Generator.
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
JPanel pane = new ShapesExample();
f.getContentPane().add("Center", pane);
f.pack();
f.setSize(new Dimension(1000,1000));
f.setResizable(false);
f.setVisible(true);
pane.paint(svgGenerator);
// Finally, stream out SVG to the standard output using
// UTF-8 encoding.
boolean useCSS = true; // we want to use CSS style attributes
String svgFile = "example.svg";
try {
OutputStream outputStream = new FileOutputStream(svgFile);
Writer outputStreamWriter = new OutputStreamWriter(outputStream);
svgGenerator.stream(outputStreamWriter, useCSS);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
((ShapesExample)pane).loadSVG(svgFile);
}
在loadSVG()方法中,我需要在新的JFrame中呈现从SVG文件读取的表单。 如何使用SVG Salamander库从SVG文件中提取单个几何形状,并在Graphics2D“上下文”中报告它们? 谢谢大家的支持!