jgraph php,java – 在JPanel上绘制SimpleWeightedGraph

看起来你要从你的问题中留下一些重要的细节,如果没有

Minimal,Complete,and Verifiable example,很难说问题出在哪里.

但请注意,您尝试采用的样本非常陈旧. JGraph已经转向JGraphX.请考虑以下示例,该示例演示了使用JGraphXAdapter的JGraphT和JGraphX的链接.

import javax.swing.JFrame;

import javax.swing.SwingUtilities;

import org.jgrapht.ListenableGraph;

import org.jgrapht.ext.JGraphXAdapter;

import org.jgrapht.graph.DefaultWeightedEdge;

import org.jgrapht.graph.ListenableDirectedWeightedGraph;

import com.mxgraph.layout.mxCircleLayout;

import com.mxgraph.layout.mxIGraphLayout;

import com.mxgraph.swing.mxGraphComponent;

public class DemoWeightedGraph {

private static void createAndShowGui() {

JFrame frame = new JFrame("DemoGraph");

frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

ListenableGraph g = buildGraph();

JGraphXAdapter graphAdapter =

new JGraphXAdapter(g);

mxIGraphLayout layout = new mxCircleLayout(graphAdapter);

layout.execute(graphAdapter.getDefaultParent());

frame.add(new mxGraphComponent(graphAdapter));

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGui();

}

});

}

public static class MyEdge extends DefaultWeightedEdge {

@Override

public String toString() {

return String.valueOf(getWeight());

}

}

public static ListenableGraph buildGraph() {

ListenableDirectedWeightedGraph g =

new ListenableDirectedWeightedGraph(MyEdge.class);

String x1 = "x1";

String x2 = "x2";

String x3 = "x3";

g.addVertex(x1);

g.addVertex(x2);

g.addVertex(x3);

MyEdge e = g.addEdge(x1,x2);

g.setEdgeWeight(e,1);

e = g.addEdge(x2,x3);

g.setEdgeWeight(e,2);

e = g.addEdge(x3,x1);

g.setEdgeWeight(e,3);

return g;

}

}

请注意,MyEdge扩展了DefaultWeightedEdge以提供显示边缘权重的自定义toString().一个更干净的解决方案可能是覆盖mxGraph.convertValueToString,检查单元格的内容并根据需要提供自定义标签. toString是演示的快捷方式,我也注意到DefaultWeightedEdge.getWeight()受到保护,所以无论如何都需要扩展:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值