Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- import java.util.Random;
- import javax.imageio.ImageIO;
- public class City {
- private static final Random rand = new Random();
- public Area area;
- public List<Area> areas = new ArrayList<>();
- public City(Area area, int dist, int density, int densityMin, int densityMax) {
- this.area = area;
- Queue<Area> list = new LinkedList<>();
- list.add(area);
- while (!list.isEmpty()) {
- Area current = list.remove();
- int wval = get(densityMin, densityMax);
- int hval = get(densityMin, densityMax);
- if (current.w < wval || current.h < hval) {
- areas.add(current);
- if (get(0, 100) <= density) {
- current.visible = true;
- }
- } else {
- int woffs = (int) ((float) current.w / 100 * dist);
- int hoffs = (int) ((float) current.h / 100 * dist);
- int w = current.w / 2 + get(-woffs / 2, woffs / 2);
- int h = current.h / 2 + get(-hoffs / 2, hoffs / 2);
- list.add(new Area(current.x, current.y, w, h));
- list.add(new Area(current.x + w, current.y, current.w - w, h));
- list.add(new Area(current.x, current.y + h, w, current.h - h));
- list.add(new Area(current.x + w, current.y + h, current.w - w, current.h - h));
- }
- }
- for (Area a : areas) {
- a.fillObjects();
- }
- }
- public void saveMap(String path) {
- BufferedImage map = new BufferedImage(area.w + 1, area.h + 1, BufferedImage.TYPE_INT_RGB);
- Graphics2D g = (Graphics2D) map.getGraphics();
- g.setColor(new Color(0xBFBBBA));
- g.fillRect(0, 0, area.w, area.h);
- for (Area a : areas) {
- if (a.visible) {
- g.setColor(new Color(0x636B77));
- g.drawRect(a.x, a.y, a.w, a.h);
- if (rand.nextInt(5) != 0) {
- g.drawRect(a.x + 1, a.y + 1, a.w, a.h);
- g.drawRect(a.x - 1, a.y - 1, a.w, a.h);
- }
- }
- int or = 120 + rand.nextInt(5);
- int og = 120 + rand.nextInt(5);
- int ob = 120 + rand.nextInt(20);
- g.setColor(new Color(or, og, ob, 255));
- for (Rectangle r : a.objects) {
- g.fillRect(r.x, r.y, r.width, r.height);
- }
- }
- g.dispose();
- try {
- ImageIO.write(map, "png", new File(path));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private int get(int x1, int x2) {
- return x1 + rand.nextInt(x2 - x1 + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement