1

I need to create a loop which draws rectangles in a vertical line with alternating color (e.g. black, white, black, white). Can anyone tell me how this is done?

I have tried numerous ways but can't seem to get the loop to work, thanks.

for (int x = 0; x>10;x++) {
    int y= 180;
    graph2D.drawRect(170, y, 20, 50);
    y = y + 45;
}

This is what I have, it won't draw the rectangles for some reason and I cant get it to alternate colors.

3
  • We probably need to see more code since it isn't working. Are you able to draw a single rectangle, without the loop? Commented Apr 18, 2016 at 16:16
  • To start, you are re-initializing y each time you enter the loop so the squares are drawing on top of each other. Try moving int y = 180; before the loop and see what you get. From there you can work on colors and the rest Commented Apr 18, 2016 at 16:18
  • String color = x%2 == 0 ? "black" : "white". Something like that with color changes. Commented Apr 18, 2016 at 16:20

2 Answers 2

4

You've got a few issues here.

Your for loop will not perform any iterations, as your condition is x > 10 instead of x < 10.

Change the first line from:

for (int x = 0; x>10;x++){

To:

for (int x = 0; x < 10; x++) {

Also, you reset y to 180 every iteration, so once your loop does start, all of the rectangles will be drawn on top of each other. Declare y outside of the loop, or use x to calculate the rectangles position.

Either like this:

int y = 180;
for (int x = 0; x < 10; x++) {
    graph2D.drawRect(170, y, 20, 50);
    y = y + 45;
}

Or like this:

for (int x = 0; x < 10; x++) {
    graph2D.drawRect(170, (x * 45) + 180, 20, 50);
}

The above math (x * 45) + 180 is a super simple way of saying that the first rectangle will be at (x * 45) + 180 = 0 + 180 = 180, the second will be at (x * 45) + 180 = 45 + 180 = 225 and so on and so on.

To change the color of the rectangles, you'll need to make a list or array of Colors, and use a different Color from the list, in each iteration.

//Make the list
Color[] colors = {Color.black, Color.blue, Color.cyan, Color.darkGray,
                  Color.green, Color.lightGray, Color.magenta, Color.magenta,
                  Color.orange, Color.pink, Color.red, Color.white, Color.yellow};

//Draw each rectangle
for (int x = 0; x < 10; x++) {

    //Change the color
    g.setColor(colors[x]);

    //Draw the rectangle
    graph2D.drawRect(170, (x * 45) + 180, 20, 50);
}

Of course if you want the colors to be random, you can look into using the Random class, and generating a random number between 0 and the length of your colors array. Also note that I use x as an index for the colors array, and if your loop increments x to be higher than the number of colors in the array, you'll get an ArrayIndexOutOfBoundsException.

I also assumed you named your instance of Graphics as g, since it is done that way most of the time.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Matthew, how would I make it so that the rectangles are filled in the loop instead of just the outlines changing colour? I didnt think of using an array for it so thanks for that
No problem, to make the rectangles filled in simple use the method fillRect rather than drawRect. It takes the same parameters as drawRect so all you have to do is change the name.
2

Why do you use the y variable instead of x that you are looping though?

@Override
public void paint(Graphics graph2D) {
    for (int y=0; y<10; y++) {
        int height = 50;
        if (y%2==0) {
            graph2D.setColor(Color.white);
        } else {
            graph2D.setColor(Color.black);
        }
        graph2D.fillRect(170, 180 + y*height, 20, 50);
    }
}

Also mind the difference while drawing a rectangle:

  • .drawRect(..) draws the border of a rectangle.
  • .fillRect(..) draws the rectangle itself.

In case you want to switch between black and white color, change the color with every loop. Every even number y%2 == 0 of loop will have the one color, otherwise the second one (also mentioned in the code above):

if (y%2==0) {
    graph2D.setColor(Color.white);
} else {
    graph2D.setColor(Color.black);
}

4 Comments

static final int may be slightly better than simply int - it is a compile-time constant, better not leave it to the JVM to recognize that.
@SándorMátyásMárton Where are you talking about?
@MatthewCliatt It was a side remark about height, the loop variable is not a compile time constant of course :)
@SándorMátyásMárton Oh of course, I don't know why I didn't immediately realize what you meant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.