Advertisement
INSECURE-FLOWERPOT

Untitled

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