import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.image.BufferedImage;
import
java.io.File;
import
javax.imageio.ImageIO;
import
javax.swing.JFrame;
import
com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import
com.mxgraph.swing.mxGraphComponent;
import
com.mxgraph.util.mxCellRenderer;
import
com.mxgraph.view.mxGraph;
public
class
AutoLayoutSample {
public
AutoLayoutSample() {
JFrame f =
new
JFrame();
f.setSize(
422
,
500
);
f.setLocation(
300
,
200
);
final
mxGraph graph =
new
mxGraph();
mxGraphComponent graphComponent =
new
mxGraphComponent(graph);
f.getContentPane().add(BorderLayout.CENTER, graphComponent);
f.setVisible(
true
);
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
Object A = graph.insertVertex(parent,
null
,
"A"
,
100
,
100
,
80
,
30
);
Object B = graph.insertVertex(parent,
null
,
"B"
,
100
,
100
,
80
,
30
);
Object C = graph.insertVertex(parent,
null
,
"C"
,
100
,
100
,
80
,
30
);
Object D = graph.insertVertex(parent,
null
,
"D"
,
100
,
100
,
80
,
30
);
Object E = graph.insertVertex(parent,
null
,
"E"
,
100
,
100
,
80
,
30
);
Object F = graph.insertVertex(parent,
null
,
"F"
,
100
,
100
,
80
,
30
);
Object G = graph.insertVertex(parent,
null
,
"G"
,
100
,
100
,
80
,
30
);
Object H = graph.insertVertex(parent,
null
,
"H"
,
100
,
100
,
80
,
30
);
// Object I = graph.insertVertex(parent, null, "I", 100, 100, 80, 30);
// Object J = graph.insertVertex(parent, null, "J", 100, 100, 80, 30);
graph.insertEdge(parent,
null
,
"A->B"
, A, B);
graph.insertEdge(parent,
null
,
"A->C"
, A, C);
graph.insertEdge(parent,
null
,
"A->D"
, A, D);
graph.insertEdge(parent,
null
,
"A->E"
, A, E);
graph.insertEdge(parent,
null
,
"B->F"
, B, F);
graph.insertEdge(parent,
null
,
"C->G"
, C, G);
graph.insertEdge(parent,
null
,
"F->H"
, F, H);
// graph.insertEdge(parent, null, "D->I", D, I);
// graph.insertEdge(parent, null, "E->J", E, J);
}
finally
{
graph.getModel().endUpdate();
}
// mxCellState state = graph.getView().getState( graph.getDefaultParent());
// state.getStyle().put( mxConstants.STYLE_STARTSIZE, 0);
//树形layout
mxHierarchicalLayout layout =
new
mxHierarchicalLayout(graph);
//layout.setFineTuning(true);
//layout.setInterHierarchySpacing(10);
//layout.setInterRankCellSpacing(10);
//layout.setIntraCellSpacing(100);
//layout.setParallelEdgeSpacing(10);
// 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();
graph.getModel().endUpdate();
}
//保存图片
saveAsImage(graphComponent, graph);
}
private
void
saveAsImage(mxGraphComponent graphComponent, mxGraph graph){
Color bg = graphComponent.getBackground();
BufferedImage image = mxCellRenderer.createBufferedImage(graph,
null
,
1
, bg, graphComponent.isAntiAlias(),
null
, graphComponent.getCanvas());
if
(image !=
null
){
try
{
File png =
new
File(
"D://test.png"
);
ImageIO.write(image,
"png"
, png);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
public
static
void
main(String[] args) {
new
AutoLayoutSample();
}
}
转自:http://bbs.csdn.net/topics/390203940