Advertisement
Guest User

City.java

a guest
Mar 10th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package com;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.Rectangle;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.util.ArrayList;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Queue;
  12. import java.util.Random;
  13.  
  14. import javax.imageio.ImageIO;
  15.  
  16. public class City {
  17.     private static final Random rand = new Random();
  18.    
  19.     public Area area;
  20.     public List<Area> areas = new ArrayList<>();
  21.    
  22.     public City(Area area, int dist, int density, int densityMin, int densityMax) {
  23.         this.area = area;
  24.         Queue<Area> list = new LinkedList<>();
  25.         list.add(area);
  26.         while (!list.isEmpty()) {
  27.             Area current = list.remove();
  28.             int wval = get(densityMin, densityMax);
  29.             int hval = get(densityMin, densityMax);
  30.             if (current.w < wval || current.h < hval) {
  31.                 areas.add(current);
  32.                 if (get(0, 100) <= density) {
  33.                     current.visible = true;
  34.                 }
  35.             } else {
  36.                 int woffs = (int) ((float) current.w / 100 * dist);
  37.                 int hoffs = (int) ((float) current.h / 100 * dist);
  38.                 int w = current.w / 2 + get(-woffs / 2, woffs / 2);
  39.                 int h = current.h / 2 + get(-hoffs / 2, hoffs / 2);
  40.                 list.add(new Area(current.x, current.y, w, h));
  41.                 list.add(new Area(current.x + w, current.y, current.w - w, h));
  42.                 list.add(new Area(current.x, current.y + h, w, current.h - h));
  43.                 list.add(new Area(current.x + w, current.y + h, current.w - w, current.h - h));
  44.             }
  45.         }
  46.        
  47.         for (Area a : areas) {
  48.             a.fillObjects();
  49.         }
  50.     }
  51.    
  52.     public void saveMap(String path) {
  53.         BufferedImage map = new BufferedImage(area.w + 1, area.h + 1, BufferedImage.TYPE_INT_RGB);
  54.        
  55.         Graphics2D g = (Graphics2D) map.getGraphics();
  56.         g.setColor(new Color(0xBFBBBA));
  57.         g.fillRect(0, 0, area.w, area.h);
  58.        
  59.         for (Area a : areas) {
  60.             if (a.visible) {
  61.                 g.setColor(new Color(0x636B77));
  62.                 g.drawRect(a.x, a.y, a.w, a.h);
  63.                 if (rand.nextInt(5) != 0) {
  64.                     g.drawRect(a.x + 1, a.y + 1, a.w, a.h);
  65.                     g.drawRect(a.x - 1, a.y - 1, a.w, a.h);
  66.                 }
  67.             }
  68.            
  69.             int or = 120 + rand.nextInt(5);
  70.             int og = 120 + rand.nextInt(5);
  71.             int ob = 120 + rand.nextInt(20);
  72.             g.setColor(new Color(or, og, ob, 255));
  73.             for (Rectangle r : a.objects) {
  74.                 g.fillRect(r.x, r.y, r.width, r.height);
  75.             }
  76.         }
  77.         g.dispose();
  78.        
  79.         try {
  80.             ImageIO.write(map, "png", new File(path));
  81.         } catch (Exception e) {
  82.             e.printStackTrace();
  83.         }
  84.     }
  85.    
  86.     private int get(int x1, int x2) {
  87.         return x1 + rand.nextInt(x2 - x1 + 1);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement