jgraph分析
johnny.deng
jgraph是一个开源的,兼容swing的基于mvc体系结构图形组件,具有以下特点:
1) 完全swing兼容;
2) 简单、高效的设计;
3) 时间效率高;
4) 100 %纯java;

生成的图例
二、jgraph设计
1) mvc
swing是java(sun)提供的ui标准实现之一,swing基于awt(abstract windowing toolkit)。jgraph完全兼容swing,它的实现仍然基于mvc体系结构。
jgraph 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有类似这样一个cellattrib的map,来定义其外观。
外观可以指定诸如一条边的箭头样式等属性。
model:
数据对象可以看成是jgraph中两个独立结构的链接点:grahp结构和group结构。graph结构基于图论中的顶点、边定义。group结构是cell的composition结构。graph结构中getsource()和gettarget()方法,获得源和目标节点。而在group中通过getchild(),getparent()来获得cell的组成结构。
2) 低层基于图论逻辑
即:一个图g包含一个非空的元素集v(g)和一个e(g),其中,e(g)是v(g)中两个无序元素组成的二元组。v(g)称为图g顶点的集合,如果任意集合v(g)中的顶点x/y,(x,y)在e(g)中,边(x,y)可能以连接顶点x和y的边(弧)所代表,x与y就被称为邻接的,否则x与y不邻接。
三、jgraph的应用
以下是一个基于jgraph的helloworld的分析:
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)hello和world,并分别设置他们的属性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);
-
关于我们
公司介绍 最新动态 联系我们 -
产品与服务
域名注册 jsp空间 php空间 -
常见问题
空间操作手册 网站备案相关 退款相关问题 -
技术支持
技术 QQ :178966803 联系电话:13616026886 联系邮箱:fjjsp@vip.163.com
扫描关注微信公众号
闽公网安备 35060202000074号