Exemple #1
0
  // ====================================================================
  private void findShort() {
    // svårt att behålla linjefärgerna?
    int start, s**t;
    try { // read from station
      String str = from.getText();
      str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
      start = noderna.find(str).getNodeNo();
    } catch (NullPointerException npe) {
      route.setText(felTextStart + "\n");
      return;
    }
    try { // read to station
      String str = to.getText();
      str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
      s**t = noderna.find(str).getNodeNo();
    } catch (NullPointerException npe) {
      route.setText(felTextSlut + "\n");
      return;
    }

    double totWeight = 0;
    int totNodes = 0;
    route.setText("");
    karta.clearLayer(DrawGraph.Layer.OVERLAY);
    Iterator<BusEdge> it = grafen.shortestPath(start, s**t);
    while (it.hasNext()) {
      BusEdge e = it.next();
      route.append(makeText1(e) + "\n");
      totNodes++;
      totWeight += e.getWeight();
      // draw the shortest path
      BusStop from = noderna.find(e.from), to = noderna.find(e.to);
      karta.drawLine(
          from.xpos, from.ypos, to.xpos, to.ypos, Color.black, 4.0, DrawGraph.Layer.OVERLAY);
    }
    karta.repaint();
    route.append("Antal: " + totNodes + " totalvikt: " + totWeight + "\n");
    from.setText("");
    to.setText("");
  } // findShort
Exemple #2
0
 // ====================================================================
 private void findMinSpan() {
   route.setText("");
   Iterator<BusEdge> it = grafen.minimumSpanningTree();
   if (it != null && it.hasNext()) {
     double totWeight = 0;
     int totNodes = 0; // only for easier testing
     karta.clearLayer(DrawGraph.Layer.OVERLAY);
     while (it.hasNext()) {
       BusEdge be = it.next();
       totNodes++;
       totWeight += be.getWeight();
       route.append(makeText2(be) + "\n");
       // draw the MST
       BusStop from = noderna.find(be.from);
       BusStop to = noderna.find(be.to);
       karta.drawLine(
           from.xpos, from.ypos, to.xpos, to.ypos, Color.red, 2.5, DrawGraph.Layer.OVERLAY);
     }
     karta.repaint();
     route.append("Antal: " + totNodes + " totalvikt: " + totWeight + "\n");
   } else from.setText("Det fanns ej någon lösning");
   to.setText("");
   from.setText("");
 } //  findMinSpan