服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

jgraph分析

jgraph分析
                                                                        johnny.deng

jgraph是一个开源的,兼容swing的基于mvc体系结构图形组件,具有以下特点:

1)   完全swing兼容;

2)   简单、高效的设计;

3)   时间效率高;

4)   100 %java

生成的图例
生成的图例

 

二、jgraph设计

1)  mvc

swingjavasun)提供的ui标准实现之一,swing基于awtabstract windowing toolkit)。jgraph完全兼容swing,它的实现仍然基于mvc体系结构。

 

 

jgraph mvcjgraph mvc

 

view

jgraph不包含实际的数据,它提供了数据的视;jgraph对象画图的机制是:

将图元定义为一个一个的cell,每个cell可以是一个顶点(vertex)、边(edge)或者节点(port)中的一种。顶点可以有邻接的顶点,他们通过边相联系,边联接的两个端点称为目标和源,每个目标或者源是一个节点。节点是顶点的孩子。每个cell都可以有自己的孩子。

每个cell的外观由相应的属性定义,属性序列是指一系列的键-值对,他们以map形式组织,例如:

map cellattrib = new hashtable();

// set bounds

rectangle2d hellobounds = new rectangle2d.double(20, 20, 40, 20);

graphconstants.setbounds(cellattrib, hellobounds);

// set black border

graphconstants.setbordercolor(cellattrib, color.black);

 

一个cell有类似这样一个cellattribmap,来定义其外观。

外观可以指定诸如一条边的箭头样式等属性。

 

model

数据对象可以看成是jgraph中两个独立结构的链接点:grahp结构和group结构。graph结构基于图论中的顶点、边定义。group结构是cellcomposition结构。graph结构中getsource()和gettarget()方法,获得源和目标节点。而在group中通过getchild(),getparent()来获得cell的组成结构。

 

2)  低层基于图论逻辑

即:一个图g包含一个非空的元素集v(g)和一个eg),其中,eg)是vg)中两个无序元素组成的二元组。vg)称为图g顶点的集合,如果任意集合vg)中的顶点x/y,(xy)在eg)中,边(xy)可能以连接顶点xy的边(弧)所代表,xy就被称为邻接的,否则xy不邻接。

 

三、jgraph的应用

以下是一个基于jgraphhelloworld的分析:

import省略

public class helloworld {

 

  public static void main(string[] args) {

 

    // construct model and graph

    //

    graphmodel model = new defaultgraphmodel();

    jgraph graph = new jgraph(model);

    graph.setselectnewcells(true);

 

    // create nested map (from cells to attributes)

// map中记录所有属性,其中的键-值对是cell-cellattribute

// 每个cellattribute又是一个map,其键-值对是具体一个cell的属性-

    map attributes = new hashtable();

 

//  以下建立两个顶点(cell)helloworld,并分别设置他们的属性map

// create hello vertex

    //

    defaultgraphcell hello = new defaultgraphcell("hello");

   

    // create hello vertex attributes

    //

    map helloattrib = new hashtable();

    attributes.put(hello, helloattrib);

    // set bounds

    rectangle2d hellobounds = new rectangle2d.double(20, 20, 40, 20);

    graphconstants.setbounds(helloattrib, hellobounds);

    // set black border

    graphconstants.setbordercolor(helloattrib, color.black);

 

    // add a port

    //  每个顶点为了与其他顶点相邻接,必须添加节点(cell

    defaultport hp = new defaultport();

    hello.add(hp);

 

    // create world vertex

    //

    defaultgraphcell world = new defaultgraphcell("world");

 

    // create world vertex attributes

    //

    map worldattrib = new hashtable();

    attributes.put(world, worldattrib);

    // set bounds

    rectangle2d worldbounds = new rectangle2d.double(140, 140, 40, 20);

    graphconstants.setbounds(worldattrib , worldbounds);

    // set fill color

    graphconstants.setbackground(worldattrib, color.orange);

    graphconstants.setopaque(worldattrib, true);

    // set raised border

    graphconstants.setborder(worldattrib,

           borderfactory.createraisedbevelborder());

 

    // add a port

    //

    defaultport wp = new defaultport();

    world.add(wp);