【问题标题】:Processing Randomized Building Drawing Window Issue处理随机建筑绘图窗口问题
【发布时间】:2021-05-01 12:17:33
【问题描述】:

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

但它应该看起来像这样。

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

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

【问题讨论】:

  • 图像看起来很有趣。确定窗口的大小(以像素为单位)。根据窗口和填充确定地板的高度(以像素为单位)。再次根据窗口和填充确定建筑物的宽度(以像素为单位)。绘制建筑物,为每个建筑物获取随机数量的楼层。绘制窗户,随机确定灯是否亮。

标签: java processing computer-science


【解决方案1】:

您在根据您的建筑物调整窗户 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);
    }
  }
}

【讨论】:

    【解决方案2】:

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

    我把月亮弄得更黄了,因为那是月亮接近地平线时的颜色。

    这是完整的可运行代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      相关资源
      最近更新 更多