Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.util.*;
- /**
- * The StateMap class is a subclass of JPanel. This subclass is used for creating a map of the United States that displayes
- * birthcities.
- *
- */
- public class StateMap extends JPanel
- {
- /**Stores keys,cities, and values, latitudes and longitudes.**/
- private HashMap<String,String> points = new HashMap<String,String>();
- /**Stores the keys (cities) to the above hash map.**/
- private ArrayList<String> keys = new ArrayList<String>();
- /**
- * StateMap's constructor.
- * @param mapCoordinates Passes in a hash map of cities and corresponding latitudes and longitudes.
- * @param keys Passes in a list of cities.
- */
- public StateMap(HashMap<String,String> mapCoordinates, ArrayList<String>keys)
- {
- points = mapCoordinates;
- this.keys = keys;
- }
- /**
- * /**
- * The paintComponent(Graphics g) method overrides the JPanel's version of this method. This method
- * is meant for creating a map of the United States with people's birthcities plotted on it.
- * <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 person's birth location and city name.<br>
- * </P>
- *
- */
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- ImageIcon picture = new ImageIcon("Map1.PNG");
- picture.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(!keys.isEmpty()&&!points.isEmpty())
- {
- for(int i=0; i<keys.size(); i++)
- {
- if(points.containsKey(keys.get(i)))
- {
- String latLong = points.get(keys.get(i));
- if(latLong != null)
- {
- String[]tokens = latLong.split("[,]");//changed from [; -]
- if(tokens.length == 2)
- {
- //This chunk of code converts latitudes and longitudes into doubles then
- //performes some algebra to get it to pixels then displays the point on the map
- //with the corresponding city name.
- double lat = Double.parseDouble(tokens[0]);
- double lon = Double.parseDouble(tokens[1]);
- int systemLat = (int)(690-13.7*lat);
- int systemLong = (int)(1388.2-11.1*lon);
- g.fillOval(systemLong-2,systemLat-3,5,5);
- g.drawString(keys.get(i), systemLong-2,systemLat-3);
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement