承接上篇!!!
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-->
分享到:
相关推荐
NULL 博文链接:https://134team.iteye.com/blog/715603
JGraph Document
介绍JGraph框架,还有一些demo和一些架包!
jgraph源码+例子 jgraph源码+例子 jgraph源码+例子
jgraph画图实现代码,适合初学者,很多功能jgraph画图实现代码,适合初学者,很多功能jgraph画图实现代码,适合初学者,很多功能jgraph画图实现代码,适合初学者,很多功能jgraph画图实现代码,适合初学者,很多功能...
JGraph: manual Tutorial, 简介和使用技巧 可以快速上手,亦可深入研究
jgraph 学习手册 官方 画图 方便学习jgraph
JGraph 实现讲述
英文版本。介绍了如何利用JGraph包开发自己的图形程序,就内容而言是比较全的。比看那些抄来抄去,还没有实际意义的博客强多了!!!
Java图形组件 JGraph
JGraph 图形 流程图 文档 中文资料
jgraph一个简单的开始JGraph是一个开源的,兼容Swing的基于MVC体系结构图形组件
JGraph组件下载 JGraph.jar组件下载 javabean组件下载 图像绘制javabean
全面的jgraph使用示例,包括十多个例子,与详细的说明及源代码
NULL 博文链接:https://liujianeye.iteye.com/blog/1095824
博文链接:https://walnut.iteye.com/blog/113037
jgraph-draw.io web流程图
Jgraph所有的讲解,以及使用方法说明等
JGraph X 1.10.0.0 API文档,chm格式的。
JGRAPH开发jar 方便开发java的GUI