“此方法不适用于参数”错误

时间:2012-12-01 21:17:36

标签: java syntax-error processing

任何人都知道我为什么会收到这个错误?本程序基本上显示图像,显示直方图或图像。然后调整每个像素的亮度,再次使用调整后的值显示图像和直方图。

错误是:

  

The method drawHistogram(int[], int, int) is not applicable for arguments (PImage, int[]);

PImage rocks;
PImage rocksBrightened;
PImage rocks2;
int rockBytes[][];
int rockBrights[][];

void setup() {  

  rocks = loadImage("rocks.png");
  size(rocks.width+256, rocks.height*2);
  rocks2 = createGraphics(rocks.width, rocks.height, P2D);
  rocks2.loadPixels();

  int h = rocks.height;
  size(rocks.width*2, rocks.height*2);
  image(rocks, 0, 0);
  rockBytes = new int[rocks.width][rocks.height];
  rockBrights = new int[rocks.width][rocks.height];
  for (int i = 0;i < rocks.width; i++) {  
    for (int j = 0; j < rocks.height; j++) {
    rockBytes[i][j] = rocks.get(i, j);
    } 
  }  
  for (int i = 0; i < rocks.width; i++) { 
    for (int j = 0; j < rocks.height; j++) { 
      rockBrights[i][j] = rocksBrightened.get(i, j); 
      }  
    } 
  drawHistogram(rocks, computeHistogram(rocks)); //THIS LINE CAUSES ERROR
  brighten(rockBrights);
  // makeImage(rocksBrightened, rockBrights);
  image(rocksBrightened, 0, h);
  }

void loadPixelArray(PImage anImage) { 
  anImage.loadPixels(); //load pixels into pixels[] array
  }

int[] computeHistogram (PImage anImage) {
  int histogram[] = new int[256];
  for (int i = anImage.pixels.length-1; i >= 0; --i) {
    int v = (int) brightness(anImage.pixels[i]);
    histogram[v]++;
  }
  int maxV = 0;
  for (int i = 0; i < 256; i++) {
    maxV = max(maxV,histogram[i]);
  }
  for (int i = 0; i < 256; i++) {
    histogram[i] = (int) (histogram[i]*100.0/maxV);
  }
  return histogram; 
}


void brighten(int anyArray[][]) { 

   for (int i = rocks.pixels.length-1; i >= 0; --i) {
      int v = rocks.pixels[i];
      float br = brightness(v);
      float r = constrain(br*-.004436+2.1308*red(v),0,255);
      float g = constrain(br*-.004436+2.1308*green(v),0,255);
      float b = constrain(br*-.004436+2.1308*blue(v),0,255);
      rocks2.pixels[i] = color(r,g,b);
      } 
  }

  void drawHistogram(int histogram[], int x, int y) {
  noStroke();
  fill(0);
  stroke(255);
  rect(x, y, 256, 256);
  for (int i = 0; i < 256; i++) {
    line(x + i, y+256, x + i, y+256 - histogram[i]);
  }
 }


 void makeImage(PImage anImage, int imgArray[][]) { 

   anImage.loadPixels();
   for (int i = 0; i < imgArray.length; i++) {   
     for (int j = 0; j < imgArray[0].length; j++) {   
     int loc = i + j*imgArray.length;   
     set(i, j+100, color(imgArray[i][j])); 
     } 
   }
   anImage.updatePixels();
}

2 个答案:

答案 0 :(得分:3)

嗯,错误信息说明了一切:

  

方法drawHistogram(int [],int,int)不适用于参数(PImage,int []);

这意味着您正在尝试调用方法drawHistogram(),它被声明为drawHistogram(int[], int, int)(这意味着它需要一个int数组和两个int作为参数),但是您传递了一个{ {1}}和一个int数组而不是必需的参数。这没有意义。

这有点像试图将一张美元钞票贴在只接受硬币的机器上:它不起作用。

答案 1 :(得分:1)

正如JB Nizet所说。要纠正此特定错误,只需使用computeHistogram()方法,调用:

drawHistogram(computeHistogram(rocks), x, y);

其中x和y是绘制直方图的基准位置。但之后代码将无法编译。 您可以追踪其他一些错误:)