Advertisement
INSECURE-FLOWERPOT

Untitled

Mar 27th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import java.awt.*;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.util.*;
  6.  
  7. /**
  8. * The StateMap class is a subclass of JPanel. This subclass is used for creating a map of the United States that displayes
  9. * birthcities.
  10. *
  11. */
  12. public class StateMap extends JPanel
  13. {
  14. /**Stores keys,cities, and values, latitudes and longitudes.**/
  15. private HashMap<String,String> points = new HashMap<String,String>();
  16.  
  17. /**Stores the keys (cities) to the above hash map.**/
  18. private ArrayList<String> keys = new ArrayList<String>();
  19.  
  20. /**
  21. * StateMap's constructor.
  22. * @param mapCoordinates Passes in a hash map of cities and corresponding latitudes and longitudes.
  23. * @param keys Passes in a list of cities.
  24. */
  25. public StateMap(HashMap<String,String> mapCoordinates, ArrayList<String>keys)
  26. {
  27. points = mapCoordinates;
  28. this.keys = keys;
  29. }
  30.  
  31. /**
  32. * /**
  33. * The paintComponent(Graphics g) method overrides the JPanel's version of this method. This method
  34. * is meant for creating a map of the United States with people's birthcities plotted on it.
  35. * <P>
  36. * Algorithm:<br>
  37. * 1. Creates a map of the United States.<br>
  38. * 2. Runs through the points hash map and gets latitudes and longitudes.<br>
  39. * 3. Converts the latitude and longitude into pixel locations.<br>
  40. * 4. Draws a point on the map from step 3.<br>
  41. * 5. Displays the person's birth location and city name.<br>
  42. * </P>
  43. *
  44. */
  45.  
  46. public void paintComponent(Graphics g)
  47. {
  48. super.paintComponent(g);
  49. ImageIcon picture = new ImageIcon("Map1.PNG");
  50. picture.paintIcon(this,g,0,0);
  51. Font font = new Font("Comic Sans Serif", Font.PLAIN, 9);
  52. g.setFont(font);
  53. setBackground(Color.white);
  54. g.setColor(Color.black);
  55.  
  56. if(!keys.isEmpty()&&!points.isEmpty())
  57. {
  58. for(int i=0; i<keys.size(); i++)
  59. {
  60. if(points.containsKey(keys.get(i)))
  61. {
  62. String latLong = points.get(keys.get(i));
  63. if(latLong != null)
  64. {
  65. String[]tokens = latLong.split("[,]");//changed from [; -]
  66. if(tokens.length == 2)
  67. {
  68. //This chunk of code converts latitudes and longitudes into doubles then
  69. //performes some algebra to get it to pixels then displays the point on the map
  70. //with the corresponding city name.
  71. double lat = Double.parseDouble(tokens[0]);
  72. double lon = Double.parseDouble(tokens[1]);
  73.  
  74. int systemLat = (int)(690-13.7*lat);
  75. int systemLong = (int)(1388.2-11.1*lon);
  76.  
  77. g.fillOval(systemLong-2,systemLat-3,5,5);
  78. g.drawString(keys.get(i), systemLong-2,systemLat-3);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement