[solved]fillRect() in loop?

public void paint(Graphics2D g) {

            g.fillRect(x, 10, WIDTH, HEIGHT);

            g.fillRect(x - 51, 10, WIDTH, HEIGHT);

            g.fillRect(x - 102, 10, WIDTH, HEIGHT);

            g.fillRect(x - 153, 10, WIDTH, HEIGHT);

            g.fillRect(x - 204, 10, WIDTH, HEIGHT);

    }

i tried:

for (int x = 230; x > 0; x-=51)
         {
               g.fillRect(x, 10, WIDTH, HEIGHT);
          }

But it is not working.

Is there any way to make my code shorter?


230-51 = 279
279-51 = 228
228-51 = 177
177-51 = 126
126-51 = 75
75-51  = 24

It’s a math problem.

And by the way, what is “not working”?

Okay, let’s see…

Here, you used ‘x’ as the position of the x-axis to create some cubes.

While here, you used ‘x’ again to control exact positioning. I think you may have overridden your variable. Try this code.

Try to use a new index for for loops when you iterate them.

Edit: wrong direction :stuck_out_tongue:

is

[quote]for (x = 230; x > 0; x-=51) {
g.fillRect(x, 10, W, H);}
[/quote]
not equal to

for(int i = 230; i < 0; i -= 51){
          
            g.fillRect(x - i, 10, WIDTH, HEIGHT);}

Well, only if you aren’t using x higher up the chain. The way it looks like, you are trying to position blocks. You need 2 variables to do that effectively in your case. X controls the x axis start position, I controls the rest of the blocks in the row. If you post more code, it will help me help you out a lot more.

Oh I don`t know what is the problem but I tried the code that you said.

public void paint(Graphics2D g) {
        for(int i = 0; i < 300; i+=51) {
            g.fillRect(x - i, Y, WIDTH, HEIGHT);
        }
    }

Thanks sir.