/* GridInt * Lab 7 * April 3, 2008 * Charles H C Meyer * CompSci 210 */ /* * A variation on the GridInt class that stores floats instead of ints. * Almost identical to the GridInt class, except for a couple lines that are * changed from int to float. See the GridInt class for better comenting and * details. The only problem with this program is that as of right now, it * refuses to render correcty. Everything comes out as out of the min-max range. * Could probally be debugged with bit of effort, but as of right now it * is 1 AM and I still have to coment and put a header on the GridGIS class, as * well as carefully test everything. Thus, maybe another time I will fix it. */ import java.util.*; import java.io.*; import java.lang.*; import java.awt.*; import javax.swing.*; public class GridFloat extends JFrame{ private int ncols; private int nrows; private float cellSize; private int noDataValue; private float[][] theGrid; private boolean renderGrid; //weather or not to paint the grid private int colorScheme; //the color scheme in use private int windowWidth; private int windowHeight; private float xStretch; private float yStretch; private int maxHeight; private int minHeight; private static final int MAX_HEIGHT_DEFAULT = 200; private static final int MIN_HEIGHT_DEFAULT = 0; private static final int COLOR_SCHEME_DEFAULT = 2; public GridFloat (String fileName) { super("yeah"); //sets up some defualts renderGrid = false; maxHeight = MAX_HEIGHT_DEFAULT; minHeight = MIN_HEIGHT_DEFAULT; colorScheme = COLOR_SCHEME_DEFAULT; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(false); File f = new File(fileName); Scanner sc = null; try { sc = new Scanner(f); } catch (FileNotFoundException e) { System.err.println("File not found!"); System.exit(0); } setHeader(sc); windowWidth = ncols; windowHeight = nrows; xStretch = 1; yStretch = 1; setSize(windowWidth,windowHeight); theGrid = new float[nrows][ncols]; int j = 0; int k = 0; while (j < nrows) { theGrid[j][k] = sc.nextFloat(); k++; if ( k == ncols) { k = 0; j++; } } //a piece usefull for debugging, prints the array, although it is ill adivsed //to use it on large terrains /* for (int i = 0; i < theGrid.length; i++) { for (int m = 0; m < theGrid[i].length; m++) { System.out.print(theGrid[i][m]); } System.out.println(); } */ } private void setHeader (Scanner sc) { boolean hasNCols = false; boolean hasNRows = false; boolean hasCellSize = false; boolean hasNoDataValue = false; //scans through the header, asigning values where they are needed while (!sc.hasNextFloat()) { String theLine = sc.next(); if (theLine.equals("ncols")) { if (hasNCols == false) { ncols = sc.nextInt(); hasNCols = true; } else { System.err.println("error with header ncols"); } } else if (theLine.equals("nrows")) { if (hasNRows == false) { nrows = sc.nextInt(); hasNRows = true; } else { System.err.println("error with header nrows"); } } else if (theLine.equals("cellsize")) { if (hasCellSize == false) { cellSize = sc.nextFloat(); hasCellSize = true; } else { System.err.println("error with header cellsize"); } } else if (theLine.equals("NODATA_value")) { if (hasNoDataValue == false) { noDataValue = sc.nextInt(); hasNoDataValue = true; } else { System.err.println("error with header NODATA_value"); } } else { sc.next(); } } if (!(hasNCols && hasNRows && hasCellSize && hasNoDataValue)) { System.err.println("error with header insuficient data"); } } public void setWindow(int newWindowWidth, int newWindowHeight) { windowWidth = newWindowWidth; windowHeight = newWindowHeight; setSize(windowWidth,windowHeight); xStretch = windowWidth/ncols; yStretch = windowHeight/nrows; repaint(); } public void setMinHeight(int newHeight) { minHeight = newHeight; repaint(); } public void setMaxHeight(int newHeight) { maxHeight = newHeight; repaint(); } public void setColorScheme(int newScheme) { colorScheme = newScheme; repaint(); } public void paint(Graphics window) { super.paint(window); //goes through the array, drawing lines for all but the far most right coloumn and //the bottom row if (theGrid != null && renderGrid) { for (int j = 0; j < theGrid.length-1; j++) { for (int k = 0; k < theGrid[j].length-1; k++) { //System.out.println(k + " " + j); window.setColor(getColor((theGrid[j][k] + theGrid[j][k+1])/2)); window.drawLine((int)(k*xStretch),(int)(j*yStretch), (int)((k+1)*xStretch),(int)(j*yStretch)); window.setColor(getColor((theGrid[j][k] + theGrid[j+1][k+1])/2)); window.drawLine((int)(k*xStretch),(int)(j*yStretch), (int)((k+1)*xStretch),(int)((j+1)*yStretch)); window.setColor(getColor((theGrid[j][k] + theGrid[j+1][k])/2)); window.drawLine((int)(k*xStretch),(int)(j*yStretch), (int)(k*xStretch),(int)((j+1)*yStretch)); } } } } private Color getColor(float height) { if (colorScheme == 1) { return getColor1(height); } else if (colorScheme == 2) { return getColor2(height); } else if (colorScheme == 3) { return getColor3(height); } else { System.err.println("Error with color scheme choice, defaulting to " + COLOR_SCHEME_DEFAULT); colorScheme = COLOR_SCHEME_DEFAULT; } return getColor2(height); } private Color getColor1(float height) { float numColor = (height-minHeight)/(maxHeight-minHeight); if (numColor > 1) { numColor = 1; } if (numColor < 0) { numColor = 0; } Color toReturn = new Color(numColor,numColor,numColor); return toReturn; } private Color getColor2(float height) { //the components of the color, given a value to make java compile float ColorR = 0; float ColorG = 0; float ColorB = 0; if (height == noDataValue) { ColorR = 1; ColorG = 1; ColorB = 1; } else if (height < minHeight) { ColorR = 1; ColorG = 0; ColorB = 0; } else if (height <= (maxHeight+minHeight)/2) { ColorR = 1 - (height-minHeight)*2/(maxHeight - minHeight); ColorG = (height-minHeight)*2/(maxHeight - minHeight); ColorB = 0; } else if (height <= maxHeight) { ColorR = 0; ColorG = (height-minHeight)*2/(maxHeight - minHeight)-1; ColorB = 2 - (height-minHeight)*2/(maxHeight - minHeight); } else if (height >maxHeight) { ColorR = 0; ColorG = 0; ColorB = 1; } else { //should never happen, indicates a problem with the code assert false; } //debuggin line, prints every color //System.out.println("h" + height + " r " + ColorR + " G " + ColorG +" B " + ColorB); Color toReturn = new Color(ColorR, ColorG, ColorB); return toReturn; } private Color getColor3(float height) { float ColorR = 0; float ColorG = 0; float ColorB = 0; if (height == noDataValue) { ColorR = 1; ColorG = 1; ColorB = 1; } else if (height < minHeight) { ColorR = 0; ColorG = 0; ColorB = 0; } else if (height <= (maxHeight+minHeight*5)/6) { ColorR = 6*(height-minHeight)/(maxHeight-minHeight); ColorG = 0; ColorB = 0; } else if (height <= (maxHeight+minHeight*2)/3) { ColorR = 2-6*(height-minHeight)/(maxHeight-minHeight); ColorG = 0; ColorB = 0; } else if (height <= (maxHeight+minHeight)/2) { ColorR = 0; ColorG = 6*(height-minHeight)/(maxHeight-minHeight) - 2; ColorB = 0; } else if (height <= (maxHeight*2+minHeight)/3) { ColorR = 0; ColorG = 4 - 6*(height-minHeight)/(maxHeight-minHeight); ColorB = 0; } else if (height <= (maxHeight*5+minHeight)/6) { ColorR = 0; ColorG = 0; ColorB = 6*(height-minHeight)/(maxHeight-minHeight) - 4; } else if (height <= maxHeight) { ColorR = 0; ColorG = 0; ColorB = 6 - 6*(height-minHeight)/(maxHeight-minHeight); } else if (height >maxHeight) { ColorR = 0; ColorG = 0; ColorB = 0; } //de buggin line, prints every color //System.out.println("h" + height + " r " + ColorR + " G " + ColorG +" B " + ColorB); Color toReturn = new Color(ColorR, ColorG, ColorB); return toReturn; } public static void main(String[] args) { GridFloat theGrid = new GridFloat (args[0]); theGrid.renderGrid(); } public void renderGrid () { renderGrid = true; setVisible(true); } }