Advertisement
CornOnTheKob

Map.java

Feb 14th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.JPanel;
  3.  
  4. public class Map extends JPanel {
  5.    
  6.     //Width & Height of map
  7.     private int width, height;
  8.    
  9.     //Constructor
  10.     public Map(int w, int h) {
  11.         this.width = w;
  12.         this.height = h;
  13.     }
  14.    
  15.     //Scales coordinates to width/height of map
  16.     private void scaleCoordinates() {
  17.         for (int i = 0; i < Constants.COUNTRY_COORD.length; i++) {
  18.             Constants.COUNTRY_COORD[i][0] = ((Constants.COUNTRY_COORD[i][0] / 1000) * width);
  19.             Constants.COUNTRY_COORD[i][1] = ((Constants.COUNTRY_COORD[i][1] / 600) * height);
  20.         }
  21.     }
  22.    
  23.     public void paint(Graphics g) {
  24.        
  25.         scaleCoordinates();
  26.        
  27.         //Draws lines between adjacent countires
  28.         for (int x = 0; x < Constants.COUNTRY_COORD.length; x++) {
  29.             for (int y = 0; y < Constants.ADJACENT[x].length; y++) {
  30.                 Graphics2D lineDrawer = (Graphics2D) g;
  31.                 lineDrawer.setColor(Color.BLACK);
  32.  
  33.                 //Draws one line through edges of map instead of through the middle
  34.                 if ((x == 8 && y == 2) || (x == 22 && y == 0)){
  35.                     lineDrawer.drawLine((int) Constants.COUNTRY_COORD[22][0] + (Constants.COUNTRY_DIAMETER / 2),
  36.                                         (int) Constants.COUNTRY_COORD[22][1] + (Constants.COUNTRY_DIAMETER / 2),
  37.                                               width,
  38.                                         (int) Constants.COUNTRY_COORD[22][1] + (Constants.COUNTRY_DIAMETER / 2) - 2);
  39.                    
  40.                     lineDrawer.drawLine((int) Constants.COUNTRY_COORD[8][0] + (Constants.COUNTRY_DIAMETER / 2),
  41.                                         (int) Constants.COUNTRY_COORD[8][1] + (Constants.COUNTRY_DIAMETER / 2),
  42.                                               0,
  43.                                         (int) Constants.COUNTRY_COORD[8][1] + (Constants.COUNTRY_DIAMETER / 2) + 2);
  44.                     continue;
  45.                 }
  46.  
  47.                 //Draws all the other lines
  48.                 lineDrawer.drawLine((int) Constants.COUNTRY_COORD[x][0] + (Constants.COUNTRY_DIAMETER / 2),
  49.                                     (int) Constants.COUNTRY_COORD[x][1] + (Constants.COUNTRY_DIAMETER / 2),
  50.                                     (int) Constants.COUNTRY_COORD[(Constants.ADJACENT[x][y])][0] + (Constants.COUNTRY_DIAMETER / 2),
  51.                                     (int) Constants.COUNTRY_COORD[(Constants.ADJACENT[x][y])][1] + (Constants.COUNTRY_DIAMETER / 2));
  52.             }
  53.         }
  54.        
  55.         //Draws circles for each country
  56.         for (int i = 0; i < Constants.COUNTRY_COORD.length; i++) {
  57.             Graphics2D countryDrawer = (Graphics2D) g;
  58.            
  59.             //Switches color of painter based on continent
  60.             switch (Constants.CONTINENT_IDS[i]) {
  61.                     case 0: countryDrawer.setColor(Color.RED);
  62.                     break;
  63.                     case 1: countryDrawer.setColor(Color.YELLOW);
  64.                     break;
  65.                     case 2: countryDrawer.setColor(Color.GREEN);
  66.                     break;
  67.                     case 3: countryDrawer.setColor(Color.BLUE);
  68.                     break;
  69.                     case 4: countryDrawer.setColor(Color.CYAN);
  70.                     break;
  71.                     case 5: countryDrawer.setColor(Color.MAGENTA);
  72.                     break;
  73.             }
  74.            
  75.             //Draws each circle
  76.             countryDrawer.fillOval((int) Constants.COUNTRY_COORD[i][0],
  77.                                    (int) Constants.COUNTRY_COORD[i][1],
  78.                                    Constants.COUNTRY_DIAMETER,
  79.                                    Constants.COUNTRY_DIAMETER);
  80.         }
  81.        
  82.     }
  83.        
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement