1

I'm using the JGraphX library and am trying to perform an automatic layout using the 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);
    }
}

I want to find out how to apply a fast organic layout, the documentation defines it as mxFastOrganicLayout. The problem is that I cannot turn it into a component like this :

mxGraphComponent graphComponent = new mxGraphComponent(graph);

as mxGraphComponent does not take mxFastOrganicLayout as a constructor, but mxFastOrganicLayout takes mxGraph, which seems to be the right step? I would be grateful for any help, regards.

1 Answers1

2

This is how you apply a single layout:

    // define layout
    mxIGraphLayout layout = new mxFastOrganicLayout(graph);

    // layout graph
    layout.execute(graph.getDefaultParent());

This is how you set some properties before you apply a single layout:

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

This is how you layout using morphing:

    // 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();
    }
Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55