Search code examples
androidpositionheightwidthrect

On Android, how to draw a Rect like pygame Rect? (x, y, w, h)


Is there any way of drawing a Rect in Android in the following pattern, instead of "left, top, right, bottom"? The problem is that I'm using random coordinates:

Rect e = new Rect(random.nextInt(300)+20,20,random.nextInt(300)+45,70);

And it's hard to keep track of the first left random position to include in the right one to the width. In the "x, y, w, h", it's easy to apply my collision test:

if ((((p.left+p.width())>e.left)&&(p.left<(e.left+e.width())))&&
       (((p.top+p.height())>e.top)&&(p.top<(e.top+e.height())))) {
    gameScreen = 2;
}

Solution

  • Just move the random numbers outside of the constructor for your rect.

    int left = random.nextInt(300)+20;
    int right = left + 20;
    int top = random.nextInt(300)+45;
    int bottom =  top + 70;
    
    Rect e = new Rect(left, top, right, bottom);