java batik_Java Batik框架画SVG图 JSVGCanvas

这篇博客介绍如何利用Apache Batik库中的JSVGCanvas组件在Java应用程序中加载和显示SVG图形。通过创建JFrame,添加按钮和JSVGCanvas,用户可以选择SVG文件并进行渲染。同时,监听SVG文档加载、GVT树构建和渲染过程,更新状态信息。
摘要由CSDN通过智能技术生成

软件下载地址:

创建一个JSVGCanvas代码案例:import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

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.SVGDocumentLoaderAdapter;

import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;

import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;

import org.apache.batik.swing.svg.GVTTreeBuilderEvent;

public class SVGApplication {

public static void main(String[] args) {

// Create a new JFrame.

JFrame f = new JFrame("Batik");

SVGApplication app = new SVGApplication(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(400, 400);

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...");

// The status label.

protected JLabel label = new JLabel();

// The SVG canvas.

protected JSVGCanvas svgCanvas = new JSVGCanvas();

public SVGApplication(JFrame f) {

frame = f;

}

public JComponent createComponents() {

// Create a panel and add the button, status label and the SVG canvas.

final JPanel panel = new JPanel(new BorderLayout());

JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));

p.add(button);

p.add(label);

panel.add("North", p);

panel.add("Center", svgCanvas);

// Set the button action.

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

JFileChooser fc = new JFileChooser(".");

int choice = fc.showOpenDialog(panel);

if (choice == JFileChooser.APPROVE_OPTION) {

File f = fc.getSelectedFile();

try {

svgCanvas.setURI(f.toURL().toString());

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

});

// Set the JSVGCanvas listeners.

svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {

public void documentLoadingStarted(SVGDocumentLoaderEvent e) {

label.setText("Document Loading...");

}

public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {

label.setText("Document Loaded.");

}

});

svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {

public void gvtBuildStarted(GVTTreeBuilderEvent e) {

label.setText("Build Started...");

}

public void gvtBuildCompleted(GVTTreeBuilderEvent e) {

label.setText("Build Done.");

frame.pack();

}

});

svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {

public void gvtRenderingPrepare(GVTTreeRendererEvent e) {

label.setText("Rendering Started...");

}

public void gvtRenderingCompleted(GVTTreeRendererEvent e) {

label.setText("");

}

});

return panel;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值