我正在使用 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
,这似乎是正确的步骤?我将不胜感激任何帮助,问候。
最佳答案
这是应用单一布局的方法:
// 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();
}
关于java - 如何在 JGraphX 中自动调用有机布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33039739/