code.txt
上传用户:zl20001122
上传日期:2021-12-13
资源大小:3k
文件大小:12k
源码类别:

Applet

开发平台:

Java

  1. java applet网络拓扑图的源代码
  2. 源代码见下:
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import com.zhtelecom.common.topograph.*;
  6. //演示拓扑图的主要功能。可同时在Applet和应用程序中运行。
  7. //network的展开和隐藏;没有限制的级别,network下可再挂network,node或link
  8. //树的图形显示
  9. //图形改变后,拓扑图自动更新
  10. //颜色自动向上改变
  11. //放大、缩小 转到上层
  12. //Link 路由模式和直连模式
  13. //添加、删除告警,自动触发颜色变化
  14. //鼠标、菜单处理
  15. //树选择与拓扑图选择同步
  16. //添加、删除 节点、网络、连接
  17. //<p>Copyright: [url]www.zhtelecom.com[/url] Copyright (c) 2007</p>
  18. public class NMSExample extends JApplet
  19. {
  20.     static int i = 1;
  21.     public static int getNum()
  22.     {
  23.         return i++;
  24.     }
  25.     /**
  26.      * WEB Applet小程序初始化接口。也是Application应用程序的初始化接口。
  27.      * 构建拓扑图框架。
  28.      */
  29.     public void init()
  30.     {
  31.         try
  32.         {
  33.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  34.             //创建数据源
  35.             TopoDataSource source = new TopoDataSource();
  36.             //通过数据源创建拓扑视图。
  37.             TopoGraphView topoView = new TopoGraphView(source);
  38.             topoView.getStatusLabel().setText("状态栏");
  39.             //初始化图形界面。Swing技术。
  40.             JScrollPane sGraph = new JScrollPane(topoView);
  41.             //设置拓扑图视图的事件处理程序。
  42.             TopoMouseAction menuHandle = new TopoMouseAction();
  43.             topoView.setMouseHandle(menuHandle);
  44.             //根据数据源创建树
  45.             TopoTreeView tree = new TopoTreeView(source);
  46.             //将树的用户选择对象、拓扑图的用户选择对象,同步起来。
  47.             tree.syncTopoview(topoView);
  48.             JScrollPane sTree = new JScrollPane(tree);
  49.             JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sTree,
  50.                                              sGraph);
  51.             pane.setDividerLocation(200);
  52.             this.getContentPane().setLayout(new BorderLayout());
  53.             this.getContentPane().add(pane, BorderLayout.CENTER);
  54.             //this.setVisible(true);
  55.             //最后一步,单独加载拓扑数据。(也可以选择先加载拓扑数据,再构造拓扑视图)
  56.             processData(topoView);
  57.         } catch (Exception ex)
  58.         {
  59.             ex.printStackTrace();
  60.         }
  61.     }
  62.     /**
  63.      * 单独集中处理拓扑数据。
  64.      * 将拓扑图的数据处理与界面显示分开处理。
  65.      *
  66.      */
  67.     private void processData(TopoGraphView topoView)
  68.     {
  69.         //获取数据源
  70.         TopoDataSource source = topoView.getTopoDataSource();
  71.         //定义多业务汇聚网络
  72.         TopoNetwork serviceNet = new TopoNetwork(new StringBuffer("多业务汇聚网")); // 使用StringBuffer,便于后面可修改
  73.         serviceNet.setXY(163, 131);
  74.         //定义多业务汇聚网内设备
  75.         TopoNode nodePC = new TopoNode(new StringBuffer("PC"));
  76.         nodePC.setXY(15, 273); // 设置x/y坐标
  77.         TopoNode nodeTV = new TopoNode(new StringBuffer("TV机顶盒"));
  78.         nodeTV.setXY(15, 166);
  79.         nodeTV.setImageIconFromTopoFile("backRecovery.png"); //设置图片类型
  80.         TopoNode nodePhone = new TopoNode(new StringBuffer("IP电话"));
  81.         nodePhone.setXY(15, 57);
  82.         nodePhone.setImageIconFromTopoFile("digiphone.png");
  83.         TopoNode nodeGW = new TopoNode(new StringBuffer("家庭网关"));
  84.         nodeGW.setXY(151, 155);
  85.         nodeGW.setImageIconFromTopoFile("dm.png");
  86.         TopoNode nodeAcess = new TopoNode(new StringBuffer("接入设备"));
  87.         nodeAcess.setXY(281, 160);
  88.         TopoNode serverManager = new TopoNode(new StringBuffer("业务服务器"));
  89.         serverManager.setXY(301, 459);
  90.         serverManager.setImageIconFromTopoFile("server_db.png");
  91.         nodeAcess.setImageIconFromTopoFile("cartridge_system.png");
  92.         TopoNode serverDB = new TopoNode(new StringBuffer("数据库"));
  93.         serverDB.setXY(431, 452);
  94.         serverDB.setImageIconFromTopoFile("single_database.png");
  95.         TopoNode serverWeb = new TopoNode(new StringBuffer("网站服务器"));
  96.         serverWeb.setXY(538, 459);
  97.         serverWeb.setImageIconFromTopoFile("server.png");
  98.         TopoNode routerAccess = new TopoNode(new StringBuffer("接入路由器"));
  99.         routerAccess.setImageIconFromTopoFile("tw130.png");
  100.         routerAccess.setXY(415, 148);
  101.         TopoNode routerData = new TopoNode(new StringBuffer("数据路由器"));
  102.         routerData.setXY(584, 48);
  103.         routerData.setImageIconFromTopoFile("tw130.png");
  104.         TopoNode routerPhone = new TopoNode(new StringBuffer("语音路由器"));
  105.         routerPhone.setXY(702, 150);
  106.         routerPhone.setImageIconFromTopoFile("tw130.png");
  107.         TopoNode routerTV = new TopoNode(new StringBuffer("视频路由器"));
  108.         routerTV.setXY(584, 235);
  109.         routerTV.setImageIconFromTopoFile("tw130.png");
  110.         //定义多业务汇聚网内连接
  111.         TopoLink linkPCGW = new TopoLink(new StringBuffer("100M"), nodePC, nodeGW); //通过连接起点、终点,创建Link
  112.         linkPCGW.setStraight(false); //设置转折线
  113.         linkPCGW.setBrokenLink(true); //设置虚线
  114.         linkPCGW.setLinkWidth(1); //设置宽度
  115.         TopoLink linkTVGW = new TopoLink(new StringBuffer("Cable"), nodeTV, nodeGW);
  116.         linkTVGW.setStraight(false);
  117.         linkTVGW.setBrokenLink(true);
  118.         linkTVGW.setLinkWidth(1);
  119.         TopoLink linkPhoneGW = new TopoLink(new StringBuffer("RJ45"), nodePhone, nodeGW);
  120.         linkPhoneGW.setStraight(false);
  121.         linkPhoneGW.setBrokenLink(true);
  122.         linkPhoneGW.setLinkWidth(1);
  123.         TopoLink linkGWAcess = new TopoLink(new StringBuffer("FE"), nodeGW, nodeAcess);
  124.         TopoLink linkAcessRouterAC = new TopoLink(new StringBuffer("GE"), nodeAcess, routerAccess);
  125.         TopoLink linkAcessManager = new TopoLink(new StringBuffer("L1"), routerAccess, serverManager);
  126.         linkAcessManager.setStraight(false);
  127.         TopoLink linkAcessDB = new TopoLink(new StringBuffer("L2"), routerAccess, serverDB);
  128.         linkAcessDB.setStraight(false);
  129.         TopoLink linkAcessWeb = new TopoLink(new StringBuffer("L3"), routerAccess, serverWeb);
  130.         linkAcessWeb.setStraight(false);
  131.         TopoLink linkRouterACData = new TopoLink(new StringBuffer("G1"), routerAccess, routerData);
  132.         linkRouterACData.setShowArrow(false);
  133.         linkRouterACData.setLinkWidth(5);
  134.         TopoLink linkRouterACTV = new TopoLink(new StringBuffer("G2"), routerAccess, routerTV);
  135.         linkRouterACTV.setShowArrow(false);
  136.         linkRouterACTV.setLinkWidth(5);
  137.         TopoLink linkRouterACPhone = new TopoLink(new StringBuffer("G3"), routerAccess, routerPhone);
  138.         linkRouterACPhone.setShowArrow(false);
  139.         linkRouterACPhone.setLinkWidth(5);
  140.         TopoLink linkRouterTVData = new TopoLink(new StringBuffer("备份1"), routerTV, routerData);
  141.         linkRouterTVData.setShowArrow(false);
  142.         linkRouterTVData.setBrokenLink(true);
  143.         TopoLink linkRouterTVPhone = new TopoLink(new StringBuffer("备份2"), routerTV, routerPhone);
  144.         linkRouterTVPhone.setShowArrow(false);
  145.         linkRouterTVPhone.setBrokenLink(true);
  146.         TopoLink linkRouterPhoneData = new TopoLink(new StringBuffer("备份3"), routerPhone, routerData);
  147.         linkRouterPhoneData.setShowArrow(false);
  148.         linkRouterPhoneData.setBrokenLink(true);
  149.         //添加告警
  150.         nodePC.addAlarm("alarm1", TopoSysConfig.AlarmSeverity_Critical); //紧急
  151.         nodeTV.addAlarm("alarm2", TopoSysConfig.AlarmSeverity_Major); //主要
  152.         nodePhone.addAlarm("alarm3", TopoSysConfig.AlarmSeverity_Minor); //次要;
  153.         nodeGW.addAlarm("alarm4", TopoSysConfig.AlarmSeverity_Warning); //警告;
  154.         linkGWAcess.addAlarm("alarm5", TopoSysConfig.AlarmSeverity_Indeterminate); //未知
  155.         nodeAcess.addAlarm("alarm6", TopoSysConfig.AlarmSeverity_Indeterminate); //未知
  156.         ////将多业务汇聚网络的数据放入到数据源容器中......................
  157.         source.addTopoData(serviceNet); //serviceNet在根视图下
  158.         source.addTopoData(nodePC, serviceNet); //nodePC在serviceNet下
  159.         source.addTopoData(nodeTV, serviceNet);
  160.         source.addTopoData(nodePhone, serviceNet);
  161.         source.addTopoData(nodeGW, serviceNet);
  162.         source.addTopoData(nodeAcess, serviceNet);
  163.         source.addTopoData(routerAccess, serviceNet);
  164.         source.addTopoData(serverManager, serviceNet);
  165.         source.addTopoData(serverWeb, serviceNet);
  166.         source.addTopoData(serverDB, serviceNet);
  167.         source.addTopoData(routerTV, serviceNet);
  168.         source.addTopoData(routerData, serviceNet);
  169.         source.addTopoData(routerPhone, serviceNet);
  170.         source.addTopoData(linkPCGW, serviceNet);
  171.         source.addTopoData(linkTVGW, serviceNet);
  172.         source.addTopoData(linkPhoneGW, serviceNet);
  173.         source.addTopoData(linkGWAcess, serviceNet);
  174.         source.addTopoData(linkAcessRouterAC, serviceNet);
  175.         source.addTopoData(linkAcessDB, serviceNet);
  176.         source.addTopoData(linkAcessManager, serviceNet);
  177.         source.addTopoData(linkAcessWeb, serviceNet);
  178.         source.addTopoData(linkRouterACData, serviceNet);
  179.         source.addTopoData(linkRouterACTV, serviceNet);
  180.         source.addTopoData(linkRouterACPhone, serviceNet);
  181.         source.addTopoData(linkRouterTVData, serviceNet);
  182.         source.addTopoData(linkRouterTVPhone, serviceNet);
  183.         source.addTopoData(linkRouterPhoneData, serviceNet);
  184. //................................................//
  185.         //定义多子网网络和下属设备、连接
  186.         TopoNetwork mutiNet = new TopoNetwork(new StringBuffer("多子网网络"));
  187.         mutiNet.setXY(392, 242);
  188.         TopoNetwork internet = new TopoNetwork(new StringBuffer("Internet"));
  189.         internet.setXY(331, 303);
  190.         TopoNetwork pstn = new TopoNetwork(new StringBuffer("电话网络"));
  191.         pstn.setXY(330, 105);
  192.         TopoNetwork vide = new TopoNetwork(new StringBuffer("视频网络"));
  193.         vide.setXY(140, 192);
  194.         TopoNode nodesw = new TopoNode(new StringBuffer("交换机"));
  195.         nodesw.setXY(158, 393);
  196.         TopoNode nodeVod1 = new TopoNode(new StringBuffer("视频服务器1"));
  197.         nodeVod1.setXY(100, 100);
  198.         TopoNode nodeVod2 = new TopoNode(new StringBuffer("视频服务器2"));
  199.         nodeVod2.setXY(300, 100);
  200.         TopoLink link1 = new TopoLink(new StringBuffer("Link1"), internet, pstn);
  201.         TopoLink link2 = new TopoLink(new StringBuffer("Link2"), pstn, vide);
  202.         TopoLink link3 = new TopoLink(new StringBuffer("Link3"), vide, internet);
  203.         TopoLink link = new TopoLink(new StringBuffer("video"), nodeVod1, nodeVod2);
  204.         //添加告警
  205.         pstn.addAlarm("a1", TopoSysConfig.AlarmSeverity_Major);
  206.         nodeVod1.addAlarm("a2", TopoSysConfig.AlarmSeverity_Critical);
  207.         nodesw.addAlarm("a3", TopoSysConfig.AlarmSeverity_Warning);
  208.         //将多子网网络内数据加入到数据源
  209.         source.addTopoData(mutiNet);
  210.         source.addTopoData(internet, mutiNet);
  211.         source.addTopoData(pstn, mutiNet);
  212.         source.addTopoData(vide, mutiNet);
  213.         source.addTopoData(nodesw, mutiNet);
  214.         source.addTopoData(link1, mutiNet);
  215.         source.addTopoData(link2, mutiNet);
  216.         source.addTopoData(link3, mutiNet);
  217.         source.addTopoData(nodeVod1, vide);
  218.         source.addTopoData(nodeVod2, vide);
  219.         source.addTopoData(link, vide);
  220.     }
  221.     //Appliction程序入口,兼容Applet模式。
  222.     public static void main(String[] args)
  223.     {
  224.         //初始化数据和图形
  225.         NMSExample helloApplet = new NMSExample();
  226.         helloApplet.init();
  227.         //创建Application 窗口
  228.         JFrame frame = new JFrame("网络拓扑图演示 u2014u2014 智和信通出品");
  229.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  230.         frame.setSize(screenSize.width, screenSize.height);
  231.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  232.         frame.setContentPane(helloApplet.getContentPane());
  233.         frame.setVisible(true);
  234.     }