笔者最近公司的新项目需要对网络设备连接状态的进行监听。在网上找啊找,终于发现了JGraph这个开源的开发包可以利用。虽然用起来还是比较麻烦,但总比自己用SWING,AWT慢慢写要轻松许多。
下面简单介绍下最近摸索JGraph的用法(复杂的用法还没有研究出来),首先是整个的画布,JGraph的画布有一个GraphModel,画面上所有的对象都要添加在GraphModel中。其次是画面上的各种元素, JGraph将图元定义为一个一个的cell,每个cell可以是一个顶点(Vertex)、边(Edge)或者节点(Port)中的一种。顶点可以有邻接的顶点,他们通过边相联系,边连接的两个端点称为目标和源,每个目标或者源是一个节点。节点是顶点的孩子。每个cell都可以有自己的孩子。然后是为画布和画面上的元素添加属性。
每个cell的外观由相应的属性定义,属性序列是指一系列的键-值对,他们以Map形式组织,例如:
// 创建LOCAL SERVER对象及属性
DefaultGraphCell LocServer = new DefaultGraphCell("LOCAL SERVER");
Map LocServerattrib = new Hashtable();
attributes.put(LocServer, LocServerattrib);
GraphConstants.setIcon(LocServerattrib, icon);
GraphConstants.setBorder(LocServerattrib, BorderFactory
.createCompoundBorder());
GraphConstants.setEditable(LocServerattrib, false);
最后是添加事件,JGraph里有一个BasicMarqueeHandler类,应该是用来捕捉事件的,我的鼠标双击事件如下:
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
Object[] obj = BasicMarqueeHandler.getGraphForEvent(e)
.getSelectionCells();
if (obj.length != 0) {
System.out.println(obj[obj.length - 1].toString());
}
}
}
对于JGraph,我是在摸索中学习的,只能简单的介绍下最基本的应用,由于工作进度,只能先这样用了,还请各位高手多多指点。下面是我学习摸索时做的小例子, 请大家多提宝贵意见。
1package example;
2
3import java.awt.BorderLayout;
4import java.awt.event.MouseEvent;
5import java.awt.event.MouseListener;
6import java.awt.geom.Rectangle2D;
7import java.awt.Dimension;
8import java.awt.Font;
9import java.awt.Color;
10
11import java.util.Hashtable;
12import java.util.Map;
13
14import javax.swing.BorderFactory;
15import javax.swing.Icon;
16import javax.swing.ImageIcon;
17import javax.swing.JFrame;
18import javax.swing.JPanel;
19import javax.swing.JScrollPane;
20
21import org.jgraph.JGraph;
22import org.jgraph.graph.BasicMarqueeHandler;
23import org.jgraph.graph.ConnectionSet;
24import org.jgraph.graph.DefaultEdge;
25import org.jgraph.graph.DefaultGraphCell;
26import org.jgraph.graph.DefaultGraphModel;
27import org.jgraph.graph.DefaultPort;
28import org.jgraph.graph.GraphConstants;
29import org.jgraph.graph.GraphModel;
30
31public class topology {
32 /** *//**
33 * 本程序用于画网络拓扑图
34 */
35 public static void main(String[] args) {
36 JFrame frame = new topologyFrame(4);
37 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
38 frame.setVisible(true);
39 }
40}
41
42/** *//**
43 * 主画面
44 */
45class topologyFrame extends JFrame {
46 private static final int DEFAULT_WIDTH = 1024;
47 private static final int DEFAULT_HEIGHT = 768;
48
49 public topologyFrame(int iNumber) {
50 this.setTitle("拓扑图");
51 this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
52
53 final topologyPanel panel = new topologyPanel(iNumber);
54 this.add(panel, BorderLayout.CENTER);
55
56 }
57}
58
59/** *//**
60 * 拓扑图面板
61 */
62class topologyPanel extends JPanel {
63 private static final int PANEL_WIDTH = 1024;
64 private static final int PANEL_HEIGHT = 768;
65 private static final int SERVER_WIDTH = 64;
66 private static final int SERVER_HEIGHT = 96;
67 private static final int LINE_LENGTH = 250;
68 private static final String IMAGE_DIR = "D:\\testimg\\SERVER.jpg";
69
70 public topologyPanel(int iServerNum) {
71 setSize(PANEL_WIDTH, PANEL_HEIGHT);
72 this.add(new JScrollPane(setTopologyMaker(iServerNum)));
73 }
74
75 /** *//**
76 * 通过点坐标画线
77 */
78 private JGraph setTopologyMaker(int iServer) {
79
80 GraphModel model = new DefaultGraphModel();
81 Object[] cells = null;
82 final JGraph graph = new JGraph(model);
83 graph.setSelectionEnabled(true);
84 graph.addMouseListener(new MouseEventDemo());
85 Map attributes = new Hashtable();
86 Map LocServerattrib = new Hashtable();
87 Icon icon = new ImageIcon(IMAGE_DIR);
88 double iAngle = 0;
89
90 graph.setMinimumSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
91 graph.setAutoResizeGraph(false);
92 graph.setSizeable(false);
93 graph.setLocation(0, 0);
94 graph.setSize(PANEL_WIDTH, PANEL_HEIGHT);
95
96 // 创建LOCAL SERVER对象及属性
97 DefaultGraphCell LocServer = new DefaultGraphCell("LOCAL SERVER");
98 attributes.put(LocServer, LocServerattrib);
99 GraphConstants.setIcon(LocServerattrib, icon);
100 GraphConstants.setBorder(LocServerattrib, BorderFactory
101 .createCompoundBorder());
102 GraphConstants.setEditable(LocServerattrib, false);
103
104 // 中心显示本地SERVER
105 attributes.put(LocServer, LocServerattrib);
106 Rectangle2D LocServerbounds = new Rectangle2D.Double(graph.getWidth() / 2
107 - SERVER_WIDTH / 2, graph.getHeight() / 2 - SERVER_HEIGHT / 2,
108 SERVER_WIDTH, SERVER_HEIGHT);
109 GraphConstants.setBounds(LocServerattrib, LocServerbounds);
110 GraphConstants.setValue(LocServerattrib, "LOCAL SERVER");
111 GraphConstants.setFont(LocServerattrib, new Font(null, Font.BOLD, 10));
112
113 DefaultPort LocPort = new DefaultPort();
114 LocServer.add(LocPort);
115
116 cells = new Object[] { LocServer };
117 model.insert(cells, attributes, null, null, null);
118
119 if (iServer != 0) {
120 iAngle = 360 / iServer;
121 } else {
122 iAngle = 0;
123 }
124
125 for (int i = 0; i < iServer; i++) {
126 double x;
127 double y;
128
129 x = graph.getWidth()
130 / 2
131 + (java.lang.Math.cos(((iAngle) * i + 45)
132 * java.lang.Math.PI / 180) * LINE_LENGTH)
133 - SERVER_WIDTH / 2;
134 y = graph.getHeight()
135 / 2
136 - (java.lang.Math.sin(((iAngle) * i + 45)
137 * java.lang.Math.PI / 180) * LINE_LENGTH)
138 - SERVER_HEIGHT / 2;
139
140 // 创建PEER SERVER对象及属性
141 DefaultGraphCell peerServer = new DefaultGraphCell("PEER SERVER"
142 + (i + 1));
143 Map PeerServerattrib = new Hashtable();
144 attributes.put(peerServer, PeerServerattrib);
145 GraphConstants.setIcon(PeerServerattrib, icon);
146 GraphConstants.setBorder(PeerServerattrib, BorderFactory
147 .createCompoundBorder());
148 GraphConstants.setEditable(PeerServerattrib, false);
149
150 attributes.put(peerServer, PeerServerattrib);
151
152 Rectangle2D PeerMTAbounds = new Rectangle2D.Double(x, y, SERVER_WIDTH,
153 SERVER_HEIGHT);
154 // System.out.println("(" + x + "," + y + ")");
155 GraphConstants.setBounds(PeerServerattrib, PeerMTAbounds);
156 GraphConstants.setValue(PeerServerattrib, "PEER SERVER" + (i + 1));
157 GraphConstants
158 .setFont(PeerServerattrib, new Font(null, Font.BOLD, 10));
159
160 DefaultPort PeerPort = new DefaultPort();
161 peerServer.add(PeerPort);
162
163 // 创建边对象及属性
164 DefaultEdge edge = new DefaultEdge();
165 Map edgeattrib = new Hashtable();
166 attributes.put(edge, edgeattrib);
167 int arrow = GraphConstants.ARROW_NONE;
168 GraphConstants.setLineEnd(edgeattrib, arrow);
169 GraphConstants.setEditable(edgeattrib, false);
170 GraphConstants.setLabelAlongEdge(edgeattrib, true);
171 GraphConstants.setEndFill(edgeattrib, true);
172 GraphConstants.setDisconnectable(edgeattrib, false);
173 GraphConstants.setValue(edgeattrib, "R" + (i + 1));
174 GraphConstants.setFont(edgeattrib, new Font(null, Font.BOLD, 10));
175 GraphConstants.setLineColor(edgeattrib, Color.GREEN);
176 GraphConstants.setLineWidth(edgeattrib, 1.5f);
177
178 // 连接边的两个节点
179 ConnectionSet cs = new ConnectionSet(edge, LocPort, PeerPort);
180
181 cells = new Object[] { edge, peerServer };
182
183 // 向model添加cells对象
184 model.insert(cells, attributes, cs, null, null);
185
186 }
187 return graph;
188 }
189}
190
191class MouseEventDemo implements MouseListener {
192
193 public void mouseClicked(MouseEvent e) {
194 if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
195 Object[] obj = BasicMarqueeHandler.getGraphForEvent(e)
196 .getSelectionCells();
197 if (obj.length != 0) {
198 System.out.println(obj[obj.length - 1].toString());
199 }
200 }
201 }
202
203 public void mouseEntered(MouseEvent e) {
204 // System.out.println("mouseEntered");
205 }
206
207 public void mouseExited(MouseEvent e) {
208 // System.out.println("mouseExited");
209 }
210
211 public void mousePressed(MouseEvent e) {
212 // System.out.println("mousePressed");
213 }
214
215 public void mouseReleased(MouseEvent e) {
216 // System.out.println("mouseReleased");
217 }
218}
posted on 2008-08-29 10:08
小狐狸 阅读(5602)
评论(3) 编辑 收藏 所属分类:
J2SE