1

私はこの種の方法を持っています:

public void SaveImageOntoObject(String filepath) throws IOException {


      BufferedImage image = ImageIO.read(getClass().getResourceAsStream(filepath));
      this.width = image.getWidth();
      this.height = image.getHeight();
      this.ResetPointInformation();
      for (int row = 0; row < width; row++) {
         for (int col = 0; col < height; col++) {
            this.PointInformation[row][col] = new Color(image.getRGB(col, row));
        }
     }
  }

画像のファイルパスを入力として受け取り、各ピクセルのRPG値をカラーオブジェクトに変換してから、メソッドが呼び出されたオブジェクトの2次元配列PointInformationに格納します。

今私の問題に:

このようないくつかの写真が:

ここに画像の説明を入力してください

魅力のように働く、他の人はこのように:

ここに画像の説明を入力してください

エラーが発生します。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
at Drawing.Object2D.SaveImageOntoObject(Object2D.java:75)** (that's the class whose object's my method works on)

なんでそんなの?Javaは特定のRGB値を色に変換できないようですか?

どうすればそれを機能させることができるか教えていただけますか?

4

1 に答える 1

3

エラーメッセージには、実際には「インデックスが範囲外です」と表示されます。座標とその境界を混同しているようです。 getRGBパラメータx(範囲0 .. width)を最初に、y(範囲0 .. height)を2番目に取ります。

this.width = image.getWidth();
this.height = image.getHeight();

for (int row = 0; row < height; row++) {        // swapped the ...
    for (int col = 0; col < width; col++) {     // ... bounds
        this.PointInformation[row][col] = new Color(image.getRGB(col, row));
    }
}

最初の例はwidth=heightであるため、問題は表示されません。

于 2012-11-27T20:32:49.840 に答える