1

voidを返す再帰メソッドを定義しました(少なくとも再帰的だと思います)。別のメソッドでそれを呼び出したいのですが、方法がわかりません。非常に基本的なことはわかっていますが、誰か助けてもらえますか?ありがとう。

再帰的な方法:

private static void recursiveWhiteToBlack(BufferedImage image, int width, int height){
    image.getRaster().setPixel(width,height, new int [] {0, 0, 0, 0, 0, 0});        
    int[][] neighbors = neighborsXY(width,height);

    for(int i = 0; i<neighbors.length; i++){
        int neighborX = neighbors[i][0];
        int neighborY = neighbors[i][1];
        int[] neighborColor = image.getRaster().getPixel(neighborX, neighborY, new int[] {0, 0, 0, 0, 0, 0});

        if(neighborColor[0] == 1){
            recursiveWhiteToBlack(image, neighborX, neighborY);
        }   
    }   
}

それを呼び出す:

public static BufferedImage countObjects(BufferedImage image, BufferedImage original, ComponentPanel panel){
      BufferedImage target = copyImage(image);

      for(int width=1; width<image.getRaster().getWidth()-1; width++){ //Determine the dimensions for the width (x)         

          for(int height=1; height<image.getRaster().getHeight()-1; height++){ //Determine the dimensions for the height (y)

              int[] pixel = image.getRaster().getPixel(width, height, new int[] {0, 0, 0, 0, 0, 0});

              if(pixel[0] == 1){                      
                   none = recursiveWhitetoBlack(image, width, height);  //HOW TO CALL IT HERE!!!//

              }

      System.out.println("countObjects method called");
        return target;

    }   
4

3 に答える 3

0

あなたはそれをこのように呼びます:

if(pixel[0] == 1){                      
     recursiveWhitetoBlack(image, width, height);
}

メソッドには戻り型がないため、変数を割り当てる必要はありません。

于 2012-12-10T18:30:38.953 に答える
0

メソッドがvoidを返すので削除none =します(実際には何も返さないことを意味します)

したがって、これは次のようになります。

if(pixel[0] == 1){                      
    recursiveWhitetoBlack(image, width, height);  

}

またnone、変数/メンバーとして定義されていないため、使用することは無効であることに注意してください。

于 2012-12-10T18:30:50.677 に答える
0

これは問題になる可能性があります。本当の停止条件があるかどうかわかりません。メモリ不足エラーが発生するとすぐにわかります。

于 2012-12-10T18:31:32.383 に答える