I want to draw n Numbers of Rectangles into a Java Swing Panel. When I searched for that Problem, I only found options, that require the Rectangle to be split (if I understood correctly). I want to be able to set a fixed Width and Height and only randomize the X and Y Coordinates. I have written some Code that works well with small amounts of Rectangles (<20) but as soon as I go bigger, it stops working. The Picture I included shows that I was able to show which Rectangles are intersecting and how many are intersecting.
This is how i define "stops working"
As you can see, the Rectangles 18 and 2 Overlap
Here is my Code for that (Only the paintComponent() in JPanel):
To Explain: the correctInput is a boolean that shows if my Input is correct and the execOnce is just there to make the Code run once when a Button is pressed.
@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));
}
And then here is the Class Box (which is basically a Rectangle Class with some more Attributes like Color etc...):
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);
}
Thank you for your Help!
