import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; import java.awt.event.*; import java.io.*; /* Name of Lab: Terrain- Grid Class Your Name: Jennette Shepard Your Class: Comp Sci 210 Date: 4/3/08 This class reads the grid from the file and puts the values into an array that will be sent to the GridGIS class to be rendered. *I would also like to credit Professor Laura Toma because much of the *code I used was looked at from the class BoggleGUI */ public class Grid{ //variable to track the line //numbers of the file and the //text of each line private int line = 0; private String lineText; //number of rows and columns for the grid public static int numCols = 0; public static int numRows = 0; //NODATA_value variable public static int noDataValue = 0; //variable to track how many numbers //have been added to the array private int intNum = 0; //array to hold elevations values public static Integer[][] numsFromFile = new Integer[numRows + 1][numCols + 1]; private static final int HEADER_LENGTH = 6; //reads values from file and adds them to array //accounts for header at top public Grid(String fileName){ //grid = new Integer[][]; //error opening file boolean EOF = false; System.err.print("Reading word list from file " + fileName + "..."); //Opens file try{ LineNumberReader input = new LineNumberReader(new FileReader(fileName)); //reads lines whilst there are lines to read while((lineText = input.readLine()) != null){ //keeps track of ze lines! line = input.getLineNumber(); //if the line you are looking at is //in the header portion if(line <= HEADER_LENGTH){ //if you have found the line //containing the column number if (lineText.startsWith("ncols")){ //remove everything after the prefix //of the line lineText = lineText.substring(lineText.indexOf('s') +1, lineText.length()); //remove the extra spaces on //the rest of the line lineText = lineText.trim(); //create a scanner and search //remaining text for an int Scanner s = new Scanner(lineText); if(s.hasNextInt()){ //if int is found, count //it as the number of columns numCols = s.nextInt(); }else{ //else let user know number of columns //was untraceable System.err.println("number of columns" + " cannot be found"); } } //if you have found the line //containing the row number if (lineText.startsWith("nrows")){ //remove everything after the prefix //of the line lineText = lineText.substring(lineText.indexOf('s')+1, lineText.length()); //remove the extra spaces on //the rest of the line lineText = lineText.trim(); //create a scanner and search //remaining text for the int Scanner s = new Scanner(lineText); if(s.hasNextInt()){ //if int is found, count //it as the number of rows numRows = s.nextInt(); }else{ //else let user know number of rows //was untraceable System.err.println("number of rows" + " cannot be found"); } } //if you have found the line //containing the NODATA_value number if (lineText.startsWith("NODATA_value")){ //remove everything after the prefix //of the line lineText = lineText.substring(lineText.indexOf('e')+1, lineText.length()); //remove the extra spaces on //the rest of the line lineText = lineText.trim(); //create a scanner and search //remaining text for an int Scanner s = new Scanner(lineText); if(s.hasNextInt()){ //if int is found, count //it as the number of NODATA_value noDataValue = s.nextInt(); }else{ //else let user know number of NODATA_value //was untraceable System.err.println("NODATA_value" + " cannot be found"); } } } //if you have searched through //the entire header if(line == HEADER_LENGTH){ //create array with found values //to hold the elevations numsFromFile = new Integer[numRows + 1][numCols + 1]; } //if you are searching past //the header of the fild if(line > HEADER_LENGTH){ //create a scanner to search the lines Scanner s = new Scanner(lineText); //while there is an int // to be looked at while(s.hasNextInt()){ //add the int to the array numsFromFile[line-7][intNum%numCols] = s.nextInt(); //increase the counter //of ints that have //been located intNum++; } } } //ensures the array was filled for(int j = 0; j< (numRows); j++){ for(int i = 0; i< (numCols); i++){ if(numsFromFile[j][i]== null){ System.err.println("data received was incomplete"); } } } //Closes the file input.close(); }catch (FileNotFoundException fnfex ){ JOptionPane.showMessageDialog(null, "Unable to find elevations file " + fileName, "Error", JOptionPane.ERROR_MESSAGE); }catch(IOException e){ JOptionPane.showMessageDialog(null, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE); } System.err.println("done"); } }//end