Click to See Complete Forum and Search --> : [RESOLVED] Decrementing


genics
11-21-2007, 07:38 AM
Hey folks,

I'm trying get objects in a drawing window to move around and I've encountered some problems.

Here's my code:


import element.*;
import java.awt.Color;

public class attemp4
{
public static void main(String[] args)
{
DrawingWindow drawWin = new DrawingWindow(500,500);

for(int i = 0; i < 500; i++)
{
Rect box = new Rect(0,i,100,1);
drawWin.setForeground(Color.blue);
drawWin.fill(box);
waitNseconds(1);
}

for(int i = 500; i > 0; ++i)
{
Rect box2 = new Rect(100,i,100,100);
drawWin.setForeground(Color.yellow);
drawWin.fill(box2);
waitNseconds(1);
}
}

public static void wait1second()
{
long now = System.currentTimeMillis();
long then = now + 1;
while (System.currentTimeMillis() < then){ }
}

public static void waitNseconds(int num)
{
for (int i= 0; i < num; i++)
{
wait1second();
}
}
}


Basically the first for loop makes the first rectangle go down the page correctly, however I want the second rectangle (the yellow one) to go up instead of down.

How would I go about doing this properly?

Cheers!

jasonahoule
11-21-2007, 09:29 AM
What you have in your first loop is a post increment. The second is a pre increment. The difference is in a post increment your calculations are performed and then the value is incremented. The opposite goes for pre-increment.

Decrements would use the -- operator and the same applies here for pre and post.

for(int i=500; i > 0; i--) {...}

genics
11-21-2007, 10:35 AM
Works a treat, thanks! :)