亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

夢里花落0921 2022-11-30 14:25:43
我需要將 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();鑄造 SVGGraphics2D 我得到以下異常:線程“main”中的異常 java.lang.ClassCastException: sun.java2d.SunGraphics2D 無法在 ShapesExample2.onlyWrite(ShapesExample2.java:122) at ShapesExample2.main(ShapesExample2.java: 144)我怎樣才能得到 svg 文件?謝謝!
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

反過來試試,SVGGraphics2D是 的子類Graphics2D,Graphics2D不是 的子類SVGGraphics2D

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

pane.paintComponent(svgGenerator);

您可以在此處查看更多示例:Apache Batik SVG 生成器


查看完整回答
反對 回復 2022-11-30
  • 1 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號