如何在 JGraphX 中自动调用有机布局
Posted
技术标签:
【中文标题】如何在 JGraphX 中自动调用有机布局【英文标题】:How to automatically invoke an organic layout in JGraphX 【发布时间】:2016-01-07 11:44:37 【问题描述】:我正在使用 JGraphX 库并尝试使用 mxFastOrganicLayout 执行自动布局:
import javax.swing.JFrame;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class FastOrganic extends JFrame
public static void main(String[] args)
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
30);
Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);
finally
graph.getModel().endUpdate();
mxGraphComponent graphComponent = new mxGraphComponent(graph);
FastOrganic frame = new FastOrganic();
frame.add(graphComponent);
frame.pack();
frame.setVisible(true);
我想了解如何应用快速有机布局,文档将其定义为mxFastOrganicLayout
。问题是我不能把它变成这样的组件:
mxGraphComponent graphComponent = new mxGraphComponent(graph);
as mxGraphComponent
不采用mxFastOrganicLayout
作为构造函数,但mxFastOrganicLayout
采用mxGraph
,这似乎是正确的一步?我将不胜感激任何帮助,问候。
【问题讨论】:
【参考方案1】:这是应用单个布局的方式:
// define layout
mxIGraphLayout layout = new mxFastOrganicLayout(graph);
// layout graph
layout.execute(graph.getDefaultParent());
这是在应用单个布局之前设置一些属性的方式:
// define layout
mxFastOrganicLayout layout = new mxFastOrganicLayout(graph);
// set some properties
layout.setForceConstant( 40); // the higher, the more separated
layout.setDisableEdgeStyle( false); // true transforms the edges and makes them direct lines
// layout graph
layout.execute(graph.getDefaultParent());
这是你使用变形的布局方式:
// define layout
mxIGraphLayout layout = new mxFastOrganicLayout(graph);
// layout using morphing
graph.getModel().beginUpdate();
try
layout.execute(graph.getDefaultParent());
finally
mxMorphing morph = new mxMorphing(graphComponent, 20, 1.2, 20);
morph.addListener(mxEvent.DONE, new mxIEventListener()
@Override
public void invoke(Object arg0, mxEventObject arg1)
graph.getModel().endUpdate();
// fitViewport();
);
morph.startAnimation();
【讨论】:
以上是关于如何在 JGraphX 中自动调用有机布局的主要内容,如果未能解决你的问题,请参考以下文章
Could not find artifact com.github.jgraph:jgraphx:jar:v3.9.3