Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.JPanel;
- public class Map extends JPanel {
- //Width & Height of map
- private int width, height;
- //Constructor
- public Map(int w, int h) {
- this.width = w;
- this.height = h;
- }
- //Scales coordinates to width/height of map
- private void scaleCoordinates() {
- for (int i = 0; i < Constants.COUNTRY_COORD.length; i++) {
- Constants.COUNTRY_COORD[i][0] = ((Constants.COUNTRY_COORD[i][0] / 1000) * width);
- Constants.COUNTRY_COORD[i][1] = ((Constants.COUNTRY_COORD[i][1] / 600) * height);
- }
- }
- public void paint(Graphics g) {
- scaleCoordinates();
- //Draws lines between adjacent countires
- for (int x = 0; x < Constants.COUNTRY_COORD.length; x++) {
- for (int y = 0; y < Constants.ADJACENT[x].length; y++) {
- Graphics2D lineDrawer = (Graphics2D) g;
- lineDrawer.setColor(Color.BLACK);
- //Draws one line through edges of map instead of through the middle
- if ((x == 8 && y == 2) || (x == 22 && y == 0)){
- lineDrawer.drawLine((int) Constants.COUNTRY_COORD[22][0] + (Constants.COUNTRY_DIAMETER / 2),
- (int) Constants.COUNTRY_COORD[22][1] + (Constants.COUNTRY_DIAMETER / 2),
- width,
- (int) Constants.COUNTRY_COORD[22][1] + (Constants.COUNTRY_DIAMETER / 2) - 2);
- lineDrawer.drawLine((int) Constants.COUNTRY_COORD[8][0] + (Constants.COUNTRY_DIAMETER / 2),
- (int) Constants.COUNTRY_COORD[8][1] + (Constants.COUNTRY_DIAMETER / 2),
- 0,
- (int) Constants.COUNTRY_COORD[8][1] + (Constants.COUNTRY_DIAMETER / 2) + 2);
- continue;
- }
- //Draws all the other lines
- lineDrawer.drawLine((int) Constants.COUNTRY_COORD[x][0] + (Constants.COUNTRY_DIAMETER / 2),
- (int) Constants.COUNTRY_COORD[x][1] + (Constants.COUNTRY_DIAMETER / 2),
- (int) Constants.COUNTRY_COORD[(Constants.ADJACENT[x][y])][0] + (Constants.COUNTRY_DIAMETER / 2),
- (int) Constants.COUNTRY_COORD[(Constants.ADJACENT[x][y])][1] + (Constants.COUNTRY_DIAMETER / 2));
- }
- }
- //Draws circles for each country
- for (int i = 0; i < Constants.COUNTRY_COORD.length; i++) {
- Graphics2D countryDrawer = (Graphics2D) g;
- //Switches color of painter based on continent
- switch (Constants.CONTINENT_IDS[i]) {
- case 0: countryDrawer.setColor(Color.RED);
- break;
- case 1: countryDrawer.setColor(Color.YELLOW);
- break;
- case 2: countryDrawer.setColor(Color.GREEN);
- break;
- case 3: countryDrawer.setColor(Color.BLUE);
- break;
- case 4: countryDrawer.setColor(Color.CYAN);
- break;
- case 5: countryDrawer.setColor(Color.MAGENTA);
- break;
- }
- //Draws each circle
- countryDrawer.fillOval((int) Constants.COUNTRY_COORD[i][0],
- (int) Constants.COUNTRY_COORD[i][1],
- Constants.COUNTRY_DIAMETER,
- Constants.COUNTRY_DIAMETER);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement