JGraph分析

来源:互联网 发布:mac版千牛怎么设字体 编辑:程序博客网 时间:2024/04/27 18:05

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

 

// 建立联接两个顶点的边

// 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);

  }

 

}