Processing Randomized Building Drawing Window Issue

Viewed 185

I'm reposting this as the code was not correct the first time I posted this. I got it all down up to this part. It looks like this.

It Looks Like This,

but it should look like this.

It Should Look Like This.

Any Help with making it so the windows align to the buildings would be great as I am quite new to this. Thanks

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();
}
2 Answers

You had a problem adjusting the windows x/y positions according to your building, I tried to fix it to look as close as possible to the example picture you showed

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);
    }
  }
}

Since someone already posted an accepted Processing answer, I'll go ahead and post my Java Swing attempt.

Downtown at night

I made the moon more yellow since that's the color of the moon when it's near the horizon.

Here's the complete runnable code.

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();
        }
        
    }

}
Related