java操作svg文件

该程序演示了如何使用Java和Batik库解析SVG文件,并实现SVG元素的加载、显示、放大、缩小、旋转和平移操作。通过监听事件处理用户交互,允许在SVG画布上动态添加矩形和圆形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 *  功能:解析svg文件,并对svg文件进行操作
 */
package svgapplication;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGElement;


public class SVGDemo {


    public static void main(String[] args) {
        // Create a new JFrame.
        JFrame f = new JFrame("Batik");
        SVGDemo app = new SVGDemo(f);


        // Add components to the frame.
        f.getContentPane().add(app.createComponents());


        // Display the frame.
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.setSize(800, 800);
        f.setVisible(true);
    }
    // The frame.
    protected JFrame frame;
    // The "Load" button, which displays up a file chooser upon clicking.
    protected JButton button = new JButton("Load...");
    protected JButton Bigbutton = new JButton("放大");
    protected JButton Smallbutton = new JButton("缩小");
    protected JButton Rotatebutton = new JButton("旋转");
    protected JButton Rightbutton = new JButton("右");
    protected JButton Downbutton = new JButton("下");
    protected JButton Leftbutton = new JButton("左");
    protected JButton Upbutton = new JButton("上");
    protected JButton addbutton1 = new JButton("add矩形");
    protected JButton addbutton2 = new JButton("add圆形");
    protected JButton deletebutton = new JButton("delete");
    protected JButton savebutton = new JButton("sss");
    // The status label.
    protected JLabel label = new JLabel();
    // The SVG canvas.
    protected JSVGCanvas svgCanvas = new JSVGCanvas();
    SVGGraphics2D svgGenerator;//一个实例生成器
    SVGDocument svgDocument;//SVGDocument实例
    SVGElement svgRoot;//SVGElement实例
    Element ele;//通过ID获得的元素
    float n = 1;//放大缩小按钮点击次数
    float m = 1;//旋转按钮点击次数
    int a = 0;//x轴按钮点击次数
    int b = 0;//y轴按钮点击次数
    int
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值