簡體   English   中英

在 Java 中的 JPanel 上繪制隨機非重疊矩形

[英]Drawing Random non-Overlapping Rectangles on JPanel in Java

我想在 Java Swing 面板中繪制 n 個矩形。 當我搜索那個問題時,我只找到了需要拆分矩形的選項(如果我理解正確的話)。 我希望能夠設置固定的寬度和高度,並且只隨機化 X 和 Y 坐標。 我已經編寫了一些代碼,它適用於少量的矩形(<20),但是一旦我 go 變大,它就會停止工作。 我包含的圖片顯示我能夠顯示哪些矩形相交以及有多少相交。

這是我的用戶界面

這就是我定義“停止工作”的方式

如您所見,矩形 18 和 2 重疊

這是我的代碼(僅 JPanel 中的 paintComponent()):

解釋一下:正確的輸入是 boolean,它顯示我的輸入是否正確,並且 execOnce 只是在按下按鈕時使代碼運行一次。

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

然后這里是 Class 框(基本上是一個矩形 Class 具有更多屬性,如顏色等......):

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

謝謝您的幫助!

編輯:我知道已經更正了我的代碼。 我看到了這個視頻 那里的人使用 p5.js,我在 Java 中編寫了代碼。 這是更新后的代碼(我沒有更改 Box 類):

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

如果您有興趣,我在我的 Github 上發布了源代碼和可運行的JAR

這是我現在的 UI 的圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM