Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.util.*;
- /**
- * The TeamMap class is a subclass of JPanel. This subclass is used for drawing a map of the United States
- * that displays a specific teams location, and draws lines to the players respective birth cities.
- **/
- public class TeamMap extends JPanel
- {
- /**Stores a list of the people that are on the team.**/
- private ArrayList<String>peopleOnTeam=new ArrayList<String>();
- /**Stores a key, team members, and a value, latitude and longitude, for the birthcities of the team members.**/
- private HashMap<String,String>points=new HashMap<String,String>();
- /**Stores the latitude for the home city of the team.**/
- private double latitudeTeam;
- /**Stores the longitude for the home city of the team.**/
- private double longitudeTeam;
- /**Stores the name for the team.**/
- private String teamName;
- /**
- * Constructor for this class.
- * @param name Passes in the teams name.
- * @param lat Passes in the teams home cities latitude.
- * @param lon Passes in the teams home cities longitude.
- * @param people Passes in a list of team members.
- * @param latLon Passes in a hash map of team member names and their latitudes and longitudes.
- */
- public TeamMap(String name, double lat, double lon, ArrayList<String>people, HashMap<String,String>latLon)
- {
- teamName=name;
- latitudeTeam=lat;
- longitudeTeam=lon;
- peopleOnTeam=people;
- points=latLon;
- }
- /**
- * The paintComponent(Graphics g) method overrides the JPanel's version of this method. This method
- * is meant for creating the teams map of players birth cities.
- * <P>
- * Algorithm:<br>
- * 1. Creates a map of the United States.<br>
- * 2. Runs through the points hash map and gets latitudes and longitudes.<br>
- * 3. Converts the latitude and longitude into pixel locations.<br>
- * 4. Draws a point on the map from step 3.<br>
- * 5. Displays the teams location and name.<br>
- * </P>
- *
- */
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- ImageIcon mapTeam = new ImageIcon("Map1.PNG");
- mapTeam.paintIcon(this,g,0,0);
- Font font = new Font("Comic Sans Serif",Font.PLAIN,9);
- g.setFont(font);
- setBackground(Color.white);
- g.setColor(Color.black);
- if(!peopleOnTeam.isEmpty() && ! points.isEmpty())
- {
- for(int i=0; i<peopleOnTeam.size(); i++)
- {
- if(points.containsKey(peopleOnTeam.get(i)))//Checks to see if this person has a birth city.
- {
- String[]tokens=points.get(peopleOnTeam.get(i)).split("[,]");
- if(tokens.length == 2)
- {
- //This block of code converts the latitudes and longitudes into pixels and draws them with a line connectiong the
- //home city of the team.
- double teamLat=Double.parseDouble(tokens[0]);
- double teamLong=Double.parseDouble(tokens[1].substring(1));
- int systemLat = (int)(690-13.7*teamLat);
- int systemLong = (int)(1388.2-11.1*teamLong);
- g.fillOval(systemLong-2,systemLat-3,5,5);
- g.drawLine(systemLong-2,systemLat-3,((int)(690-13.7*longitudeTeam))-2,((int)(1388.2-11.1*latitudeTeam))-3);
- }
- }
- }
- //This next block draws the teams home city and name on the map.
- g.setColor(Color.blue);
- g.fillOval(((int)(690-13.7*longitudeTeam))-2,((int)(1388.2-11.1*latitudeTeam))-3,5,5);
- g.drawString(teamName,((int)(690-13.7*longitudeTeam))-2,((int)(1388.2-11.1*latitudeTeam))-3);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement