import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; import java.lang.Math.*; /* Jack Morrison * April 3, 2008 * * Takes a .asc file of values, and renders it into * a 2D representation, using 5 color ranges to make it * more realistic. Also has contour lines of a sort. * */ public class Grid extends JFrame { // Array to store image data private int[][] data; // Header data private int cols,rows,cellSize,noData; private static final int HEADER_LINES=6; // Information from data private int highVal,lowVal; // Variables for drawing, colors // Static private static final double SCALE=.05; private static final Color DEFAULT_COLOR = Color.BLACK; // Instance private int avgElev; private Color paintColor; private Color LOW_COLOR,MID1_COLOR,MID2_COLOR,MID3_COLOR,HIGH_COLOR; private int lowValUpper,mid1Val,mid2Val,mid3Val,valSpread; // CONSTRUCTOR public Grid(String fileName) { // Makes a JFrame super("GIS Grid"); initGrid(fileName); // In case the image is too small, we make it bigger, // so that it is visible if (rows*cellSize<100 || cols*cellSize<100){ cellSize=10;} // Sets the size of the JFrame to be big enough to // accomodate the entire image setSize((int)(cols*cellSize),(int)(rows*cellSize)); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); repaint(); } // Make a new grid using the file passed to this method. public static void main (String[] args) { Grid set = new Grid(args[0]); } // PURPOSE: Initializes our grid array, and variables associated with it. // Scans through the file, copies to a data array, sets highVal, lowVal, // and color boundary regions for the file. private void initGrid(String fileName) { // Load File File f = new File(fileName); // Scanners and scanner variables String nextLine; int nextVal; Scanner lineScan = null; Scanner valScan = null; // Initialize high variable highVal = 0; // A giant number, so that the first number // is sure to be less than it. lowVal = 10000000; // Initialize our current line int currentLine = 1; // Create the line scanner try {lineScan = new Scanner(f);} catch(FileNotFoundException fnfex) {return;} // Scan through all the lines while (lineScan.hasNextLine()){ // Get then next line nextLine = lineScan.nextLine(); // We're starting at the first column int currentCol = 0; // Try to make a scanner for this line try {valScan = new Scanner(nextLine);} catch(IllegalArgumentException iaex) {return;} // Store the number of columns if (currentLine == 1 && valScan.next().equals("ncols")){ cols = valScan.nextInt(); } // Store the number of rows else if (currentLine == 2 && valScan.next().equals("nrows")) { rows = valScan.nextInt(); // Initialize the data array data = new int[rows][cols];} // We skip lines 3 and 4 of the header, because we have // no need for the data contained in them // Get the cell size from the header else if (currentLine == 5 && valScan.next().equals("cellsize")){ cellSize = (int)(valScan.nextFloat()*SCALE);} // Get the header value for NODATA else if (currentLine == 6){ valScan.next(); noData = (int)valScan.nextFloat(); } // When we leave the header, copy each new number from // the file into the data array. All numbers are cast as // ints to save some space, because the precision of floats // is not needed for a representation of this type. else if (currentLine>HEADER_LINES ) { while (valScan.hasNext()){ nextVal = (int)valScan.nextFloat(); data[currentLine-HEADER_LINES-1][currentCol] = nextVal; // Store new high or low values if (nextVal>highVal) highVal = nextVal; else if (nextVal=lowVal && avgElev<=lowValUpper){ LOW_COLOR= new Color(0,(int)(Math.abs(avgElev%40)+100),31); return LOW_COLOR;} if (avgElev>lowValUpper && avgElev<=mid1Val){ MID1_COLOR = new Color(0,(int)(Math.abs(avgElev%40)+160),48); return MID1_COLOR;} if (avgElev>mid1Val && avgElev<=mid2Val) { MID2_COLOR = new Color(162,(int)(Math.abs(avgElev%40)+211),80); return MID2_COLOR;} if (avgElev>mid2Val && avgElev<=mid3Val) { MID3_COLOR= new Color(211,(int)(Math.abs(avgElev%40)+179),80); return MID3_COLOR;} if (avgElev>mid3Val && avgElev<=highVal) { HIGH_COLOR= new Color(121,(int)(Math.abs(avgElev%40)+92),0); return HIGH_COLOR;} return DEFAULT_COLOR; } // PURPOSE: Does all the rendering! // Works by iterating through the entire data array and // painting three lines from each point: // 1 straight up (unless it is the top row) // 1 to the right (unless it is the rightmost row) // 1 diagonal up-right (unless you're in the top row, or the rightmost column) public void paint(Graphics g) { for (int i=0; i=1){ paintColor = checkColor(data[i][j],data[i-1][j]); g.setColor(paintColor); g.drawLine((int)j*cellSize,(int)i*cellSize, (int)j*cellSize,(int)(i-1)*cellSize); } } } } }