Zeke P
Zeke P

Reputation: 7

While Loop for Drawing Circle

So far this is the code I have I previously made a for loop with the same code to create a 100 random circles, but now I need to use the while statement and just a little confused on what integers to use and what not. I compile it without any errors but when it runs the window pops up with nothing in it....

public void paintComponent(Graphics g) {
super.paintComponent(g);

Dimension d = getSize();
{
int i = 0;
while (i<=100)                        
{    
    Color color = new Color(generator.nextInt(255),
        generator.nextInt(255), generator.nextInt(255));
    g.setColor(color);

    int circleSize = generator.nextInt(d.width / 4);
    int x = generator.nextInt(d.width - circleSize);
    int y = generator.nextInt(d.height - circleSize);
    g.fillOval(x, y, circleSize, circleSize);
    g.drawArc(x, y, circleSize, circleSize, 0, 360);
    }
}

Upvotes: 0

Views: 3010

Answers (1)

Vivin Paliath
Vivin Paliath

Reputation: 95588

You're in an infinite loop. You aren't incrementing i. Add i++; to the bottom of your loop and the loop should at least terminate.

Upvotes: 4

Related Questions