在建筑物中创建 "windows" 的循环? - Java 摇摆

Loop which creates "windows" in buildings? - Java Swing

我是 Java 的初学者,尤其是 Java Swing。我想要做的是创建随机生成的 "buildings"(我已经完成了)并在它们之上添加 rows/columns 中的 windows 大小和间距相等。但是我想循环执行此操作(for 循环?)。我的最终目标是每次也只点亮一些 windows(这也是随机生成的)代码是 运行。这是我到目前为止所拥有的,我创建的 window 基本上是我想要的大小。我知道我的属性 maxX 没有被使用,但我创建它是为了提醒我最大 X 值,以备我以后需要它。

import java.awt.*; 
import javax.swing.*;
public class JPanelExample extends JPanel 
{     
  private int maxX = 784;
  private int maxY = 712;
  @Override 
  public void paint(Graphics g) 
  { 
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.WHITE);
    g2d.fillRect(5, 5, 25, 25);
    int width = (int)(Math.random()*100+100);
    int height = (int)(Math.random()*350+100);

    for (int i =10; i<634; i+=(width+10))
    {
      for (int j = 462 ; j>=462; j--)
      {

        g2d.setColor(Color.GRAY);
        g2d.drawRect(i, maxY-height, width, height);
        g2d.fillRect(i, maxY-height, width, height);
        g2d.setColor(Color.YELLOW);

        g2d.drawRect(i+5, (maxY-height)+5, width/6, width/6);
        g2d.fillRect(i+5, (maxY-height)+5, width/6, width/6);

        height = (int)(Math.random()*462+100);

        while (width == i+(width+10))
        {
          width = (int)(Math.random()*100+100);
        }
      }
    }
  }
  public static void main(String[] args) 
  { 
    JFrame frame = new JFrame("Frame"); 
    frame.add(new JPanelExample());
    frame.setBackground(Color.BLACK);
    frame.setSize(800, 750); 
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

我希望你玩得开心 Java 如果你还在寻找答案(尽管有很多),我的建议是:

为了在您在这里绘制的建筑物上获得一行 windows,您需要知道:

  • 你要画多少 windows(从你的代码来看,你总是想要 6 windows)
  • 从哪里开始绘制(从你的代码来看,这似乎是每个建筑开始后的 5px
  • 你想要 windows 之间有多少间距(这在你的代码中并不明显,但它可以是你想要的任何东西——在这个例子中我们将再次使用 5px)
  • window 应该是什么颜色(在本例中,我们将使用白色表示无光,黄色表示有光)。

设置

我们可以像这样将所有这些数字存储在变量中(使用您代码中的值):

// for the lights:
Color off = Color.WHITE;
Color on = Color.YELLOW;

// for window calculations
int space = 5;
int startx = i+space;
int starty = maxY-height+space;
int interval = (width-space)/6;

请注意,我们在除以 6 之前从建筑物的宽度中减去一个 space,以在第一个 window 之前折扣初始 space。我相信你已经看过了,但是从 Java/Swing

开始画图也很有帮助

循环

我们现在可以遍历您想放在 windows 行中的 windows 个数。在这种情况下,看起来您每次都需要 6 windows。对于每个 window(在 for 循环内),我们首先要决定颜色(在示例中,我们使用 10% 的机会打开灯,如下所述)。一旦我们设置了颜色,我们只需使用我们上面提到的值来填充下一个 window 矩形,这将在下面解释。 确保您的 for 循环具有不同的变量名称,因为我们将它们嵌套在这里

for(int k = 0; k < 6; k++) {
    if((int)(Math.random()*100) < 10) //10% of the time, turn the light color on
        g2d.setColor(on);
    else 
        g2d.setColor(off);

    //g2d.drawRect(i+5, (maxY-height)+5, width/6, width/6);
    g2d.fillRect(startx+(k*interval), starty, (interval)-space, (interval)-space);
}

注意:这里的 drawRect 和 fillRect 是多余的。我不确定您是否有特定目的首先绘制矩形(在这种情况下,请忽略此),但查看您的应用程序并注释掉 drawRect 以查看它的作用。

如果您对此处的 Math.random 函数有任何疑问,请查看这些资源:

http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

Math.random() explained

探索示例中的 fillRect:

fillRect 采用下面提到的几个变量(详情见:http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

fillRect(int x, int y, int width, int height)

在我们的示例中,我们设置了以下内容:

x = startx + (k * interval) ,k 是 for 循环的增量变量(从 1 .. 6 运行),在每个 [= 之后将矩形的 x 值增加 window 的间隔121=]绘制。

y = starty ,每一行都是一致的。

width, height = interval - space ,这会设置 window 的尺寸并确保在下一个 window.

之前有一个正确的 space

这应该给你一个有效的 1 行 windows。

获取 windows 的列与获取行非常相似。有很多方法可以完成所有这些事情,但我将简要介绍另一个嵌套 for 循环。

为了得到 windows 的列,我们需要知道:

  • 你想要多少行(每列多少windows)

因为我们已经设置了 window 维度,所以我们可以使用以下方法确定可以放入一列的 windows 的数量:

int num_rows = (height - space) / interval;
//and to set a maximum number of rows to a column (ex: 6)
int num_rows = Math.min(((height-space) / interval), 6); //a max number can replace 6 here

这里我们将使用另一个 for 循环来连续添加行,num_rows 次。 注意:我们已经更改了 fillRect 使得 y = starty + (l*interval) ,每次绘制一行时将起始位置 y 递增到行的下一个位置。

for(int l = 0; l < num_rows; l++){
    for(int k = 0; k < 6; k++) {
        if((int)(Math.random()*100) < 10) //10% of the time, turn the light color on
            g2d.setColor(on);
        else 
            g2d.setColor(off);

        g2d.fillRect(startx+(k*interval), starty + (l*interval), (interval)-space, (interval)-space);
    }
}

就是这样。希望这对您有所帮助,不要太混乱。

想了半天,想出了解决办法。我已将 for 循环更改为:

   for (int i =10; i<634; i+=(a+10))//buildings
{

  g2d.setColor(Color.GRAY);
  g2d.drawRect(i, maxY-height, width, height);
  g2d.fillRect(i, maxY-height, width, height);


  rows = Math.round((height)/25);
  columns = Math.round(width/25);

  for (int j = 1; j<=columns; j++)//windows
  {
    for (int k = 1; k<=rows; k++)
    {
      g2d.setColor(Color.BLACK);
      g2d.drawRect(i+5*j+20*(j-1), (maxY-height)+5*k+20*(k-1), 20, 20);
      if (Math.random()<0.7)
      {
        g2d.setColor(Color.YELLOW);
        g2d.fillRect(i+5*j+20*(j-1), (maxY-height)+5*k+20*(k-1), 20, 20);
      }
      else
      {
        g2d.setColor(transYellow);
        g2d.fillRect(i+5*j+20*(j-1), (maxY-height)+5*k+20*(k-1), 20, 20);
        g2d.setColor(transYellow);
        g2d.fillRect(i+5*j+20*(j-1), (maxY-height)+5*k+20*(k-1), 20, 20);
      }
    }
  }

  addBuilding();
  a = width;
  height = (int)(Math.random()*462+100);
  width = (int)(Math.random()*100+100);

}