peter paul
peter paul

Reputation: 1

how to slowly display circles one by one?

I am new to coding. I am writing a practice app and trying to make it display circles one by one on screen. here is my code below. but it only can display all the circles together at one time. How can I fix it?

public class DrawCircle extends View
{
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
int color;
int x=0;
int y=0;
int r=0;
int n=0;

public DrawCircle(Context con, AttributeSet attr)
{
    super(con,attr);
}

@Override
protected void onDraw(Canvas c)
{
    super.onDraw(c);
    try
    {
        while(n<10)
        {
            gen();
            p.setColor(color);
            p.setStyle(Paint.Style.FILL);
            c.drawCircle(x, y, r, p);
            Thread.sleep(100);
            n++;
        }
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
}

public void gen()
{
    color = Color.rgb(rand(0,255),rand(0,255),rand(0,255));
    x=rand(0,1200);
    y=rand(0,1200);
    r=rand(50,150);
}

public int rand(int a, int b)
{
    return((int)((b-a+1)*Math.random() + a));
}
}

Upvotes: 0

Views: 60

Answers (1)

Piaget Hadzizi
Piaget Hadzizi

Reputation: 835

You are using one paint object. Can you change your code to initialise paint inside the while loop:

while(n<10)
        {
            gen();
            p = new Paint(Paint.ANTI_ALIAS_FLAG);//ADD THIS LINE HERE
            p.setColor(color);
            p.setStyle(Paint.Style.FILL);
            c.drawCircle(x, y, r, p);
            Thread.sleep(100);
            n++;
        }

Upvotes: 1

Related Questions