Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.scene.*;
- import javafx.scene.paint.*;
- import javafx.scene.canvas.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Scanner;
- public class Controller {
- public Canvas mapCanvas = new Canvas();
- USAData usa = new USAData();
- int repVotes;
- int demVotes;
- int other;
- private HashMap results;
- private static class USAData {
- String mapName;
- double minLongitude;
- double minLatitude;
- double maxLongitude;
- double maxLatitude;
- int subregionCount;
- ArrayList<Subregion> subregionList = new ArrayList<>();
- } // end class USAData
- private static class Subregion {
- int pointCount;
- String stateName;
- ArrayList<Double> longitudes = new ArrayList<>();
- ArrayList<Double> latitudes = new ArrayList<>();
- } // end class Subregion
- public void readUSA() throws FileNotFoundException {
- Scanner fin = new Scanner(new File("USA.txt"));
- // Reads in the four integers on first line
- usa.minLongitude = fin.nextDouble();
- usa.minLatitude = fin.nextDouble();
- usa.maxLongitude = fin.nextDouble();
- usa.maxLatitude = fin.nextDouble();
- // Get number of subregions
- usa.subregionCount = fin.nextInt();
- // Set usa's mapname to "USA"
- usa.mapName = "USA";
- Subregion sub = new Subregion();
- fin.nextLine();
- fin.nextLine();
- while (fin.hasNext()) {
- String stateName = fin.nextLine();
- sub.stateName = stateName;
- fin.nextLine();
- sub.pointCount = fin.nextInt();
- fin.nextLine();
- // Add coordinates
- addCoordinates(sub, fin);
- usa.subregionList.add(sub);
- } // end while
- } // end readUSA
- private void addCoordinates(Subregion sub, Scanner sc) {
- // Add a loop to run through all the coordinates in this block of the text file, stops when it hits blank line.
- Scanner oneLine;
- while (sc.hasNextLine()) {
- // Get next line's longitude and latitude and add them to the subregion's longitude and latitude lists
- String nextLine = sc.nextLine();
- oneLine = new Scanner(nextLine);
- // If next line is blank, break while loop
- if (!oneLine.hasNextDouble()) {
- break;
- } // end if
- double first = oneLine.nextDouble();
- double second = oneLine.nextDouble();
- sub.longitudes.add(first);
- sub.latitudes.add(second);
- } // end while
- } // end addCoordinates
- public void initialize() throws FileNotFoundException {
- ElectionResults electionResults;
- readUSA();
- paint();
- readElectionResults();
- } // end initialize
- // Part 2: Drawing the map.
- public void paint() {
- GraphicsContext gc = mapCanvas.getGraphicsContext2D();
- double mX = (mapCanvas.getWidth() - 1) / (usa.maxLongitude - usa.minLongitude);
- double bX = -mX * usa.minLongitude;
- double mY = (mapCanvas.getHeight() - 1) / (usa.minLatitude - usa.maxLatitude);
- double bY = -mY * usa.maxLatitude;
- for (int i = 0; i < usa.subregionCount; i++) {
- double xCoordinate[] = new double[usa.subregionList.get(i).pointCount];
- double yCoordinate[] = new double[usa.subregionList.get(i).pointCount];
- for (int j = 0; j < usa.subregionList.get(j).pointCount; j++) {
- xCoordinate[j] = mX * usa.subregionList.get(i).longitudes.get(j) + bX;
- yCoordinate[j] = mY * usa.subregionList.get(i).latitudes.get(j) + bY;
- } //end for
- gc.strokePolygon(xCoordinate, yCoordinate, xCoordinate.length);
- } // end for
- } // end paint
- // Part 3
- class ElectionResults {
- String name;
- private HashMap<String, ElectionResults> results;
- public int repVotes;
- public int demVotes;
- public int other;
- } // end class ElectionResults
- public void readElectionResults() throws FileNotFoundException {
- Scanner fin = new Scanner(new File("USA2012.txt"));
- fin.useDelimiter(",");
- this.results = new HashMap<>();
- fin.nextLine();
- String stateName;
- while (fin.hasNextLine()) {
- // Stats for current state
- stateName = fin.next();
- int rep = fin.nextInt();
- int dem = fin.nextInt();
- int third = fin.nextInt();
- fin.nextLine();
- ElectionResults state = new ElectionResults();
- state.name = stateName;
- state.repVotes = rep;
- state.demVotes = dem;
- state.other = third;
- this.results.put(stateName, state);
- } // end while
- } // end readElectionResults
- } // end class Controller
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement