我正在用Java编写我的第一个实际执行UI的程序,所以如果这个问题的答案是显而易见的,请耐心等待。
我使用JGraph 5 (5.14)来可视化由JGrapht (0.8.3)创建的图形。
我可以用JGrapht创建图形,而且我相信使用org.jgrapht.ext.JGraphModelAdapter可以将其转换为JGraph OK。问题是,当结果显示在窗口中时(我使用的是JApplet中的面板),所有顶点都显示在另一个顶点的顶部。
其他人遇到了这个问题(JGraph Layout Does Not Work),我尝试了那里提供的解决方案,但只显示了两个节点。基本上,我只希望以某种方式显示图形,其中节点彼此独立。
有些代码胜过千言万语,所以这是我目前所拥有的,它只显示了两个节点(图中有219个节点):
class ourGraphVisualizer extends JApplet
{
private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF");
private static final Dimension DEFAULT_SIZE = new Dimension(1280, 1024);
// this init overrides the JApplet.init(). Our class here extends JApplet so we can do the visualization
public void init(ListenableDirectedWeightedGraph<String, DefaultWeightedEdge> theGraph)
{
JGraphModelAdapter<String, DefaultWeightedEdge> jgAdapter;
JPanel panel = new JPanel();
panel.setPreferredSize(DEFAULT_SIZE);
JScrollPane scrollpane = new JScrollPane(panel);
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(scrollpane, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.add(this);
frame.setTitle("Call Graph, " + theGraph.vertexSet().size() + "nodes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setPreferredSize(DEFAULT_SIZE);
jgAdapter = new JGraphModelAdapter<String, DefaultWeightedEdge>(theGraph);
JGraph jgraph = new JGraph(jgAdapter);
panel.add(jgraph);
resize(DEFAULT_SIZE);
// Let's see if we can lay it out
JGraphFacade jgf = new JGraphFacade(jgraph);
JGraphFastOrganicLayout layoutifier = new JGraphFastOrganicLayout();
layoutifier.run(jgf);
System.out.println("Layout complete");
final Map nestedMap = jgf.createNestedMap(true, true);
jgraph.getGraphLayoutCache().edit(nestedMap);
jgraph.getGraphLayoutCache().update();
jgraph.refresh();
frame.setVisible(true);
panel.setVisible(true);
scrollpane.setVisible(true);
}任何有建设性的建议/帮助/灵感都将不胜感激!
谢谢..。
-Eric
发布于 2013-03-13 14:00:11
如果你想避免顶点重叠,只需尝试不同的图形布局,我在这里给出了一些分层布局的图形布局,并调用run方法
final JGraphHierarchicalLayout hir = new JGraphHierarchicalLayout();
final JGraphFacade graphFacade = new JGraphFacade(jgraph);
hir.run(graphFacade);
final Map nestedMap = graphFacade.createNestedMap(true, true);
jgraph.getGraphLayoutCache().edit(nestedMap);发布于 2012-06-21 20:37:54
最好创建两个JPanels,然后使用适当的布局管理器将图形分别添加到JPanels中,并将JPanels添加到JFrame中
https://stackoverflow.com/questions/11124793
复制相似问题