Exemple #1
0
  public void generateRandom(String type, int size) {
    this.type = type;
    this.size = size;

    graph.getModel().beginUpdate();
    try {
      Object[] nodes = new Object[size];

      for (int i = 0; i < size; i++) {
        nodes[i] = graph.insertVertex(parent, null, Integer.toString(i), 0, 0, 40, 20);
      }

      if (type.equals("complete")) {
        for (int i = 0; i < size; i++) {
          for (int j = i + 1; j < size; j++) {
            graph.insertEdge(parent, null, "", nodes[i], nodes[j]);
          }
        }
      } else {
        if (type.equals("binary")) {
          int limit = size;
          for (int i = 0; i < size; i++) {
            if (i * 2 + 1 < size) graph.insertEdge(parent, null, "", nodes[i], nodes[i * 2 + 1]);
            if (i * 2 + 2 < size) graph.insertEdge(parent, null, "", nodes[i], nodes[i * 2 + 2]);
          }
        } else {
          if (type.equals("circuit")) {
            for (int i = 0; i < size - 1; i++) {
              graph.insertEdge(parent, null, "", nodes[i], nodes[i + 1]);
            }
            graph.insertEdge(parent, null, "", nodes[size - 1], nodes[0]);
          }
        }
      }

      mxIGraphLayout layout = new mxFastOrganicLayout(graph);
      if (this.layout.equals("circular")) {
        layout = new mxCircleLayout(graph);
      } else {
        if (this.layout.equals("hierarchical")) {
          layout = new mxHierarchicalLayout(graph);
        } else {
          if (this.layout.equals("orthogonal")) {
            layout = new mxOrthogonalLayout(graph);
          } else {
            if (this.layout.equals("tree")) {
              layout = new mxCompactTreeLayout(graph);
            } else {
              if (this.layout.equals("random")) {
                layout = new mxFastOrganicLayout(graph);
              }
            }
          }
        }
      }
      layout.execute(parent);
    } finally {
      graph.getModel().endUpdate();
    }
  }
Exemple #2
0
 public void generateGraph(String body) {
   String[] elems = body.split("\n");
   for (int i = 0; i < elems.length; i++) {
     elems[i] = elems[i].trim();
   }
   size = 0;
   int edgesIndex = 0;
   while (!elems[edgesIndex].equals("#")) {
     if (elems[edgesIndex].length() > 0) {
       size++;
     }
     edgesIndex++;
   }
   graph.getModel().beginUpdate();
   try {
     Object[] nodes = new Object[size];
     edgesIndex = 0;
     int verticesIndex = 0;
     while (!elems[edgesIndex].equals("#")) {
       if (elems[edgesIndex].length() > 0) {
         nodes[verticesIndex] = graph.insertVertex(parent, null, elems[edgesIndex], 0, 0, 40, 20);
         verticesIndex++;
       }
       edgesIndex++;
     }
     for (int i = edgesIndex + 1; i < elems.length; i++) {
       if (elems[i].length() > 0) {
         int number1 = Integer.parseInt(elems[i].split(" ")[0]);
         int number2 = Integer.parseInt(elems[i].split(" ")[1]);
         graph.insertEdge(parent, null, "", nodes[number1 - 1], nodes[number2 - 1]);
       }
     }
     // aranjam nodurile in functie de layout
     mxIGraphLayout layout = new mxFastOrganicLayout(graph);
     if (this.layout.equals("circular")) {
       layout = new mxCircleLayout(graph);
     } else {
       if (this.layout.equals("hierarchical")) {
         layout = new mxHierarchicalLayout(graph);
       } else {
         if (this.layout.equals("orthogonal")) {
           layout = new mxOrthogonalLayout(graph);
         } else {
           if (this.layout.equals("tree")) {
             layout = new mxCompactTreeLayout(graph);
           } else {
             if (this.layout.equals("random")) {
               layout = new mxFastOrganicLayout(graph);
             }
           }
         }
       }
     }
     layout.execute(parent);
   } finally {
     graph.getModel().endUpdate();
   }
 }