关于项目中用到jgraph和applet的一点总结(2)
承接上篇!!!
private?void?addEdge(NodeBean?source,?NodeBean?target,?int?weight,
String?edgeString)?{
?
if?(source?==?null?||?target?==?null)?{
return;
}
if?(gefNodeMap?==?null)?{
gefNodeMap?=?new?HashMap();
}
if?(graphNodeMap?==?null)?{
graphNodeMap?=?new?HashMap();
}
if?(edgeList?==?null)?{
edgeList?=?new?ArrayList();
}
if?(directedGraph?==?null)?{
directedGraph?=?new?DirectedGraph();
}
?
addEdgeGef(source,?target,?weight,?edgeString);
?
DefaultGraphCell?sourceNode?=?null;
DefaultGraphCell?targetNode?=?null;
sourceNode?=?(DefaultGraphCell)?graphNodeMap.get(source);
if?(sourceNode?==?null)?{
sourceNode?=?new?DefaultGraphCell(source);
sourceNode.addPort();
graphNodeMap.put(source,?sourceNode);
}
targetNode?=?(DefaultGraphCell)?graphNodeMap.get(target);
if?(targetNode?==?null)?{
targetNode?=?new?DefaultGraphCell(target);
targetNode.addPort();
graphNodeMap.put(target,?targetNode);
}
DefaultEdge?edge?=?new?DefaultEdge(edgeString);
UnionEdge?unionEdge?=?new?UnionEdge();
unionEdge.setEdge(edge);
unionEdge.setSourceNode(sourceNode);
unionEdge.setTargetNode(targetNode);
?
edgeList.add(unionEdge);
?
}
?
private?void?addEdgeGef(NodeBean?source,?NodeBean?target,?int?weight,
String?edgeString)?{
?
if?(source.equals(target))?{
return;
}
Node?gefSourceNode?=?null;
Node?gefTargetNode?=?null;
gefSourceNode?=?(Node)?gefNodeMap.get(source);
if?(gefSourceNode?==?null)?{
gefSourceNode?=?new?Node();
gefSourceNode.width?=?GraphProp.NODE_WIDTH;
gefSourceNode.height?=?GraphProp.NODE_WIDTH;
directedGraph.nodes.add(gefSourceNode);
gefNodeMap.put(source,?gefSourceNode);
}
?
gefTargetNode?=?(Node)?gefNodeMap.get(target);
if?(gefTargetNode?==?null)?{
gefTargetNode?=?new?Node();
gefTargetNode.width?=?GraphProp.NODE_WIDTH;
gefTargetNode.height?=?GraphProp.NODE_WIDTH;
directedGraph.nodes.add(gefTargetNode);
gefNodeMap.put(target,?gefTargetNode);
}
?
Edge?gefEdge1?=?null;
try?{
gefEdge1?=?new?Edge(gefSourceNode,?gefTargetNode);
gefEdge1.weight?=?weight;
directedGraph.edges.add(gefEdge1);
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
?
public?List?send(String?hh,?String?khdm,?NodeBean?nb)?{
ObjectInputStream?in?=?null;
try?{
URL?url?=?null;
if(nb?!=?null?&&?hh?==?null?&&?khdm?==?null){
url?=?new?URL(getCodeBase(),?"Riceive?type="?+?nb.getType()?+?"&code="?+?nb.getKhdm());
}else{
url?=?new?URL(getCodeBase(),?"Riceive?hh="?+?hh?+?"&khdm="?+?khdm);
}
connect?=?url.openConnection();
showStatus("Open?Connection!");
connect.connect();
showStatus("Open?Sream!");
in?=?new?ObjectInputStream(connect.getInputStream());
showStatus("reading");
List?list?=?(List)?in.readObject();
return?list;
}?catch?(Exception?e)?{
e.printStackTrace();
return?null;
}?finally?{
try?{
in.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
?
public?String?send2(String?hh,?String?khdm,?NodeBean?nb)?{
ObjectInputStream?in?=?null;
try?{
URL?url?=?null;
if(nb?!=?null?&&?hh?==?null?&&?khdm?==?null){
url?=?new?URL(getCodeBase(),?"Riceive?type="?+?nb.getType()?+?"&code="?+?nb.getKhdm());
}else{
url?=?new?URL(getCodeBase(),?"Riceive?hh="?+?hh?+?"&khdm="?+?khdm);
}
connect?=?url.openConnection();
showStatus("Open?Connection!");
connect.connect();
showStatus("Open?Sream!");
in?=?new?ObjectInputStream(connect.getInputStream());
showStatus("reading");
String?res?=?in.readObject().toString();
return?res;
}?catch?(Exception?e)?{
e.printStackTrace();
return?null;
}?finally?{
try?{
in.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
?
public?void?labelIntroduce(GraphModel?model,?Map?attributes){
?
}
?
public?void?mouseClicked(MouseEvent?e)?{
int?x?=?e.getX();
int?y?=?e.getY();
DefaultGraphCell?node?=?(DefaultGraphCell)?graph.getSelectionCellAt(new?Point2D.Double(x,?y));
NodeBean?userObject?=?(NodeBean)node.getUserObject();
if(userObject.getType().equals("4")){
this.paintGraph(tempList);
}else{
String?res?=?this.send2(null,?null,?userObject);
?
JSObject?win=JSObject.getWindow(this);
win.call("setText",?new?Object[]{res});
}
}
?
public?void?mouseEntered(MouseEvent?e)?{
?
}
?
public?void?mouseExited(MouseEvent?e)?{
?
}
?
public?void?mousePressed(MouseEvent?e)?{
?
}
?
public?void?mouseReleased(MouseEvent?e)?{
?
}
}
3.?Jgraph图结合applet显示在网页
?
当用户访问这样的网页时,Applet被下载到用户的计算机上执行,但前提是用户使用的是支持Java的网络浏览器。
在页面上部署applet:
<object?classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase="jre-6u4-windows-i586-p.exe"
width="550"?height="450">
<param?name="CODE"?value="test.TestGraph.class">
<param?name="CODEBASE"?value=".">?
<param?name="hh"?value="<%=hh%>">
<param?name="khdm"?value="<%=khdm%>">
Could?not?find?a?plugin?supported?by?your?browser.?
Please?waiting?for?download?Sun's?Java?JRE?1.6.0
</object>
使用object是为了解决用户浏览器没有jre环境时自动下载jre。
?
?
4.?关于applet访问数据库得到数据动态显示jgraph图到网页
?
有一种方法是直接在applet类里的init方法里写纯jdbc代码,本人感觉这种方法繁琐且有限制。可以用servlet与applet之间的通信解决所有问题。
?
Applet代码:
URL?url?=?null;
if(nb?!=?null?&&?hh?==?null?&&?khdm?==?null){
url?=?new?URL(getCodeBase(),?"Riceive?type="?+?nb.getType()?+?"&code="?+?nb.getKhdm());
}else{
url?=?new?URL(getCodeBase(),?"Riceive?hh="?+?hh?+?"&khdm="?+?khdm);
}
connect?=?url.openConnection();
showStatus("Open?Connection!");
connect.connect();
showStatus("Open?Sream!");
in?=?new?ObjectInputStream(connect.getInputStream());
showStatus("reading");
List?list?=?(List)?in.readObject();
?
Servlet代码:
WebApplicationContext?appContext?=?WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
JgraphUtil?ju?=?(JgraphUtil)appContext.getBean("JgraphUtil");
?
ObjectOutputStream?out?=?new?ObjectOutputStream(?res.getOutputStream()?);
if(hh!=null?&&?khdm!=null?){
List?list?=?ju.jgraph(hh,?khdm);
?
out.writeObject(list);
out.flush();
out.close();
}
5.?关于响应鼠标事件后动态显示网页
?
将jgraph图显示到网页,点击某个图形节点时,能在图形的外面网页上显示相应的详细信息,这个时候需要用到JSObject这个类。这个类一般都已安装的jre里的lib文件夹的plugin.jar里,将这个jar包导入即可。
?
代码示例:
JSObject?win=JSObject.getWindow(this);
win.call("setText",?new?Object[]{res});//调用JavaScript自定义方法
<!--EndFragment-->