Advertisement
conception

purple amer

Nov 15th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.scene.*;
  4. import javafx.scene.paint.*;
  5. import javafx.scene.canvas.*;
  6.  
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Scanner;
  12.  
  13. public class Controller {
  14. public Canvas mapCanvas = new Canvas();
  15. USAData usa = new USAData();
  16. int repVotes;
  17. int demVotes;
  18. int other;
  19. private HashMap results;
  20.  
  21.  
  22. private static class USAData {
  23. String mapName;
  24. double minLongitude;
  25. double minLatitude;
  26. double maxLongitude;
  27. double maxLatitude;
  28. int subregionCount;
  29. ArrayList<Subregion> subregionList = new ArrayList<>();
  30.  
  31. } // end class USAData
  32.  
  33. private static class Subregion {
  34. int pointCount;
  35. String stateName;
  36. ArrayList<Double> longitudes = new ArrayList<>();
  37. ArrayList<Double> latitudes = new ArrayList<>();
  38.  
  39. } // end class Subregion
  40.  
  41. public void readUSA() throws FileNotFoundException {
  42. Scanner fin = new Scanner(new File("USA.txt"));
  43.  
  44. // Reads in the four integers on first line
  45. usa.minLongitude = fin.nextDouble();
  46. usa.minLatitude = fin.nextDouble();
  47. usa.maxLongitude = fin.nextDouble();
  48. usa.maxLatitude = fin.nextDouble();
  49.  
  50. // Get number of subregions
  51. usa.subregionCount = fin.nextInt();
  52.  
  53. // Set usa's mapname to "USA"
  54. usa.mapName = "USA";
  55. Subregion sub = new Subregion();
  56.  
  57. fin.nextLine();
  58. fin.nextLine();
  59. while (fin.hasNext()) {
  60.  
  61. String stateName = fin.nextLine();
  62. sub.stateName = stateName;
  63. fin.nextLine();
  64. sub.pointCount = fin.nextInt();
  65. fin.nextLine();
  66.  
  67. // Add coordinates
  68. addCoordinates(sub, fin);
  69. usa.subregionList.add(sub);
  70. } // end while
  71. } // end readUSA
  72.  
  73. private void addCoordinates(Subregion sub, Scanner sc) {
  74. // Add a loop to run through all the coordinates in this block of the text file, stops when it hits blank line.
  75. Scanner oneLine;
  76. while (sc.hasNextLine()) {
  77. // Get next line's longitude and latitude and add them to the subregion's longitude and latitude lists
  78. String nextLine = sc.nextLine();
  79. oneLine = new Scanner(nextLine);
  80.  
  81. // If next line is blank, break while loop
  82. if (!oneLine.hasNextDouble()) {
  83. break;
  84. } // end if
  85.  
  86. double first = oneLine.nextDouble();
  87. double second = oneLine.nextDouble();
  88.  
  89. sub.longitudes.add(first);
  90. sub.latitudes.add(second);
  91. } // end while
  92.  
  93. } // end addCoordinates
  94.  
  95. public void initialize() throws FileNotFoundException {
  96. ElectionResults electionResults;
  97. readUSA();
  98. paint();
  99. readElectionResults();
  100. } // end initialize
  101.  
  102. // Part 2: Drawing the map.
  103. public void paint() {
  104.  
  105. GraphicsContext gc = mapCanvas.getGraphicsContext2D();
  106.  
  107. double mX = (mapCanvas.getWidth() - 1) / (usa.maxLongitude - usa.minLongitude);
  108. double bX = -mX * usa.minLongitude;
  109. double mY = (mapCanvas.getHeight() - 1) / (usa.minLatitude - usa.maxLatitude);
  110. double bY = -mY * usa.maxLatitude;
  111.  
  112. for (int i = 0; i < usa.subregionCount; i++) {
  113. double xCoordinate[] = new double[usa.subregionList.get(i).pointCount];
  114. double yCoordinate[] = new double[usa.subregionList.get(i).pointCount];
  115.  
  116. for (int j = 0; j < usa.subregionList.get(j).pointCount; j++) {
  117. xCoordinate[j] = mX * usa.subregionList.get(i).longitudes.get(j) + bX;
  118. yCoordinate[j] = mY * usa.subregionList.get(i).latitudes.get(j) + bY;
  119.  
  120. } //end for
  121. gc.strokePolygon(xCoordinate, yCoordinate, xCoordinate.length);
  122. } // end for
  123.  
  124. } // end paint
  125.  
  126. // Part 3
  127. class ElectionResults {
  128. String name;
  129.  
  130. private HashMap<String, ElectionResults> results;
  131. public int repVotes;
  132. public int demVotes;
  133. public int other;
  134. } // end class ElectionResults
  135.  
  136.  
  137. public void readElectionResults() throws FileNotFoundException {
  138. Scanner fin = new Scanner(new File("USA2012.txt"));
  139. fin.useDelimiter(",");
  140.  
  141. this.results = new HashMap<>();
  142.  
  143. fin.nextLine();
  144.  
  145. String stateName;
  146. while (fin.hasNextLine()) {
  147. // Stats for current state
  148. stateName = fin.next();
  149. int rep = fin.nextInt();
  150. int dem = fin.nextInt();
  151. int third = fin.nextInt();
  152. fin.nextLine();
  153.  
  154. ElectionResults state = new ElectionResults();
  155. state.name = stateName;
  156. state.repVotes = rep;
  157. state.demVotes = dem;
  158. state.other = third;
  159.  
  160.  
  161. this.results.put(stateName, state);
  162. } // end while
  163. } // end readElectionResults
  164. } // end class Controller
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement