// DIDN'T DO IN LECTURE, BUT PRETTY COOL IF YOU WANT TO EXAMINE // THE CODE AND/OR GIVE IT A TRY! Of particplar interest is the // graphCities method. // Tyler Rigsby, CSE 142 // Plots cities with population greater than or equal to // a value specified by the user import java.awt.*; // For Graphics import java.io.*; // For File, FileNotFoundException import java.util.*; // For Scanner public class Cities { // The width and height of the overall DrawingPanel public static final int WIDTH = 800; public static final int HEIGHT = 515; // The thickness of the gray border public static final int BORDER = 100; // The minimum and maximum latitude and longitude. // I wrote code to find these in the input file rather than // manually checking. public static final int MIN_LAT = 24562841; public static final int MAX_LAT = 48996136; public static final int MIN_LON = -124610967; public static final int MAX_LON = -67012040; public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); Scanner cityScan = new Scanner(new File("cities.txt")); printIntro(); System.out.print("Plot all cities with population at least: "); int minPop = console.nextInt(); DrawingPanel panel = new DrawingPanel(WIDTH, HEIGHT); Graphics g = panel.getGraphics(); int cities = graphCities(g, cityScan, minPop); graphStatic(g, minPop, cities); } // Plots the cities with population greater than or equal to // minPop and returns the number of cities plotted. public static int graphCities(Graphics g, Scanner cityScan, int minPop) { int count = 0; while (cityScan.hasNextLine()) { String line = cityScan.nextLine(); Scanner lineScan = new Scanner(line); String city = lineScan.next(); String state = lineScan.next(); int pop = lineScan.nextInt(); if (pop >= minPop) { int lat = lineScan.nextInt(); int lon = lineScan.nextInt(); plotCity(g, lat, lon); count++; } } return count; } // plots a city with the specified latitude and longitude on the map public static void plotCity(Graphics g, int lat, int lon) { // Use convertRange to convert the latitude or longitude // to screen coordinates. int y = HEIGHT - BORDER - convertRange(lat, MIN_LAT, MAX_LAT, HEIGHT - 2 * BORDER); int x = BORDER + convertRange(lon, MIN_LON, MAX_LON, WIDTH - 2 * BORDER); g.fillOval(x, y, 2, 2); } // Takes a value, a range that it is within, and the size of a new // range. Converts the value to the new range. For example, if the // value is halfway between the rangeMin and rangeMax, it will be // converted to half of the size of the new range. public static int convertRange(int value, int rangeMin, int rangeMax, int newRange) { return (int)((value - rangeMin) * 1.0 / (rangeMax - rangeMin) * newRange); } // Draws the border and stats about how many cities were plotted. public static void graphStatic(Graphics g, int minPop, int cities) { g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, WIDTH, BORDER); g.fillRect(0, HEIGHT - BORDER, WIDTH, BORDER); g.fillRect(0, 0, BORDER, HEIGHT); g.fillRect(WIDTH - BORDER, 0, BORDER, HEIGHT); g.setColor(Color.BLACK); g.drawString("Cities with at least " + minPop + " people", 10, 20); g.drawString(cities + " cities plotted", 10, HEIGHT - 10); } // Introduces the program. public static void printIntro() { System.out.println("This program allows you to search through"); System.out.println("United States census data from the year 2009"); System.out.println("and plot cities depending on their population"); System.out.println(); } }