tags:

views:

41

answers:

1

I am trying to draw a cupola circles at random positions in an Android application. I draw them on a bitmap and then draw that bitmap on the canvas. This is the function where a draw the circles:

private void drawRandomCircles(int numOfCircles) {
    Canvas c = new Canvas(b);
    Paint cPaint = new Paint;
    cPaitn.setColor(Color.RED);
    for(int i = 0; i < numOfCircles; i++) {
        int x = Math.Random % 100;
        int y = Math.Random % 100;
        c.drawCircle(x, y, 20, cPaint)  
    }
}

The Bitmap b is global. And after calling this function I just draw the bitmap in the onDraw method. Now the problem is that I only get one circle drawn on the screen, no matter the size of numOfCircles.

Any clue what is happening here?

+1  A: 

That code doesn't even compile. What is new Paint; for instance?

I suggest you log your arguments to drawCircle to make sure you draw them on different locations. If Math.Random for instance is a field, it would change in between reads, which would put the circles on top of each other.

If you intended to write Math.random() the error is that Math.random() returns a value between 0 and 1. You may want to use

Random r = new Random();
// your loop
    int x = r.nextInt(100);
    int y = r.nextInt(100);
aioobe
First I would like to thank you for your answer and for the advice. The code doesn`t compile because I wrote it by hand instead of copying it form the source (I know it`s stupid).I changed the Math.random() calls with the Random class calls and that did the trick.Once more, thank you very much!
ViktorC