处理随机建筑绘图 Window 问题

Processing Randomized Building Drawing Window Issue

我重新发布了这个,因为我第一次发布时代码不正确。我把这一切都归结为这一部分。看起来像这样。

但它应该是这样的。

任何帮助windows 与建筑物对齐的帮助都会很棒,因为我对此很陌生。谢谢

int[] buildingHeights = new int[12];
int[] starXValues = new int[200];
int[] starYValues = new int[200];

void setup(){
 size(600,450);
 background(0);
}
void draw(){}
void drawAll(){
  fill(255);
  stroke(255);
  drawSky();
  drawBuildings();
}
void drawSky(){
  stroke(255);
  strokeWeight(1);
  ellipse(500,-20,150,150);
  for(int i = 0; i<starXValues.length; i++)
  point(starXValues[i],starYValues[i]);
}
void drawBuildings(){
  stroke(0);
  for(int i = 0; i<buildingHeights.length;i++){
    drawBuilding(i*50,height-buildingHeights[i]*50);
  }
}
void drawBuilding(int x, int y){
  fill(100);
  rect(x,height,50,-y);
  for(int i = 1; i < 7; i++){
    for(int j = 1; j < height/y; j++){
       int lights = (int)random(2);
       if(lights==1)
       fill(#ECFF27);
       else
       fill(255);
       rect(x*i,y*i,5,10);
    }
  }
}
void randomize(){
  for (int n = 0; n < starXValues.length; n++ ) 
    starXValues[n] = (int)random(width);  
  for (int n = 0; n < starYValues.length; n++ ) 
    starYValues[n] = (int)random(width);   
  for (int n = 0; n < buildingHeights.length; n++ ) 
    buildingHeights[n] = (int)random(8); 
    
}
void mousePressed(){
  background(0);
  randomize();
  drawAll();
}

您在根据您的建筑物调整 windows x/y 位置时遇到问题,我尝试将其修复为尽可能接近您显示的示例图片

void drawBuilding(int x, int y){
  fill(100);
  rect(x,height,50,-y);
  for(int i = 1; i < 6; i++){
    for(int j = 1; j <= y/25; j++){
       int lights = (int)random(2);
       if(lights==1)
       fill(#ECFF27);
       else
       fill(0);
       
       rect(x+i*10-8,height-j*25+12,5,10);
    }
  }
}

由于有人已经 post 接受了处理中的答案,我将继续 post 我的 Java Swing 尝试。

我把月亮画得更黄了,因为那是月亮靠近 horizon 时的颜色。

这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DowntownAtNight implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DowntownAtNight());
    }
    
    private DrawingPanel drawingPanel;

    @Override
    public void run() {
        JFrame frame = new JFrame("Downtown At Night");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        drawingPanel = new DrawingPanel();
        frame.add(drawingPanel, BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    public void repaint() {
        drawingPanel.repaint();
    }
    
    public class DrawingPanel extends JPanel {
        
        private static final long serialVersionUID = 1L;
        
        private final int width, height, margin;
        
        private final Dimension floor;
        private final Dimension window;
        
        private final Random random;

        public DrawingPanel() {
            this.width = 600;
            this.height = 450;
            this.margin = 10;
            this.setBackground(Color.BLACK);
            this.setPreferredSize(new Dimension(width + margin + margin, 
                    height + margin + margin));
            this.addMouseListener(new NewDowntownListener());
            this.random = new Random();
            
            // 12 buildings, 5 windows per building floor, 
            this.floor = new Dimension(50, 25);
            this.window = new Dimension(4, 10);
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            Color moonColor = new Color(252, 195, 106);
            Color buildingColor = new Color(100, 100, 100);
            Color windowOnColor = new Color(200, 200, 0);
            
            int x = width - margin - margin;
            int y = margin;
            g.setColor(moonColor);
            g.fillOval(x - 100, y - 100, 200, 200);
            
            x = margin;
            
            for (int building = 0; building < 12; building++) {
                int floors = random.nextInt(14) + 2;
                int buildingHeight = floors * floor.height;
                y = height + margin - buildingHeight;
                g.setColor(buildingColor);
                g.fillRect(x, y, floor.width, buildingHeight);
                g.setColor(Color.BLACK);
                g.drawRect(x, y, floor.width, buildingHeight);
                
                y = height + margin;
                for (int level = 0; level < floors; level++) {
                    int xf = x + 5;
                    y -= (window.height + 2);
                    for (int i = 0; i < 5; i++) {
                        boolean isOn = (random.nextInt(4) == 1);
                        if (isOn) {
                            g.setColor(windowOnColor);
                        } else {
                            g.setColor(Color.BLACK);
                        }
                        g.fillRect(xf, y, window.width, window.height);
                        xf += 9;
                    }
                    y -= floor.height - window.height - 2;
                }
                
                x += floor.width;
            }
        }
    }
    
    public class NewDowntownListener extends MouseAdapter {

        @Override
        public void mouseReleased(MouseEvent event) {
            repaint();
        }
        
    }

}