import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileInputStream; import java.util.Scanner; import java.util.StringTokenizer; /** Map.java - Using the input text file tract.txt, create an image showing * the basic geographical distribution of population among the 48 * contiguous states. But there is a mistake. Can you help me? :) */ public class Map { public static void main(String [] args) throws FileNotFoundException { Scanner in = new Scanner(new FileInputStream("tract.txt")); // Let's assume that the longitudes range from 65000 to 125000 // and the latitudes range from 24000 to 50000. // Let's divide by 10, and that number will give us a pixel number. BufferedImage image = new BufferedImage(6000, 2600, 1); int blackNumber = 0*65536 + 0*256 + 0; int whiteNumber = 255*65536 + 255*256 + 255; // Begin with white image, and paint black for inhabited pixels. for (int x = 0; x < 6000; ++x) for (int y = 0; y < 2600; ++y) image.setRGB(x, y, whiteNumber); while (in.hasNextLine()) { String line = in.nextLine(); StringTokenizer tok = new StringTokenizer(line, "\t "); int lat = Integer.parseInt(tok.nextToken()); int lon = Integer.parseInt(tok.nextToken()); int x = lon / 10 - 6500; int y = lat / 10 - 2400; System.out.printf("%d %d %d %d\n", lat, lon, x, y); image.setRGB(x, y, blackNumber); } in.close(); // Finish the image. try { ImageIO.write(image, "png", new File("usa.png")); } catch (IOException e) { System.out.printf("Encountered problem trying to write to file\n"); } } }