0

I need to create a class that displays 10 rectangles on the canvas, each with a random color and position. When it reaches 11, the first rectangle is replaced with a new random color and position. 12th rect replaces the 2nd box, and so on. I am using the acm.jar for this, http://jtf.acm.org/javadoc/student/index.html.

import acm.graphics.*;
import acm.program.*;
import java.awt.Color;
import java.util.Random;

public class Rect extends GraphicsProgram 
{
    public void run()
    {
        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 x = random.nextInt(width-boxWidth);
            int y = random.nextInt(height-boxWidth);  
            Color c = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));

                GRect r = new GRect(x, y, boxWidth, boxWidth);
                r.setFilled(true);
                r.setLocation(x, y);
                r.setFillColor(c);
                this.add(r);                   

                this.pause(100);

        }




    }
}

I already figured out how to make the colors random, I cant figure out how I will substitute the boxes with the old ones.

EDIT:::--------------------------------------------------------------------------------

I did manage to get it working with the help of the guys below. Here is what the new for loop looks like:

for(;;) {
            int x = random.nextInt(width-boxWidth);
            int y = random.nextInt(height-boxWidth);  
            Color c = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));

                GRect r = new GRect(boxWidth, boxWidth);
                r.setFilled(true);
                r.setLocation(x, y);
                r.setFillColor(c);                
                add(r, x, y);

                int n = getElementCount();
                if (n>maxBoxes)
                {
                    remove(getElement(0));
                }

                this.pause(100);

        }

One thing I dont understand is why the remove(getElement(0)) works,, how does the element change its index once one is removed? If I have 10 elements 0-9, and I remove element(0) why does the other elements change its index?

4

2 回答 2

1

You need to store the list of rectangles drawn so far. Every time you add a new rectangle, if the list is already 10 rectangles long, remove the first rectangle and add a new one. Then you need to redraw ALL rectangles every time you refresh the display, using double buffering to prevent screen flickering.

于 2012-11-12T15:33:53.093 回答
1

This really looks like homework , so I won't do it for you but give some clues.

You can use the getElementCount() method to know the current number of rectangles in your frame.

Create a list of GObjects, and populate it with your rectangles as you create them. Once you reach ten, the process becomes

  • remove rectangle from screen using remove(GObject gobj)
  • remove first element, add to end of list.

And here you are :)

于 2012-11-12T15:35:38.760 回答