如何删除一个框以继续 java 中的'for'循环?



我是Java的新手,我正在尝试编写一个代码,该代码生成10个随机框,然后删除一个框并添加另一个框。因此,总计保留了10个框,但是循环持续下去。我已经弄清楚了如何创建10个随机框,但是我不确定如何从中删除一个框。这是代码:

final int width = 800;
        final int height = 600;
        final int boxWidth = 50;
        final int maxBoxes = 10;
        this.setSize(width, height);
            Random random = new Random();
                for(int box=0;box<maxBoxes;box++)
                {
                    int x = random.nextInt(width-boxWidth);
                    int y = random.nextInt(height-boxWidth);
                    GRect r = new GRect(x, y, boxWidth, boxWidth);
                    Color c = new Color(random.nextInt(256),
                                        random.nextInt(256), random.nextInt(256));
                    r.setFilled(true);
                    r.setFillColor(c);
                    this.add(r);
                    this.pause(100);

您需要将框值存储在数组中。然后,在循环的每次迭代中,删除了屏幕,并重新绘制了所有10个框。然后随机替换一个框的框值并重复。

最新更新