在Java环境中使用GraphViz绘图

Java环境下使用GraphViz

本弱鸡的boss最近布置了一个任务,需要自动生成程序流图,需要通过GCC获取流图数据,再通过GraphViz可视化数据实现。记录一下本菜鸡的学习GraphVia的心得。

预备工具

  1. GraphVia http://www.graphviz.org/ 。
  2. Eclipse

1.为GraphVia创建配置文件config.properties。

可以在任意目录下创建config.properties,在后面会指定config.properties的路径。

##############################################################
#                    Linux Configurations                    #
##############################################################
# The dir. where temporary files will be created.
tempDirForLinux = /tmp
# Where is your dot program located? It will be called externally.
dotForLinux = /usr/bin/dot

##############################################################
#                   Windows Configurations                   #
##############################################################
# The dir. where temporary files will be created.
tempDirForWindows = c:/temp
# Where is your dot program located? It will be called externally.
dotForWindows = "c:/Program Files (x86)/Graphviz 2.28/bin/dot.exe"

##############################################################
#                    Mac Configurations                      #
##############################################################
# The dir. where temporary files will be created.
tempDirForMacOSX = /tmp
# Where is your dot program located? It will be called externally.
dotForMacOSX = /usr/local/bin/dot

将其中tempDirForWindows和dotForWindows中的Windows改为相应的Windows版本(Windows10或Windows 7),dotForWindows10后的路径改为dot.exe的安装路径。例如我本地文件:在这里插入图片描述

2.创建Graphviz.java并写入源代码

在这里插入图片描述
将下面代码粘入:

package Graphviz;

//GraphViz.java - a simple API to call dot from Java programs

/*$Id$*/
/*
******************************************************************************
*                                                                            *
*                    (c) Copyright Laszlo Szathmary                          *
*                                                                            *
* This program is free software; you can redistribute it and/or modify it    *
* under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 2.1 of the License, or        *
* (at your option) any later version.                                        *
*                                                                            *
* This program is distributed in the hope that it will be useful, but        *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public    *
* License for more details.                                                  *
*                                                                            *
* You should have received a copy of the GNU Lesser General Public License   *
* along with this program; if not, write to the Free Software Foundation,    *
* Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                              *
*                                                                            *
******************************************************************************
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * <dl>
 * <dt>Purpose: GraphViz Java API
 * <dd>
 *
 * <dt>Description:
 * <dd> With this Java class you can simply call dot
 *      from your Java programs.
 * <dt>Example usage:
 * <dd>
 * <pre>
 *    GraphViz gv = new GraphViz();
 *    gv.addln(gv.start_graph());
 *    gv.addln("A -> B;");
 *    gv.addln("A -> C;");
 *    gv.addln(gv.end_graph());
 *    System.out.println(gv.getDotSource());
 *
 *    String type = "gif";
 *    File out = new File("out." + type);   // out.gif in this example
 *    gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
 * </pre>
 * </dd>
 *
 * </dl>
 *
 * @version v0.5.1, 2013/03/18 (March) -- Patch of Juan Hoyos (Mac support)
 * @version v0.5, 2012/04/24 (April) -- Patch of Abdur Rahman (OS detection + start subgraph + 
 * read config file)
 * @version v0.4, 2011/02/05 (February) -- Patch of Keheliya Gallaba is added. Now you
 * can specify the type of the output file: gif, dot, fig, pdf, ps, svg, png, etc.
 * @version v0.3, 2010/11/29 (November) -- Windows support + ability to read the graph from a text file
 * @version v0.2, 2010/07/22 (July) -- bug fix
 * @version v0.1, 2003/12/04 (December) -- first release
 * @author  Laszlo Szathmary (<a href="jabba.laci@gmail.com">jabba.laci@gmail.com</a>)
 */
public class GraphViz
{
   
    /**
     * Detects the client's operating system.
     */
    private final static String osName = System.getProperty("os.name").replaceAll("\\s","");

    /**
     * Load the config.properties file.
     * 加载配置路径
     */
    private final static String cfgProp = "E:\\Java\\WorkSpace\\Demo1\\src\\Graphviz\\config.properties";
    private final static Properties configFile = new Properties() {
   
        private final static long serialVersionUID = 1L; {
   
            try {
   
                load(new FileInputStream(cfgProp));
            } catch (Exception e) {
   }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值