Search code examples
javaandroidcanvaslogicrect

Drawing multiple rectangles in different directions android issue


Ello, kind ladies and gentle mans. I got stuck around this issue that i have and any help would be much appreciated. I will do my best to explain what is going on exactly, hopefully you will be able to help me, so here it goes : Basically i am drawing some walls and sending them on a server, there are two things that i am sending, width and height. Width or height is wallThickness dependant on the wall direction, if it is vertical width= wallThickness, if horizontal height = wallThickness. Now i need to draw those walls in a form of a Rect in a separate app, first one starts at view location 0,0 and every next one should attach itself to the end of the previous wall and go in right direction. I dont have an issue when Rect(wall) is drawn from top to bottom or left to right enter image description here

i figure because i managed to get correct starting point for those Rects. The question is how to draw Rects that need to be drawn from bottom to up, or right to left like this: enter image description here

this is the method that feeds the canvas with Rect keep in mind that first startX and Y are 0,0 and that widths and heights that the method recieves are in a correct order, for example if we have 4 walls it will go from 1 to 4.

public void drawWall (int width,int height,int wallListSize){

    wall = new Rect();
    wall.set(startX,startY,width+startX,height+startY);

    wallList.add(wall);

    Log.i(TAG, "drawWall: Rect bottom:"+wall.bottom+" right:"+wall.right+" top:"+wall.top+" left:"+wall.left);

    //setting up starting position for next wall
    if (chooseDirection(width)){
        //vertical
        if (wall.bottom>startY){
            // up to down stroke
            Log.i(TAG, "drawWall: up to down stroke");
            startY = wall.bottom;
            startX = wall.left;

        }else {
            //down to up stroke
            Log.i(TAG, "drawWall: down to up stroke");
            startY = wall.top;
            startX = wall.right;

           // startX = wall
        }
    }else {
        //horizontal
        if (wall.right>startX){
            //left to right stroke
            Log.i(TAG, "drawWall: left to right stroke");
            startX = wall.right;
            startY = wall.bottom;
        }else {
            //right to left stroke
            Log.i(TAG, "drawWall: right to left stroke");
            startX = wall.left;
            startY = wall.top;

        }
    }
    //Checking if there is more walls or not
    Log.i(TAG, "drawWall: ---STARTING POSITION--- startX "+startX+" startY "+startY+" ");
    if (wallIndex < wallListSize){
        Log.i(TAG, "drawWall: incrementing index");
        wallIndex++;

    }else {
        Log.i(TAG, "drawWall: end of drawing, matching layout to wrap content");
        setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT));
    }
}

private boolean chooseDirection(int width){
    if (width == wallThickness){
        return true;
    }else {
        return false;
    }
}

Solution

  • Found the answer, had to take a different approach when deciding what kind of stroke it was. able to get it if i send starting and ending points from server.

     public void drawWall (int width,int height,int wallListSize,int x1,int x2,int y1,int y2){
    
        wall = new Rect();
        //setting up starting position for next wall
        if (chooseDirection(width)){
            //vertical
            if (y1<y2){
                // up to down stroke
    
                wall.set(startX,startY,wallThickness+startX,height+startY);
                startY = wall.height();
               // startX=startX+wallThickness;
                Log.i(TAG, "drawWall: up to down stroke");
    
    
            }else  {
                //down to up stroke
    
                wall.set(startX,height-startY,wallThickness+startX,startY);
                startY = startY - wall.height();
               // startX = startX + wallThickness;
                Log.i(TAG, "drawWall: down to up stroke");
    
    
               // startX = wall
            }
        }else {
            //horizontal
            if (x1<x2){
                //left to right stroke
                Log.i(TAG, "drawWall: left to right stroke");
                wall.set(startX,startY,width+startX,wallThickness+startY);
                startX = wall.width();
               // startY = startY+wallThickness;
            }else {
                //right to left stroke
                wall.set(width-startX,startY,startX,wallThickness+startY);
                Log.i(TAG, "drawWall: right to left stroke");
                startX = startX-wall.width();
               // startY = startY+wallThickness;
            }
        }
        //adding wall to the pool
        wallList.add(wall);
    
        //Checking if there is more walls or not
        Log.i(TAG, "drawWall: Rect bottom:"+wall.bottom+" right:"+wall.right+" top:"+wall.top+" left:"+wall.left);
        Log.i(TAG, "drawWall: ---STARTING POSITION FOR NEXT WALL--- startX "+startX+" startY "+startY+" ");
        if (wallIndex < wallListSize){
            wallIndex++;
    
        }else {
            Log.i(TAG, "drawWall: end of drawing, matching layout to wrap content");
            startDraw = true;
            invalidate();
        }
    }