Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Apache Batik库转换SVGGraphics2D中的Graphics2D?_Java_Svg_Batik - Fatal编程技术网

Java 如何使用Apache Batik库转换SVGGraphics2D中的Graphics2D?

Java 如何使用Apache Batik库转换SVGGraphics2D中的Graphics2D?,java,svg,batik,Java,Svg,Batik,我需要将JPanel(图形)中的内容(形状)转换为SVGGraphics2D对象,以获得使用Apache Batik库生成的svg文件 public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

我需要将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();
    }       

}
公共组件(图形g){
图形2d g2=(图形2d)g;
g2.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.设置涂料(fg);
//绘制文本
g2.拉丝(“你好,世界!”,250,400);
//画线2D。双精度
g2.色漆(橙色);
g2.绘制(新线条2D.双色(50,50,200,200));
//画点2D,双精度
g2.设置油漆(红色);
g2.填充(新椭圆E2D.双(400400,5,5));
//绘制矩形2D。双精度
g2.固色漆(品红);
g2.绘制(新矩形2D.双(600600300150));
g2.固色漆(品红);
g2.绘制(新矩形2D.双(300300300150));
//画一个椭圆2d,双倍
g2.设置油漆(蓝色);
g2.绘制(新椭圆E2D.双(400400200200));
//绘制通用路径(多边形)
g2.设置油漆(绿色);
int x1点[]={200500300};
int Y1点[]={100100300};
GeneralPath多边形=新的GeneralPath(GeneralPath.WIND\u偶数\u奇数,
x1点(长度);
moveTo(x1点[0],y1点[0]);
对于(int index=1;index
铸造SVGGS2D时出现以下异常:

线程“main”java.lang.ClassCastException中的异常: 无法将sun.java2d.SunGraphics2D强制转换为 org.apache.batik.svggen.SVGGraphics2D位于 ShapeSample2.onlyWrite(ShapeSample2.java:122)位于 main(ShapesExample2.java:144)

如何获取svg文件?
谢谢大家!

换一种方式试试,
SVGGraphics2D
Graphics2D
的子类,
Graphics2D
不是
SVGGraphics2D
的子类

因此,只需将SVG图形对象传递给组件的paint方法即可

pane.paintComponent(svgGenerator);

您可以在这里看到更多示例:

谢谢,但所显示的代码是对更一般情况的简化,在这种情况下,我无法在绘制形状之前传递svgGenerator对象。形状已经存在于JPanel中,我应该“提取”它们并将它们放入svg文件中。我甚至可以在另一个JPanel中重新绘制它们。但是我该怎么做呢?非常感谢。调用
paintComponent(svgGenerator)
将强制面板在SVG对象上绘制自身,因此面板应该绘制的内容将显示在SVG文件中。