如何在JGraphX中自动调用有机布局

时间:2015-10-09 13:37:33

标签: java jgraphx graph-layout

我正在使用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);

由于mxGraphComponent不将mxFastOrganicLayout作为构造函数,但mxFastOrganicLayout需要mxGraph,这似乎是正确的步骤?对于任何帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

这是您应用单一布局的方式:

    // 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();
    }