- 论坛徽章:
- 0
|
从最简单的Jgraph程序我们可以发现创建Jgraph程序的框架。
1. 首先创建GraphModel,
GraphModel model = new DefaultGraphModel();
2.创建GraphLayoutCache,
GraphLayoutCache cache = new GraphLayoutCache(model,
new DefaultCellViewFactory());
3.创建JGraph
JGraph g = new JGraph(model, cache);
4.创建图上的元素,包括节点,边,port,以及分组等。
DefaultGraphCell[] cells = new DefaultGraphCell[3];
cells[0] = new DefaultGraphCell("Hello");
cells[0].addPort();
GraphConstants.setBounds(cells[0].getAttributes(), new Rectangle2D.Double(100, 100, 40, 20));
cells[1] = new DefaultGraphCell("World");
cells[1].addPort();
GraphConstants.setBounds(cells[1].getAttributes(), new Rectangle2D.Double(200, 200, 40, 20));
DefaultEdge edge = new DefaultEdge();
edge.setSource(cells[0].getChildAt(0));
edge.setTarget(cells[1].getChildAt(0));
cells[2] = edge;
5.将创建的元素添加到Jgraph上
g.getGraphLayoutCache().insert(cells);
6.将Jgraph显示
JFrame frame = new JFrame("Example3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(g);
frame.pack();
frame.setVisible(true);
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/33011/showart_257845.html |
|