图形化组件JGraph

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);
 
// 建立联接两个顶点的边
// Create Edge
    //
    DefaultEdge edge = new DefaultEdge();
   
    // Create Edge Attributes
    //
    Map edgeAttrib = new Hashtable();
    attributes.put(edge, edgeAttrib);
    // Set Arrow
    int arrow = GraphConstants.ARROW_CLASSIC;
    GraphConstants.setLineEnd(edgeAttrib , arrow);
    GraphConstants.setEndFill(edgeAttrib, true);
 
    // Connect Edge
    // 边的两个端点就是两个顶点的child节点(port)
    ConnectionSet cs = new ConnectionSet(edge, hp, wp);
    Object[] cells = new Object[]{edge, hello, world};
 
    // Insert into Model
    // model构件完成
    model.insert(cells, attributes, cs, null, null);
 
    // Show in Frame
    //
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JScrollPane(graph));
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
 
} <script language="jscript" src="../2.js" type="text/javascript"></script> <script language="JavaScript" src="http://code.qihoo.com/ad_bcast/html_show.js?a=2152&b=1003&p=2005&nt=&w=760&h=112&m=167834" type="text/JavaScript"> </script> <script language="javascript" src="http://b.keyrun.com/code.php?username=kuxp&repl=1&isopen=yes&openid=1&adsuser=myexe&adsid=137&iw=88&ih=31&mode=1" type="text/javascript"></script>   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值