PDA

View Full Version : Problem with printing circles



scrum
12-07-2007, 08:01 AM
int width = getWidth();
int height = getHeight();

int counter = 0;
int counter2 = 0;




while (counter < 7)
{
g.drawOval(width*counter/8 ,0, width*(counter+1)/8 ,height/8);

counter++;
}



I'm trying to print a row if 8 cirlces the same size along the top of a jpane, but it doesn't work.

the first circle prints
the second circles starts at the right place but streches out to double where it should be


the third also starts at the right place but stretches out weird again.

Master_script_maker
12-21-2007, 02:07 AM
can you show a screenshot?

Twey
12-21-2007, 03:42 PM
You're forgetting your basic arithmetic :) Division is applied before subtraction, so you're doing width * (counter / 8) which is always zero (counter being an int). You want (width * counter) / 8. Also, here you have an initialiser, a test, and an incrementor, which is what for loops are for, although to optimise we can put the incrementation and the addition steps together:
int width = getWidth(), height = getHeight();

for(int i = 0; i < 8; )
g.drawOval((width * i) / 8, 0, (width * (++i)) / 8, height / 8);Also note that because you started from zero, if you want to execute the loop eight times you must loop until i < 8 or i <= 7. i < 7 will only execute seven times.

jessy01
07-25-2008, 07:38 AM
Hi, i am new to this forum.
This topic is also new to me.
This kind of forums are really helpful for those who are working on java.
It is a very informative site.

===============================
Jessy

Wide Circles (http://www.widecircles.biz)