Skip to content

Instantly share code, notes, and snippets.

@satyagraha
Created November 13, 2014 08:59
Show Gist options
  • Save satyagraha/d5d7534a6116c592efe4 to your computer and use it in GitHub Desktop.
Save satyagraha/d5d7534a6116c592efe4 to your computer and use it in GitHub Desktop.
HelloWorld5
package org.mydemo.jgraphx;
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
public class HelloWorld5 extends JFrame {
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld5(int choice) {
mxGraph graph = new mxGraph();
mxStylesheet stylesheet = graph.getStylesheet();
Map<String, Object> edgeStyle = new HashMap<>(stylesheet.getDefaultEdgeStyle());
edgeStyle.put(mxConstants.STYLE_STARTARROW, mxConstants.ARROW_CLASSIC);
edgeStyle.put(mxConstants.STYLE_ENDARROW, mxConstants.NONE);
stylesheet.setDefaultEdgeStyle(edgeStyle);
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object v1 = graph.insertVertex(parent, null, "node1", 0, 0, 80, 30);
Object v2 = graph.insertVertex(parent, null, "node2", 0, 0, 80, 30);
Object v3 = graph.insertVertex(parent, null, "node3", 0, 0, 80, 30);
Object v4 = graph.insertVertex(parent, null, "node4", 0, 0, 80, 30);
Object v5 = graph.insertVertex(parent, null, "node5", 0, 0, 80, 30);
graph.insertEdge(parent, null, null, v1, v2);
graph.insertEdge(parent, null, null, v1, v3);
graph.insertEdge(parent, null, null, v2, v4);
graph.insertEdge(parent, null, null, v3, v5);
graph.insertEdge(parent, null, null, v3, v4, mxConstants.STYLE_STROKECOLOR + "=red");
graph.insertEdge(parent, null, null, v2, v5, mxConstants.STYLE_STROKECOLOR + "=blue");
} finally {
graph.getModel().endUpdate();
}
// define layout
graph.setAllowNegativeCoordinates(false);
final mxIGraphLayout layout;
switch (choice) {
case 0:
layout = new mxHierarchicalLayout(graph, SwingConstants.WEST); // works ok
break;
case 1:
layout = new mxHierarchicalLayout(graph, SwingConstants.EAST); // broken
break;
case 2:
layout = new AllDirectionsHierarchicalLayout(graph, SwingConstants.EAST); // partially broken
break;
case 3:
layout = new mxHierarchicalLayout(graph, SwingConstants.NORTH); // works ok
break;
case 4:
layout = new mxHierarchicalLayout(graph, SwingConstants.SOUTH); // broken
break;
case 5:
layout = new AllDirectionsHierarchicalLayout(graph, SwingConstants.SOUTH); // partially broken
break;
default:
layout = null;
}
layout.execute(graph.getDefaultParent());
JFrame f = new JFrame();
f.setSize(800, 800);
f.setLocation(30, 20);
mxGraphComponent graphComponent = new mxGraphComponent(graph);
f.getContentPane().add(BorderLayout.CENTER, graphComponent);
f.setVisible(true);
}
public static void main(String[] args) {
new HelloWorld5(Integer.parseInt(args[0]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment