当坐标cx / cy与矩形/ s相交时,我一直在尝试编写代码,矩形会改变颜色。 这一直在推动我。 这是我的矩形代码。
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; }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; }最满意答案
您是否试图给矩形充气,并在交叉点发生变化时改变颜色?
请注意, left , right , top和bottom的值实际上永远不会改变,因此如果在循环的第一次迭代中没有触发检查,那么它永远不会。
另外,请注意,在它相交的情况下,它会将颜色设置为DKGRAY,但在下一个循环中再将其直接设置回WHITE。 这是你想要的吗?
我认为你的意思是做这样的事情。 在这里,我们将与实际变化值进行比较。
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; }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; }更多推荐
发布评论