public void loadTree() {
   System.out.println("Loading tree");
   StreamTokenizer stream = null;
   try {
     FileInputStream f = new FileInputStream(tree);
     Reader input = new BufferedReader(new InputStreamReader(f));
     stream = new StreamTokenizer(input);
     stream.resetSyntax();
     stream.wordChars(32, 127);
   } catch (Exception e) {
     System.out.println("Error opening " + tree);
     System.exit(1);
   }
   list = new ArrayList();
   try {
     // read the file to the end
     while (stream.nextToken() != StreamTokenizer.TT_EOF) {
       // is a word being read
       if (stream.ttype == StreamTokenizer.TT_WORD) {
         list.add(new String(stream.sval));
       }
       // is a number being read
       if (stream.ttype == StreamTokenizer.TT_NUMBER) {
         list.add(new Double(stream.nval));
       }
     }
   } catch (Exception e) {
     System.out.println("\nError reading " + tree + ". Exiting...");
     System.exit(1);
   }
 }
  public String[] parseTokens(String line) throws IOException {
    List tokens = new ArrayList();

    /*StringTokenizer st = new StringTokenizer(line);
    String token;
    while((token = st.nextToken()) != null) {
        tokens.add(token);
    } */

    StreamTokenizer st = new StreamTokenizer(new StringReader(line));
    st.parseNumbers();
    st.wordChars('_', '_'); // A word can be THIS_IS_A_WORD

    int token = st.nextToken();
    while (token != StreamTokenizer.TT_EOF) {
      String element = null;
      switch (token) {
        case StreamTokenizer.TT_NUMBER:
          element = String.valueOf(st.nval);
          break;
        case StreamTokenizer.TT_WORD:
          element = st.sval;
          break;
        case '"':
        case '\'':
          element = st.sval;
          break;
        case StreamTokenizer.TT_EOL:
          break;
        case StreamTokenizer.TT_EOF:
          break;
        default:
          element = String.valueOf((char) st.ttype);
          break;
      }
      if (element != null) tokens.add(element);
      token = st.nextToken();
    }

    String[] result = new String[tokens.size()];
    for (int index = 0; index < tokens.size(); index++) result[index] = (String) tokens.get(index);
    return result;
  }
Exemple #3
0
  /**
   * Parses a text file and creates an Obj3d. The file format is as follows:
   *
   * <pre>
   *
   * # a simple kite made of two triangular polygons
   * # by foo bar, 27 Aug 2002
   * 2
   * t f CC99FF 12.1 2.4 3.5, 12.4 2.2 3.5, 3.2 5 7.4,
   * t t 336699 9.7 0 3.5, 2.2 2.2 3.5, 3.2 5 7.3,
   *
   * </pre>
   *
   * The above represents an Obj3d with two polygons, the second of which is double sided; each is a
   * differnt color; each is made up of 3 points. Both cast shadows. The first flag indicates if the
   * polygon casts a shadow. The second flag indicates if the polygon is double sided.
   */
  public Obj3d(StreamTokenizer st, ModelViewer modelViewer, boolean register)
      throws IOException, FileFormatException {

    // call to constructor must be first, but we do not
    // know how many polygons yet, hence the following
    this(modelViewer, register);

    // gobble EOL's due to comments
    while (st.nextToken() == StreamTokenizer.TT_EOL) {;
    }

    // how many polygons
    int num = (int) st.nval;

    // set num polygons
    this.setNumPolygons(num);

    // gobble up new line
    st.nextToken();

    // Tools3d.debugTokens(st); //tmp
    // System.exit(0);

    // read a line a data for each polygon
    for (int i = 0; i < num; i++) {

      // has shadow ?
      st.nextToken();
      boolean shadow = "t".equals(st.sval);

      // double sided ?
      st.nextToken();
      boolean doubleSided = "t".equals(st.sval);

      // color - a hex number
      Color color = null;
      st.nextToken();
      try {
        color = Color.decode("0x" + st.sval);
      } catch (NumberFormatException e) {
        throw new FileFormatException(
            "Unable to parse color: " + st.sval + ", polygon index: " + i);
      }

      if (color == null) {
        color = COLOR_DEFAULT;
      }

      // read point co-ords until we reach end of line
      Vector points = new Vector();
      while (st.nextToken() != StreamTokenizer.TT_EOL) {

        float[] p = new float[3];

        p[0] = (float) st.nval;
        st.nextToken();
        p[1] = (float) st.nval;
        st.nextToken();
        p[2] = (float) st.nval;

        // gobble up comma which seperates points
        // note there must be a comma after the last point
        if (st.nextToken() != ',') {
          throw new FileFormatException("Unable to parse co-ordinates; comma expected: " + st);
        }
        points.addElement(p);
      }

      // convert vector to an array
      float[][] vs = new float[points.size()][];
      for (int j = 0; j < points.size(); j++) {
        vs[j] = (float[]) points.elementAt(j);
      }

      // finally, add the polygon
      if (!shadow) {
        this.addPolygon(vs, color, doubleSided);
      } else {
        this.addPolygonWithShadow(vs, color, doubleSided);
      }
    }
  }