University of Texas at El Paso
Banner
Compuer Science
   
PixelReader.java Minimize    

import java.io.*;                             // for java.io.File
import java.util.*;                           // for java.util.Scanner 

public class PixelReader {
  public static void main (String[] args){
    JRaster pic = new JRaster();
    try{                                               // IO can have errors
      File dotFile = new File("house.txt");           // open file 
      System.out.println("file opened!");
      Scanner dotFileScanner = new Scanner(dotFile);   // attach "Scanner" to file
      while( dotFileScanner.hasNext()){                // more to read? (not at EOF)
        String line = dotFileScanner.nextLine();       // read through end-of-line
        Scanner lineScanner = new Scanner(line);       // make scanner to read the line
        int x = (int)lineScanner.nextDouble();         // read x, y 
        int y = (int)lineScanner .nextDouble();
        String color = lineScanner.next();             // read color as a token in whitespace
        System.out.println(x + " " + y + " (" + color + ")");
        pic.set(x,y, stringToColor(color));            // draw dot
      }
      dotFileScanner.close();                          // done scanning file  
    }catch (Exception e){                              // on error...
      System.out.println(e);                           //   print the exception
    }
  }
  public static int stringToColor(String color) {
    int c;
    color = color.toLowerCase();
    if(color.equals("red"))
      c = JRaster.red;
    else if (color.equals("green"))
      c = JRaster.green;
    else if (color.equals("blue"))
      c = JRaster.blue;
    else if (color.equals("black"))
      c = JRaster.black;
    else if (color.equals("cyan"))
      c = JRaster.cyan;
    else if (color.equals("magenta"))
      c = JRaster.magenta;
    else if (color.equals("yellow"))
      c = JRaster.yellow;
    else if (color.equals("purple"))
      c = JRaster.purple;
    else if (color.equals("gray"))
      c = JRaster.gray;
    else if (color.equals("silver"))
      c = JRaster.silver;
    else if (color.equals("orange"))
      c = JRaster.orange;
    else if (color.equals("darkyellow"))
      c = JRaster.darkYellow;
    else if (color.equals("verydarkyellow"))
      c = JRaster.veryDarkYellow;
    else
      c = JRaster.white;
    return(c);
  } 
}