词条 | jgraph |
释义 | JGraph的基础知识简介,一个简单的开始JGraph是一个开源的,兼容Swing的基于MVC体系结构图形组件,具有以下特点: 1) 基于Swing的扩展;(鉴于现在流行的SWT,这是一个缺点,不过SWT中加入Swing也是很方便的事) 2) 简单、高效的设计; 3) 时间效率高; 4) 100 %纯Java; 5) 强大的布局算法支持(虽然付费,大概500百美元,但其功能异常强大,适合像我这种不懂图论的java程序员) JGraph不包含实际的数据,它提供了数据的视;JGraph对象画图的机制是: 将图元定义为一个一个的cell,每个cell可以是一个顶点(vertex)、边(edge)或者节点(port)中的一种。顶点可以有邻接的顶点,他们通过边相联系,边联接的两个端点称为目标和源,每个目标或者源是一个节点。节点是顶点的孩子。每个cell都可以有自己的孩子。 每个cell的外观由相应的属性定义,属性序列是指一系列的键-值对,他们以Map形式组织,例如: Map map = new Hashtable(); // 设置一个矩形的vertex Rectangle2D rect = new Rectangle2D.Double(20, 20, 40, 20); //GraphConstants为设置整个graph图像属性的类 GraphConstants.setBounds(map, rect); // 设置各种vertex属性,这里设置其颜色为例 GraphConstants.setBorderColor(map, Color.black); 来看看一个简单的程序吧: /* author scorpion 创建日期 2006-10-10*/ import java.awt.Color; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.util.Iterator; import javax.swing.JFrame; import javax.swing.JScrollPane; import org.jgraph.JGraph; import org.jgraph.graph.DefaultCellViewFactory; import org.jgraph.graph.DefaultEdge; import org.jgraph.graph.DefaultGraphCell; import org.jgraph.graph.DefaultGraphModel; import org.jgraph.graph.DefaultPort; import org.jgraph.graph.GraphConstants; import org.jgraph.graph.GraphLayoutCache; import org.jgraph.graph.GraphModel; public class Hello { public static void main(String[] args) { // 定义一个存放cell的集合类 ArrayList<DefaultGraphCell> cells = new ArrayList<DefaultGraphCell>(); // model用于控制整个模型显示属性等,view用于控制你的图形显示属性,这里都用默认即可 GraphModel model = new DefaultGraphModel(); GraphLayoutCache view = new GraphLayoutCache(model, new DefaultCellViewFactory()); // JGraph对象 JGraph graph = new JGraph(model, view); // 建立你的第一个vertex对象 cells.add(new DefaultGraphCell(new String("Hello"))); GraphConstants.setBounds(cells.get(0).getAttributes(), new Rectangle2D.Double(20, 20, 40, 20)); // 设置颜色和透明属性 GraphConstants.setGradientColor(cells.get(0).getAttributes(), Color.orange); GraphConstants.setOpaque(cells.get(0).getAttributes(), true); // 为这个vertex加入一个port DefaultPort port0 = new DefaultPort(); cells.get(0).add(port0); // 同理加入第二个vertex cells.add(new DefaultGraphCell(new String("World"))); GraphConstants.setBounds(cells.get(1).getAttributes(), new Rectangle2D.Double(200, 200, 40, 20)); GraphConstants .setGradientColor(cells.get(1).getAttributes(), Color.red); GraphConstants.setOpaque(cells.get(1).getAttributes(), true); DefaultPort port1 = new DefaultPort(); cells.get(1).add(port1); // 同理加入第三个vertex cells.add(new DefaultGraphCell(new String("Scorpio"))); GraphConstants.setBounds(cells.get(2).getAttributes(), new Rectangle2D.Double(20, 300, 50, 20)); GraphConstants.setGradientColor(cells.get(2).getAttributes(), Color.blue); GraphConstants.setOpaque(cells.get(2).getAttributes(), true); DefaultPort port2 = new DefaultPort(); cells.get(2).add(port2); // 加入一条边,将hello和world的两个port连接起来 DefaultEdge edge = new DefaultEdge(); edge.setSource(cells.get(0).getChildAt(0)); edge.setTarget(cells.get(1).getChildAt(0)); // 将edge加入cell集合类 cells.add(edge); // 同理 DefaultEdge edge1 = new DefaultEdge(); edge1.setSource(cells.get(0).getChildAt(0)); edge1.setTarget(cells.get(2).getChildAt(0)); // 将edge加入cell集合类 cells.add(edge1); // 同理 DefaultEdge edge2 = new DefaultEdge(); edge2.setSource(cells.get(2).getChildAt(0)); edge2.setTarget(cells.get(1).getChildAt(0)); // 将edge加入cell集合类 cells.add(edge2); // 为edge设置一个箭头 int arrow = GraphConstants.ARROW_CLASSIC; GraphConstants.setLineEnd(edge.getAttributes(), arrow); GraphConstants.setEndFill(edge.getAttributes(), true); // 将以上定义的cells对象加入graph对象 Iterator it = cells.iterator(); while (it.hasNext()) { graph.getGraphLayoutCache().insert(it.next()); } // 一些graph对象的简单调整 // graph.setMoveable(false);//可否移动整个图形 // graph.setDisconnectable(false);//不能移动边的指向,但是可以移动图形 // graph.setDisconnectOnMove(false);//可否移动整个边,但是在边的源点终点改变后失效 // { graph.setGridEnabled(true); graph.setGridVisible(true); } // 显示网格 // graph.setMoveBelowZero(true); //是否允许cell越出左上角.通常设置为false,除非有特殊用处 graph.setAntiAliased(true);// 圆滑图像线条 // graph.setSelectionEnabled(false);//能否选择单个cell /* * 加入改变边属性代码位置 */ JFrame frame = new JFrame(); frame.getContentPane().add(new JScrollPane(graph)); frame.pack(); frame.setVisible(true); } } 由于在graph的cell中,决定其属性的是Attribute,这是一个map类型,所以,JGraph的edit,insert和remove方法都是使用对Attribute map的修改 在上例程序中的“加入改变属性颜色代码”位置加入以下修改代码,则可以改变其边的显示属性 //改变边颜色粗细 Map nested = new Hashtable(); Map attributeMap1 = new Hashtable(); GraphConstants.setLineColor(attributeMap1, Color.blue); GraphConstants.setLineWidth(attributeMap1, 5); nested.put(cells.get(4), attributeMap1); //改变边类型 Map attributeMap2 = new Hashtable(); GraphConstants.setLineColor(attributeMap2, Color.red); GraphConstants .setDashPattern(attributeMap2, new float[] { 5, 5, 5, 5 }); nested.put(cells.get(5), attributeMap2); //将改动加入graph graph.getGraphLayoutCache().edit(nested);// insert也可以使用类似的方法 |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。