遇到了一个超出界限的协调 - 将图片复制成网格格式

时间:2014-06-25 20:10:04

标签: java exception pixels

public static void main(String[] args) 
{     
   Picture sourcePicture = new Picture("C:\\caterpillar.jpg");

   int grid_size = 2;   

   Picture GridPic = makeGrid(sourcePicture, grid_size);

   GridPic.show();
}

public static Picture makeGrid(Picture sourcePicture, int grid_size){
  Picture gridpic = null;
  int picheight = sourcePicture.getHeight();
  int picwidth = sourcePicture.getWidth();
  Picture calculatedPic = new Picture(picwidth*grid_size, picheight*grid_size);
  int xStart =0, yStart = 0;


  for(int i = 0; i < grid_size; i ++){
      for(int j = 0; j < grid_size; j ++){
          calculatedPic = CopyPictureTo(sourcePicture, calculatedPic, xStart, yStart);
          xStart = xStart + picwidth;
      }
      xStart = 0;
      yStart = grid_size * picheight;
  }

  return calculatedPic;
}

public static Picture CopyPictureTo(Picture sourcePicture, Picture calculatedPicture, int xStart, int yStart){
  Pixel sourcePixel = null;
  Pixel targetPixel = null;

  for(int sourceX =0, targetX = xStart; sourceX < sourcePicture.getWidth(); sourceX++, targetX++){
      for(int sourceY =0, targetY = yStart; sourceY < sourcePicture.getHeight(); sourceX++, targetY++){
          sourcePixel = sourcePicture.getPixel(sourceX, sourceY);
          targetPixel = calculatedPicture.getPixel(targetX, targetY);
          targetPixel.setColor(sourcePixel.getColor());
      }
  }
  return calculatedPicture;
}

该程序的目标是通过复制和粘贴所有像素,从源图片制作方形网格布局。 (它提取像素的颜色并设置颜色) 我在下一行中面对一个数组超出范围的异常。无法弄清楚。请帮忙。

 targetPixel.setColor(sourcePixel.getColor());
  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:   协调出界!
在   sun.awt.image.IntegerInterleavedRaster.getDataElements(未知   来源)
在java.awt.image.BufferedImage.getRGB(未知   来源)
在   bookie.SimplePicture.getBasicPixel(SimplePicture.java:303)
在   bookie.Pixel.getAlpha(Pixel.java:73)
在   bookie.Pixel.setColor(Pixel.java:213)
在   com.picture.Picture.CopyPictureTo(Picture.java:189)
在   com.picture.Picture.makeGrid(Picture.java:170)
在   com.picture.Picture.main(Picture.java:103)

0 个答案:

没有答案