1

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 my UI

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!

Steini
  • 13
  • 3
  • 1
    For better help sooner post a proper [mcve] that demonstrates your issue. But so far, you're doing too many things in the `paintComponent` method, such as creating new objects. – Frakcool Sep 23 '19 at 16:21
  • *but as soon as I go bigger, it stops working* - define "stops working". Of course as you add more rectangles it will be harder to generate random rectangle that don't overlap. – camickr Sep 24 '19 at 01:09
  • related: https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other – Ray Tayek Sep 24 '19 at 22:20

1 Answers1

0

EDIT: I know have corrected my Code. I saw this video. The guy there used p5.js and I wrote the Code in Java. This is the updated Code (I did not change the Box Class):

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

If you are interested, i posted the Source Code and a runnable JAR on my Github

Here is a Picture of my UI now

Steini
  • 13
  • 3