// the original source of ReadFile.java // that reads a text file line by line and outputs to screen is at // // http://www.abbeyworkshop.com/howto/java/readFile/ReadFile.java.html // // I modified it // + to check for proper format of the primitives specification // + to store the information in a vector of vectors // each vector stores one primitive (one line) // My specification is: // + line (x1,y1) (x2,y2) // + rectangle (x1,y1) (x2,y2) // + ellipse (x,y) a,b // + circle (x,y) r // // I use this by habit (I got used to!) -- nothing special in here // // Also, instead of exit whenever there is an error, I skip the line // import java.util.*; import java.io.*; import java.lang.*; public class ReadFile { // set to true for output of intermediate computations final static boolean trace = false; // print content of a vector public static void printVector(Vector v) { System.out.println(v); } // this is the switch for drawing public static void myDraw(Vector v) { System.out.println("Start drawing ... "); Enumeration ecomp = v.elements(); int ptype; for ( ; ecomp.hasMoreElements(); ) { Vector comp = (Vector) ecomp.nextElement(); printVector(comp); ptype = new Integer(comp.firstElement().toString()); switch (ptype) { case 1: // line System.out.println("LINE "); break; case 2: // rectangle System.out.println("Rectangle "); break; case 12: // filled rectangle System.out.println("filled rectangle "); break; case 3: // ellipse System.out.println("Ellipse "); break; case 13: // filled ellipse System.out.println("Filled ellipse "); break; case 4: // circle System.out.println("Circle "); break; case 14: // filled circle System.out.println("Filled circle "); break; case 5: // polygon (open) System.out.println("Open polygon "); break; case 6: // polygon (closed) System.out.println("Closed polygon "); break; case 16: // filled polygon System.out.println("Filled polygon "); break; } } } // checking for proper format of a line public static boolean typeChecking(Vector v) { // 1 - Line, 2 - Rectangle, 3 - Ellipse, // 4 - Circle, 5 - Polygon (Open), // 6 - Polygon (Closed), 12 - Filled rectangle, // 13 - Filled ellipse, 14 - Filled circle, 16 - Filled Polygon int ptype = new Integer(v.firstElement().toString()); int vsize = v.size(); // size of vector int xy, i; int x, y, a, b; switch (ptype) { case 1: // line if (vsize != 5) { System.out.println("Incorrect line specification"); return false; } break; case 2: // rectangle case 12: // filled rectangle if (vsize != 5) { System.out.println("Incorrect rectangle specification"); return false; } break; case 3: // ellipse case 13: // filled ellipse if (vsize != 5) return false; x = new Integer(v.elementAt(1).toString()); y = new Integer(v.elementAt(2).toString()); a = new Integer(v.elementAt(3).toString()); b = new Integer(v.elementAt(4).toString()); if (x + a > 800 || y+b > 800 || x - a < 0 || y - b < 0) { System.out.println("Incorrect ellipse specification"); return false; } break; case 4: // circle case 14: // filled circle if (vsize != 4) return false; x = new Integer(v.elementAt(1).toString()); y = new Integer(v.elementAt(2).toString()); a = new Integer(v.elementAt(3).toString()); if (x + a > 800 || x - a < 0 || y + a > 800 || y - a < 0) { System.out.println("Incorrect circle specification"); return false; } break; case 5: // polygon (open) case 6: // polygon (closed) case 16: // filled polygon // polygon should have at least three points if (vsize % 2 == 0 || vsize < 7) { System.out.println("Incorrect polygon specification"); return false; } break; default: System.out.println("Unknown specification"); return false; } return true; } // parse the line and put the type and coordinates into a vector public static Vector createToken(String line) { StringTokenizer st = new StringTokenizer(line, " ,"); Vector v = new Vector(); while(st.hasMoreTokens()){ String key = st.nextToken(); Integer ikey; // process the value // should have catch the format error // and out of bound error try { ikey = new Integer(key); } catch (NumberFormatException e) { System.out.println(" NumberFormatException "); return null; } if (ikey > 800 || ikey < 0) { System.out.println(" Out of bound error "); return null; } // add to the vector v.addElement(ikey); } // need to check for properness of each primitive type boolean res = typeChecking(v); if (res == false) return null; return v; } // read the file and process it to create a vector of vectors // it is assumed that the line contains the type of the primitives // and coordinates of its vertices, separated by comma // space can be included public static void main (String args[ ]) { System.out.println("Test "+(-2)/2+" modulo "+(-2)%2); Vector fin = new Vector(); try { FileReader input = new FileReader(args[0]); BufferedReader bufRead = new BufferedReader(input); String line; // String that holds current file line int count = 0; // Line number of count // Read first line line = bufRead.readLine(); count++; // Read through file one line at time. Print line # and line while (line != null){ if (trace) System.out.println(count+": "+line); // create a vector for all the token Vector v = createToken(line); // print the vector if (trace) printVector(v); // add to the two dimention vector if (v == null) { System.out.println("Not acceptable : "+line); // could stop here } else { fin.add(v); } // get the next line line = bufRead.readLine(); count++; } bufRead.close(); // print out the vector of vectors if (trace) System.out.println(fin); // start drawing myDraw(fin); } catch (ArrayIndexOutOfBoundsException e){ /* If no file was passed on the command line, this expception is generated. A message indicating how to the class should be called is displayed */ System.out.println("Usage: java ReadFile filename\n"); } catch (IOException e){ // If another exception is generated, print a stack trace e.printStackTrace(); } } }