用Java在JPanel上绘制随机的非重叠矩形

用Java在JPanel上绘制随机的非重叠矩形,java,swing,Java,Swing,我想在Java Swing面板中绘制n个矩形。当我搜索这个问题时,我只找到了需要分割矩形的选项(如果我理解正确的话)。我希望能够设置一个固定的宽度和高度,并且只随机化X和Y坐标。我已经编写了一些代码,可以很好地处理少量的矩形(EDIT: 我知道你已经更正了我的代码。我看到了。那里的人使用了p5.js,我用Java编写了代码。这是更新的代码(我没有更改Box类): while(var.box.length

我想在Java Swing面板中绘制n个矩形。当我搜索这个问题时,我只找到了需要分割矩形的选项(如果我理解正确的话)。我希望能够设置一个固定的宽度和高度,并且只随机化X和Y坐标。我已经编写了一些代码,可以很好地处理少量的矩形(EDIT: 我知道你已经更正了我的代码。我看到了。那里的人使用了p5.js,我用Java编写了代码。这是更新的代码(我没有更改Box类):

while(var.box.length=var.maxIterations){
完成=错误;
打破
}否则{
完成=正确;
}
}
如果您感兴趣,我将源代码和一个可运行的JAR发布在我的

编辑: 我知道你已经更正了我的代码。我看到了。那里的人使用了p5.js,我用Java编写了代码。这是更新的代码(我没有更改Box类):

while(var.box.length=var.maxIterations){
完成=错误;
打破
}否则{
完成=正确;
}
}
如果您感兴趣,我将源代码和一个可运行的JAR发布在我的


为了获得更好的帮助,请尽快发布一篇适当的文章来说明您的问题。但是到目前为止,您在
paintComponent
方法中做了太多的事情,例如创建新对象。但是一旦我变大,它就会停止工作-定义“停止工作”。当然,随着添加更多矩形,生成不重叠的随机矩形将变得更加困难。相关:为了获得更好的帮助,请尽快发布一个适当的说明您的问题。但到目前为止,您在
paintComponent
方法中做了太多的事情,例如创建新对象。但一旦我变大,它就会停止工作-define“停止工作”。当然,随着添加更多矩形,生成不重叠的随机矩形将变得更加困难。相关:
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    if(!init) {
        initialize();
        repaint();
    }
    width = getWidth();
    height = getHeight();
    g2d.clearRect(0, 0, width, height);

    g2d.drawLine(0, 0, width, 0);
    if(var.correctInput && !var.exOnce) {
        box = new Box[var.inputCount];
        for(int i = 0; i < var.inputCount; i++){
            box[i] = new Box(rng.nextInt(width - var.rectWidth), rng.nextInt(height - var.rectHeight), var.rectWidth, var.rectHeight);

            for(int j = 0; j < i; j++) {
                if(box[i].getRect().intersects(box[j].getRect()) ) {
                    box[i].setOverLap(true);
                    box[j].setOverLap(true);
                    //Here I would do something like: box[i] = new Box(random X, random Y, width, height); You get the Idea i hope ?
                }
            }
        }

        var.exOnce = true;
    }

    var.actualFilled = 0;
    for(int i = 0; var.correctInput && i < var.inputCount; i++) {
        box[i].update();
        box[i].draw(g2d);

        if(box[i].isFilled()) {
            var.actualFilled ++;
        }
    }


    g2d.setColor(new Color(150, 220, 255));
    g2d.fillRect(0, 0, 50, 50);

    g2d.setColor(Color.BLACK);
    g2d.drawRect(0, 0, 50, 50);
    drawMidString(g2d, Integer.toString(var.actualFilled), 25, 25, new Font("Futura", Font.PLAIN, 25));
}
int x, y;
int width, height;

Rectangle rect;

boolean isHovered = false, isLeftClicked = false, isRightClicked = false, isFilled = false, isOverLap = false;

//Static    
Color still = new Color(111, 111, 111);
Color hover = new Color(150, 150, 220);
Color left = new Color(255, 150, 150);
Color right = new Color(150, 255, 150);

Color currentColor = still;
Color currentHover = hover;

public void draw(Graphics2D g2d) {
   g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

   g2d.setColor(currentColor);
   if(isLeftClicked || isRightClicked || isOverLap) {
       g2d.fill(rect);
       g2d.setColor(still);
       g2d.draw(rect);

       isFilled = true;
   }else {
       g2d.draw(rect);
   }
}

public Box(int x, int y, int width, int height) {
   this.x = x;
   this.y = y;
   this.width = width;
   this.height = height;

   rect = new Rectangle(this.x, this.y, this.width, this.height);
}

public void update() {
   currentHover = isLeftClicked ? left : isRightClicked ? right : hover;
   currentColor = isHovered ? currentHover : isLeftClicked ? left : isRightClicked ? right : isOverLap ? Color.RED : still;

   isFilled = isLeftClicked || isRightClicked;

   rect = new Rectangle(this.x, this.y, this.width, this.height);
}
while(var.box.length < var.inputCount) {
    temp = new Box(rng.nextInt(width - var.inputSize), rng.nextInt(height - var.inputSize), var.inputSize, var.inputSize, var.box.length+1);
    iterationCount++;

    for(int j = 0; j < var.box.length; j++) {
        next = var.box[j];

        isOverLap = temp.getRect().intersects(next.getRect());
        isUnderUI = temp.getRect().intersects(new Rectangle(0, height - 65, 300, 65));
        if(!var.excludeUI) {
            isUnderUI = false;
        }

        if(isOverLap || isUnderUI) break;
    }

    if(!isOverLap && !isUnderUI) {
        var.box = addToArray(var.box, temp);
    }

    if(iterationCount >= var.maxIterations) {
        finished = false;

        break;
    }else {
        finished = true;
    }
}