Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package Grafica;
  2.  
  3. import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
  4. import com.mxgraph.layout.mxCircleLayout;
  5. import com.mxgraph.layout.mxCompactTreeLayout;
  6. import com.mxgraph.layout.mxFastOrganicLayout;
  7. import com.mxgraph.layout.mxIGraphLayout;
  8. import com.mxgraph.layout.mxParallelEdgeLayout;
  9. import com.mxgraph.layout.mxPartitionLayout;
  10. import com.mxgraph.layout.mxStackLayout;
  11. import com.mxgraph.model.mxGeometry;
  12. import javax.swing.JFrame;
  13.  
  14. import com.mxgraph.swing.mxGraphComponent;
  15. import com.mxgraph.util.mxConstants;
  16. import com.mxgraph.util.mxPoint;
  17. import com.mxgraph.view.mxCellState;
  18. import com.mxgraph.view.mxEdgeStyle;
  19. import com.mxgraph.view.mxGraph;
  20. import com.mxgraph.view.mxGraphView;
  21. import com.mxgraph.view.mxStylesheet;
  22. import java.util.HashMap;
  23. import java.util.Hashtable;
  24. import java.util.Map;
  25. import javax.swing.JPanel;
  26. import javax.swing.SwingConstants;
  27.  
  28. public class ejemplo extends JPanel {
  29.  
  30.     /**
  31.      *
  32.      */
  33.     private static final long serialVersionUID = -2707712944901661771L;
  34.     public mxGraph graph;
  35.  
  36.     public ejemplo() {
  37.         super();
  38.  
  39.         mxGraph graph = new mxGraph();
  40. //{
  41. //  protected mxGraphView createGraphView()
  42. //  {
  43. //    return new mxGraphView(this)
  44. //    {
  45. //      public mxPoint getPoint(mxCellState state,
  46. //          mxGeometry geometry)
  47. //      {
  48. //        double x = state.getCenterX()+10;
  49. //        double y = state.getCenterY();
  50. //        if (state.getAbsolutePointCount() == 3)
  51. //        {
  52. //          mxPoint mid = state.getAbsolutePoint(1);
  53. //          x = mid.getX();
  54. //          y = mid.getY();
  55. //        }
  56. //
  57. //        return new mxPoint(x, y);
  58. //      }
  59. //    };
  60. //  }
  61. //};
  62.        
  63.         Object parent = graph.getDefaultParent();
  64.  
  65.         mxStylesheet stylesheet = graph.getStylesheet();
  66.        
  67.         Hashtable<String, Object> estiloEstado = new Hashtable<String, Object>();
  68.         estiloEstado.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_ELLIPSE);
  69.         estiloEstado.put(mxConstants.STYLE_FONTSIZE, 20);
  70.        
  71.         Hashtable<String, Object> estiloEFinal = new Hashtable<String, Object>();
  72.         estiloEFinal.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_DOUBLE_ELLIPSE);
  73.         estiloEFinal.put(mxConstants.STYLE_FONTSIZE, 20);
  74.  
  75.         Map<String, Object> edgeStyle = new HashMap<String, Object>();
  76.         edgeStyle.put(mxConstants.STYLE_SHAPE,    mxConstants.SHAPE_CONNECTOR);
  77.         edgeStyle.put(mxConstants.STYLE_ENDARROW, mxConstants.ARROW_CLASSIC);
  78.         edgeStyle.put(mxConstants.STYLE_STROKECOLOR, "#000000");
  79.         edgeStyle.put(mxConstants.STYLE_FONTCOLOR, "#000000");
  80.         stylesheet.setDefaultEdgeStyle(edgeStyle);
  81. //        Map<String, Object> EdgeStyle = graph.getStylesheet().getDefaultEdgeStyle();
  82. //EdgeStyle.put(mxConstants.STYLE_EDGE, mxEdgeStyle.OrthConnector);
  83. //EdgeStyle.put(mxConstants.STYLE_STROKECOLOR, "red");
  84. //EdgeStyle.put(mxConstants.STYLE_STROKEWIDTH, 2);
  85.        
  86.         stylesheet.putCellStyle("ESTADOFINAL", estiloEFinal);
  87.         stylesheet.putCellStyle("ESTADO", estiloEstado);
  88.         graph.setStylesheet(stylesheet);
  89.  
  90.         graph.getModel().beginUpdate();
  91.         graph.setCellsLocked(true);
  92.         graph.setVertexLabelsMovable(false);
  93.         graph.setEdgeLabelsMovable(false);
  94.  
  95.         try {
  96.             Object q0 = graph.insertVertex(parent, null, "", 100, 100, 50, 50, "opacity=0");
  97.             Object qA = graph.insertVertex(parent, null, "A", 100, 100, 50, 50, "ESTADOFINAL");
  98.             Object qB = graph.insertVertex(parent, null, "B", 100, 200, 50, 50, "ESTADO");
  99.             Object qC = graph.insertVertex(parent, null, "C", 300, 300, 50, 50, "ESTADO");
  100.             Object qD = graph.insertVertex(parent, null, "D", 400, 400, 50, 50, "ESTADO");
  101.             //Object v3 = graph.insertVertex(parent, null, "q2", 250, 200,50,50, "ESTADO");
  102.             graph.insertEdge(parent, null, "", q0, qA); //flechita inicial
  103.             graph.insertEdge(parent, null, "0", qA, qA,"rounded=1");
  104.             graph.insertEdge(parent, null, "1", qA, qB,"rounded=1");
  105.             graph.insertEdge(parent, null, "0", qB, qB,"rounded=1");
  106.             graph.insertEdge(parent, null, "1", qB, qA);
  107.             graph.insertEdge(parent, null, "1", qB, qC);
  108.             graph.insertEdge(parent, null, "1", qC, qD);
  109.  
  110. //mxParallelEdgeLayout layout = new mxParallelEdgeLayout(graph);
  111. //            mxIGraphLayout layout = new mxFastOrganicLayout(graph);
  112. //                mxCircleLayout layout = new mxCircleLayout(graph);
  113. //mxCompactTreeLayout  layout = new mxCompactTreeLayout(graph);
  114. //mxPartitionLayout layout = new mxPartitionLayout(graph, true, 50, 50);
  115. //mxParallelEdgeLayout layout2 = new mxParallelEdgeLayout(graph);
  116.             mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
  117.            
  118.            
  119.             layout.setInterRankCellSpacing(50.0);
  120.             layout.setIntraCellSpacing(50.0);
  121.            
  122. //           layout.setParallelEdgeSpacing(200.0);
  123. //                        
  124. //                        
  125.            
  126. layout.setDisableEdgeStyle(false);
  127. layout.execute(graph.getDefaultParent());
  128.            
  129.         } finally {
  130.             graph.getModel().endUpdate();
  131.         }
  132.  
  133.         this.add(new mxGraphComponent(graph));
  134.  
  135.     }
  136.  
  137.     public static void main(String[] args) {
  138.         JFrame frame = new JFrame();
  139.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140.         frame.setSize(600, 600);
  141.         ejemplo ej = new ejemplo();
  142.  
  143.         mxGraphComponent graphComponent = new mxGraphComponent(ej.graph);
  144.  
  145.         frame.add(graphComponent);
  146.  
  147.         frame.setVisible(true);
  148.  
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement