コード例 #1
0
ファイル: ShortRoute.java プロジェクト: IT-Kakan/TDA416
  // 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
コード例 #2
0
ファイル: ShortRoute.java プロジェクト: IT-Kakan/TDA416
  // ====================================================================
  // ====================================================================
  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