2017-06-19 45 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

謝謝..將盡力實施 –

相關問題