2012-03-01 7 views
0

Я написал метод, который заполняет растровое изображение, представленное матрицей m x n. То, что я пытаюсь сделать, - протолкнуть исходный пиксель в стек, затем в цикле while вытащите элемент из стека, покрасьте его и нажимайте соседние пиксели, если они имеют тот же цвет, что и исходный цвет исходного пикселя.Стек держит один и тот же элемент

public void fill(int x, int y, char c) { 
    char tempColor = this.bitmap[y - 1][x - 1]; 
    Point currentPoint; 

    Stack<Point> fillStack = new Stack<Point>(); 

    fillStack.push(new Point(x, y)); 

    do { 
     currentPoint = fillStack.pop(); 
//  System.out.println(currentPoint.x + " " + currentPoint.y); 
//  System.out.println("Current state of the stack:"); 
//  for (Point p: fillStack) 
//   System.out.println(p.x + " " + p.y); 
     this.bitmap[currentPoint.y - 1][currentPoint.x - 1] = c; 
     if (currentPoint.y - 1 > 0 && this.bitmap[currentPoint.y - 2][currentPoint.x - 1] == tempColor) { 
      fillStack.push(new Point(x, y - 1)); 
//   System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1)); 
     } 
     if (currentPoint.y - 1 < n - 1 && this.bitmap[currentPoint.y][currentPoint.x - 1] == tempColor) { 
      fillStack.push(new Point(x, y + 1)); 
//   System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y + 1)); 
     } 
     if (currentPoint.x - 1 > 0 && this.bitmap[currentPoint.y - 1][currentPoint.x - 2] == tempColor) { 
      fillStack.push(new Point(x - 1, y)); 
//   System.out.println("Pushing " + (currentPoint.x - 1) + " " + currentPoint.y); 
     } 
     if (currentPoint.x - 1 < m - 1 && this.bitmap[currentPoint.y - 1][currentPoint.x] == tempColor) { 
      fillStack.push(new Point(x + 1, y));  
//   System.out.println("Pushing " + (currentPoint.x + 1) + " " + currentPoint.y); 
      } 
     } while (!fillStack.isEmpty()); 
    } 
} 

Но это не работает по какой-то причине, я не могу заметить. Выход (отладки линии раскомментирована) выглядит следующим образом:

 
3 3 
Current state of the stack: 
Pushing 3 2 
Pushing 3 4 
Pushing 4 3 
4 3 
Current state of the stack: 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
Pushing 4 2 
Pushing 4 4 
Pushing 5 3 
4 3 
Current state of the stack: 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 
3 2 
3 4 

... и это продолжается, как это в бесконечном цикле. В чем проблема?

ответ

3

Ваши заявления на печать говорят одно, ваш код делает другое! ;)

, например:

fillStack.push(new Point(x, y - 1)); 
System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1)); 

Смотрите, если вы можете заметить разницу ...

+0

Ой, я не могу поверить, что я смотрел на эту вещь в течение часа и не мог Я вижу это. Спасибо: D – hattenn

+2

@hatten - ха-ха, иногда все, что вам нужно, это еще одна пара глаз ... – Nim

Смежные вопросы