2017-06-19 44 views

回答

2

你可以用颜色填充算法。

见链接:
Flood Fill Algorithm

private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){ 
Queue<Point> q = new LinkedList<Point>(); 
q.add(pt); 
while (q.size() > 0) { 
    Point n = q.poll(); 
    if (bmp.getPixel(n.x, n.y) != targetColor) 
     continue; 

    Point w = n, e = new Point(n.x + 1, n.y); 
    while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) { 
     bmp.setPixel(w.x, w.y, replacementColor); 
     if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor)) 
      q.add(new Point(w.x, w.y - 1)); 
     if ((w.y < bmp.getHeight() - 1) 
       && (bmp.getPixel(w.x, w.y + 1) == targetColor)) 
      q.add(new Point(w.x, w.y + 1)); 
     w.x--; 
    } 
    while ((e.x < bmp.getWidth() - 1) 
      && (bmp.getPixel(e.x, e.y) == targetColor)) { 
     bmp.setPixel(e.x, e.y, replacementColor); 

     if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor)) 
      q.add(new Point(e.x, e.y - 1)); 
     if ((e.y < bmp.getHeight() - 1) 
       && (bmp.getPixel(e.x, e.y + 1) == targetColor)) 
      q.add(new Point(e.x, e.y + 1)); 
     e.x++; 
    } 
}} 
+0

谢谢Chetan先生,但是你能否澄清一下,它是否会填满一个点击的形状,或者它将不得不通过触摸屏来填充它? –

+0

它将填满边界下的全部区域。点击任何形状后。 –

+0

谢谢..将尽力实施 –

相关问题