簡體   English   中英

如何使用Apache Batik庫在SVGGraphics2D中轉換Graphics2D?

[英]How to transform Graphics2D in SVGGraphics2D using Apache Batik Library?

我需要將JPanel(圖形)中的內容(形狀)轉換為SVGGraphics2D對象,以獲取使用Apache Batik庫生成的svg文件。

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(fg);

    // draw Text
    g2.drawString("Hello world!", 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 onlyWrite(){

    JFrame f = new JFrame("Shapes Example2");
    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 Shapes Example2");
    //ctx.setEmbeddedFontsOn(true);

    // Create an instance of the SVG Generator.
    SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);

    ShapesExample2 pane = new ShapesExample2();
    f.getContentPane().add("Center", pane);
    f.pack();
    f.setSize(new Dimension(1000,1000));
    f.setResizable(false);
    f.setVisible(true);

    svgGenerator = (SVGGraphics2D)pane.getGraphics();

    // Finally, stream out SVG to the standard output using
    // UTF-8 encoding.
    boolean useCSS = false; // we want to use CSS style attributes

    String svgFile = "example2.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();
    }       

}

鑄造SVGGraphics2D我得到以下異常:

線程“主”中的異常java.lang.ClassCastException:無法將ShapesExample2.main(ShapesExample2.java:122)處的ShapesExample2.onlyWrite(ShapesExample2.java:122)處的sun.java2d.SunGraphics2D強制轉換為org.apache.batik.svggen.SVGGraphics2D: 144)

如何獲取svg文件? 謝謝!

SVGGraphics2D一種方式嘗試, SVGGraphics2DGraphics2D的子類, Graphics2D不是SVGGraphics2D的子類。

因此,只需將SVG圖形對象傳遞給組件的paint方法即可。

pane.paintComponent(svgGenerator);

您可以在此處看到更多示例: Apache Batik SVG Generator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM