mxGraph保存图片

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(422500);
        f.setLocation(300200);
 
        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"1001008030);
            Object B = graph.insertVertex(parent, null"B"1001008030);
            Object C = graph.insertVertex(parent, null"C"1001008030);
            Object D = graph.insertVertex(parent, null"D"1001008030);
            Object E = graph.insertVertex(parent, null"E"1001008030);
            Object F = graph.insertVertex(parent, null"F"1001008030);
            Object G = graph.insertVertex(parent, null"G"1001008030);
            Object H = graph.insertVertex(parent, null"H"1001008030);
//            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, null1, 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.youkuaiyun.com/topics/390203940




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值