2011-12-08 2 views
0

Java에서 BufferedImage 클래스의 페인트 통 도구처럼 작동하는 함수를 작성했습니다. 채우기를 수행하기 위해 재귀를 사용합니다. 안타깝게도, 코드를 실행하면 java.lang.StackOverflowError가 발생합니다. 또한 System.out.println을 사용하여 BaseColor의 "빨간색"색상 채널을 검사했을 때 255가 있어야하는 곳에 제로가 부여 된 것과 같이 BaseColor를 인식하지 못한다는 사실을 발견했습니다. (색상은 . 왜 이런 일StackOverflowError Java "Fill Image"함수에서

public static void BufferedImageFill(BufferedImage bufferedImage, int FillX, int FillY, int FillRed, int FillGreen, int FillBlue, int FillAlpha, int Tolerance, boolean IsFirstPixel, Color BaseColor) { 
    if (IsFirstPixel == true) { 
     BaseColor = new Color(RGBAValuesToInt(BufferedImageGetPixelARGB(bufferedImage, "R", FillX, FillY), BufferedImageGetPixelARGB(bufferedImage, "G", FillX, FillY), BufferedImageGetPixelARGB(bufferedImage, "B", FillX, FillY), BufferedImageGetPixelARGB(bufferedImage, "A", FillX, FillY))); 
    } 
    if (FillX >= 0 && FillY >= 0 && FillX < bufferedImage.getWidth() && FillY < bufferedImage.getHeight()) { 
     int ThisR = BufferedImageGetPixelARGB(bufferedImage, "R", FillX, FillY); 
     int ThisG = BufferedImageGetPixelARGB(bufferedImage, "G", FillX, FillY); 
     int ThisB = BufferedImageGetPixelARGB(bufferedImage, "B", FillX, FillY); 
     if (Math.abs(ThisR - BaseColor.getRed()) <= Tolerance && Math.abs(ThisG - BaseColor.getGreen()) <= Tolerance && Math.abs(ThisB - BaseColor.getBlue()) <= Tolerance) { 
      bufferedImage.setRGB(FillX, FillY, RGBAValuesToInt(FillRed, FillGreen, FillBlue, FillAlpha)); 
      BufferedImageFill(bufferedImage, FillX - 1, FillY - 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
      BufferedImageFill(bufferedImage, FillX - 1, FillY, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
      BufferedImageFill(bufferedImage, FillX - 1, FillY + 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
      BufferedImageFill(bufferedImage, FillX, FillY + 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
      BufferedImageFill(bufferedImage, FillX, FillY - 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
      BufferedImageFill(bufferedImage, FillX + 1, FillY - 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
      BufferedImageFill(bufferedImage, FillX + 1, FillY, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
      BufferedImageFill(bufferedImage, FillX + 1, FillY + 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor); 
     } 
    } 
} 

아무도 알고 있나요 : 흰색는) 다음 코드인가? 주어진 도움 주셔서 감사!

답변

2

코드를 올바르게 읽으면 픽셀 (0, 0)을 채우기 위해 메서드를 호출하면 일부 지점에서 픽셀 (1, 0)을 채우기 위해 호출됩니다. 그 호출은 차례대로 픽셀 (0, 0)을 다시 채우도록 호출합니다. 그래서 당신은 무한 재귀를 가지고 있습니다. (동일한 문제가 다른 인접한 점과 함께 발생합니다. — 각각이 돌아가서 채워지는 점을 채 웁니다.)

+0

이제 ... 통찰력을 가져 주셔서 감사합니다! – neilf

1

-Neil 그럼 당신은 당신의 재귀를 중지하는 행을하지 않는 것. from과 to로 그림을 그리는 색상이 허용차 내에 있으면 픽셀을 무한히 페인트하여 결국 스택을 오버플로합니다. 이미 올바른 색인지 확인하고 그럴 경우 반환하십시오.

관련 문제