0

I've been trying to write code when the coordinates cx/cy intersect with a rectangle/s the rectangle changes color. This has been driving me up the wall. Here is my code for the rectangles.

for(int k = 0; k<=15; k++){
        k = k * 55;
        for(int i=0;i<=9;i++){
             i = i*55;  
             bounds.set(left+i,top+k,right+i,bottom+k);
             paint.setColor(Color.WHITE);
             canvas.drawRect(bounds, paint);
             if (cx == left || cx == right || cy==top|| cy == bottom){
                 paint.setColor(Color.DKGRAY);
                 canvas.drawRect(bounds, paint);
             }
             i=i/55;
            }    
        k = k/55;
    }   
Tommy
  • 99,986
  • 12
  • 185
  • 204
Mag
  • 11
  • 1
  • You could use the Rectangle class like this: Rectangle r1 = new Rectangle(x, y, width, height); ... if(r1.intersects(r2)).... – Bene Nov 30 '14 at 23:35

2 Answers2

0

Are you trying to inflate a rectangle, and change color when the intersection occurs?

Note that the values of left, right, top and bottom never actually change, so the if the check doesn't trigger in the first iteration of the loop, it never will.

Also, note that, in the case where it does intersect, it will set the color to DKGRAY, but then set it straight back to WHITE again on the next loop. Is this what you want?

I think you mean to do something like this. Here, we are comparing with the actual changing values.

for(int k = 0; k<=15; k++){
    k = k * 55;
    for(int i=0;i<=9;i++){
         i = i*55;  
         int boundsLeft = left + i;
         int boundsTop = top + k;
         int boundsRight = right + i;
         int boundsBottom = bottom + k;
         bounds.set(boundsLeft, boundsTop, boundsRight, boundsBottom);
         paint.setColor(Color.WHITE);
         canvas.drawRect(bounds, paint);
         if (cx == boundsLeft|| cx == boundsRight || cy==boundsTop || cy == boundsBottom ){
             paint.setColor(Color.DKGRAY);
             canvas.drawRect(bounds, paint);
         }
         i=i/55;
        }    
    k = k/55;
}   
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
  • I would like it to be a permanent rectangle by rectangle change. This is great input, it now runs row by row or column by column. If it runs left or right it changes the color change an entire column if it goes top to bottom it changes the color of the row to Dark Grey. – Mag Dec 01 '14 at 05:12
0
             for(int i=0;i<=9;i++){
             i = i*55;  
             int boundsLeft = left + i;
             int boundsTop = top + k;
             int boundsRight = right + i;
             int boundsBottom = bottom + k;
             bounds.set(boundsLeft, boundsTop, boundsRight, boundsBottom);
             if(boxes[k][i]==0){
                 paint.setColor(Color.GREEN);
                 canvas.drawRect(bounds, paint);
             }
             else{
                 paint.setColor(Color.DKGRAY);
                 canvas.drawRect(bounds, paint);
             }
             if (cx >= boundsLeft && cx <= boundsRight && cy>=boundsTop && cy <= boundsBottom ){
                 plow.set(boundsLeft, boundsTop, boundsRight, boundsBottom);
                 paint.setColor(Color.DKGRAY);
                 canvas.drawRect(plow, paint);
                 boxes[k][i]=1;
             }
              i=i/55;

            } 
Mag
  • 11
  • 1