Exemplo n.º 1
0
 private String makeText2(BusEdge be) {
   return noderna.find(be.from).name
       + " till "
       + noderna.find(be.to).name
       + " tar "
       + be.getWeight()
       + " minuter";
 }
Exemplo n.º 2
0
  // experimental
  // ====================================================================
  // ====================================================================
  // ====================================================================
  private void readAndDrawBIGGraph(String file) {
    // behövs inte än
    // @TODO
    // hur rita flera linjer mellan 2 noder? (för flera linjer)
    // reading of the graph should be done in the graph itself
    // it should be possible to get an iterator over nodes and one over edges
    // read in all the stops and lines and draw the lmap
    Scanner indata = null;
    // insert into p-queue to get them sorted
    names = new PriorityQueue<String>();
    try {
      // Read stops and put them in the node-table
      // in order to give the user a list of possible stops
      // assume input file is correct
      indata = new Scanner(new File(file + "-stops.txt"), "ISO-8859"); //
      while (indata.hasNext()) {
        String hpl = indata.next().trim();
        int xco = indata.nextInt();
        int yco = indata.nextInt();
        noderna.add(new BusStop(hpl, xco, yco));
        names.add(hpl);
        // Draw
        // this is a fix: fixa att Kålltorp och Torp är samma hållplats
        if (hpl.equals("Torp")) {
          xco += 11;
          hpl = "   / Torp";
        }
        karta.drawString(hpl, xco, yco, DrawGraph.Layer.BASE);
      }
      indata.close();

      //  Read in the lines and add to the graph
      indata = new Scanner(new File(file + "-lines.txt"), "ISO-8859");
      grafen = new DirectedGraph<BusEdge>(noderna.noOfNodes());
      Color color =
          new Color((float) Math.random(), (float) Math.random(), (float) Math.random()); //
      String lineNo = "1"; //
      while (indata.hasNext()) { // assume lines are correct
        int from = noderna.find(indata.next()).getNodeNo();
        int to = noderna.find(indata.next()).getNodeNo();
        grafen.addEdge(new BusEdge(from, to, indata.nextInt(), lineNo));
        indata.nextLine(); // skip rest of line
        // Draw
        BusStop busFrom = noderna.find(from);
        BusStop busTo = noderna.find(to);
        karta.drawLine(
            busFrom.xpos, busFrom.ypos, busTo.xpos, busTo.ypos, color, 2.0f, DrawGraph.Layer.BASE);
      }
      indata.close();
    } catch (FileNotFoundException fnfe) {
      throw new RuntimeException(" Indata till busshållplatserna saknas");
    }
    karta.repaint();
  } // end readAndDrawBIGGraph
Exemplo n.º 3
0
 private String makeText1(BusEdge be) {
   return "Tag "
       + be.line
       + " "
       + frome
       + " "
       + noderna.find(be.from).name
       + " till "
       + noderna.find(be.to).name
       + " framme efter "
       + be.getWeight();
 }
Exemplo n.º 4
0
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == from) {
      String tmpFrom = from.getText().trim();
      String tmpTo = to.getText().trim();
      if (tmpFrom.equals("MST") || tmpFrom.equals("mst")) findMinSpan();
      else {
        try { // check if from exists
          // String str = from.getText();
          // fixa så man kan skippa att skriva första med stor bokstav
          tmpFrom = Character.toUpperCase(tmpFrom.charAt(0)) + tmpFrom.substring(1);
          int pos = noderna.findLeading(tmpFrom).getNodeNo();
          route.setText(introText + "\n");
          from.setText(noderna.find(pos).toString());
        } catch (NullPointerException npe) {
          route.setText(felTextStart + "\n");
          return;
        }
        if (!tmpTo.equals("")) {
          findShort();
        }
      }
    } else if (e.getSource() == to) {
      String tmpFrom = from.getText().trim();
      String tmpTo = to.getText().trim();
      if (tmpTo.equals("MST") || tmpTo.equals("mst")) findMinSpan();
      else {
        try { // check if to exists
          // String str = to.getText();
          tmpTo = Character.toUpperCase(tmpTo.charAt(0)) + tmpTo.substring(1);
          int pos = noderna.findLeading(tmpTo).getNodeNo();
          route.setText(introText + "\n");
          to.setText(noderna.find(pos).toString());
        } catch (NullPointerException npe) {
          route.setText(felTextStart + "\n");
          return;
        }
        if (!tmpFrom.equals("")) {
          findShort();
        }
      }

      /*
      else if ( ! from.getText().equals("") )
      	findShort();
      */
    }
  }
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
0
  // ====================================================================
  // ====================================================================
  private void readAndDrawGraph() {
    // @TODO
    // hur rita flera linjer mellan 2 noder? (för flera linjer)
    // reading of the graph should be done in the graph itself
    // it should be possible to get an iterator over nodes and one over edges
    // read in all the stops and lines and draw the lmap
    Scanner indata = null;
    // insert into p-queue to get them sorted
    names = new PriorityQueue<String>();
    try {
      // Read stops and put them in the node-table
      // in order to give the user a list of possible stops
      // assume input file is correct
      indata = new Scanner(new File("stops.noBOM.txt"), "UTF-8");
      while (indata.hasNext()) {
        String hpl = indata.next().trim();
        int xco = indata.nextInt();
        int yco = indata.nextInt();
        noderna.add(new BusStop(hpl, xco, yco));
        names.add(hpl);
        // Draw
        /*
        // Denna fix som slår ihop Kålltorp och Torp är förvirrande eftersom de är olika noder i grafen.
        // Tror man att det är samma nod blir resultatet förrvirrande.
        // Till nästa gång: Gör till samma nod med namnet Virginsgatan. Så är det i verkligheten nu.

        				// this is a fix: fixa att Kålltorp och Torp är samma hållplats
        				if ( hpl.equals("Torp") ) {
        					xco += 11;
        					hpl = "   / Torp";
        				}
        */
        karta.drawString(hpl, xco, yco, DrawGraph.Layer.BASE);
      }
      indata.close();

      //  Read in the lines and add to the graph
      indata = new Scanner(new File("lines.noBOM.txt"), "UTF-8");
      grafen = new DirectedGraph<BusEdge>(noderna.noOfNodes());
      while (indata.hasNext()) {
        String lineNo = indata.next();
        int antal = indata.nextInt() - 1;
        int from = noderna.find(indata.next()).getNodeNo();
        // hur rita flera linjer mellan 2 noder?
        // enkel inc fungerar inte
        // färgen kunde vara "äkta" dvs linjefärg
        Color color =
            new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
        for (int i = 0; i < antal; i++) {
          int to = noderna.find(indata.next()).getNodeNo();
          grafen.addEdge(new BusEdge(from, to, indata.nextInt(), lineNo));
          // Draw
          BusStop busFrom = noderna.find(from);
          BusStop busTo = noderna.find(to);
          karta.drawLine(
              busFrom.xpos,
              busFrom.ypos,
              busTo.xpos,
              busTo.ypos,
              color,
              2.0f,
              DrawGraph.Layer.BASE);
          from = to;
        }
      }
      indata.close();
    } catch (FileNotFoundException fnfe) {
      throw new RuntimeException(" Indata till busshållplatserna saknas");
    }
    karta.repaint();
  } // end readAndDrawGraph