开启辅助访问best365还可以买足彩吗

行健网络

?找回密码
?立即注册
查看: 2249|回复: 0
打印 上一主题 下一主题

[其它] 第一个jgraphx程序

[复制链接]
跳转到指定楼层
楼主
发表于 2016-5-31 10:00:33 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

直接上代码

[Java] syntaxhighlighter_viewsource syntaxhighlighter_copycode
import java.util.Map;
import javax.swing.JFrame;
import com.mxgraph.layout.mxFastOrganicLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;

public class SimpleGraph {
        public static void main(String[] args){
                mxGraph graph = new mxGraph();
                mxCell parent = (mxCell)graph.getDefaultParent();
                mxIGraphModel model = graph.getModel();
                
                mxStylesheet sheet =graph.getStylesheet();
                Map map = sheet.getDefaultVertexStyle();
                map.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_ELLIPSE);
                
                model.beginUpdate();
                try{
                        //添加顶点
                        Object a = graph.insertVertex(parent, null, "A", 0, 0, 20, 20);
                        Object b = graph.insertVertex(parent, null, "B", 100, 0, 20, 20);
                        Object c = graph.insertVertex(parent, null, "C", 200, 0, 20, 20);
                        Object d = graph.insertVertex(parent, null, "D", 100, 100, 20, 20);
                        Object e = graph.insertVertex(parent, null, "E", 200, 100, 20, 20);
                        Object f = graph.insertVertex(parent, null, "F", 300, 100, 20, 20);
                        Object g = graph.insertVertex(parent, null, "G", 100, 200, 20, 20);
                        Object h = graph.insertVertex(parent, null, "H", 200, 200, 20, 20);
                        Object i = graph.insertVertex(parent, null, "I", 300, 200, 20, 20);
                        
                        //添加边
                        graph.insertEdge(parent, null, "", a, b);
                        graph.insertEdge(parent, null, "", b, c);
                        graph.insertEdge(parent, null, "", b, d);
                        graph.insertEdge(parent, null, "", c, b);
                        graph.insertEdge(parent, null, "", c, f);
                        graph.insertEdge(parent, null, "", d, g);
                        graph.insertEdge(parent, null, "", e, c);
                        graph.insertEdge(parent, null, "", f, i);
                        graph.insertEdge(parent, null, "", g, a);
                        graph.insertEdge(parent, null, "", g, h);
                        graph.insertEdge(parent, null, "", h, e);
                        graph.insertEdge(parent, null, "", i, h);
                }finally{
                        model.endUpdate();
                }
                
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mxGraphComponent comp = new mxGraphComponent(graph);
                frame.getContentPane().add(comp);
                frame.pack();
                frame.setVisible(true);
        }
}

输出结果如下图:


上面的代码比较简单,没有什么很多要说明的,唯一要注意的是,在插入节点和边时,最好是包围在model.beginUpdate()与mode.endUpdate()之间,特别是在插入大量的节点情况下

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|网站地图|行健网络

GMT+8, 2019-10-16 11:14 , Processed in 0.080818 second(s), 17 queries .

Powered by Discuz! X3.2

? 2001-2013 Comsenz Inc.

快速回复 返回顶部 best365还可以买足彩吗