简体   繁体   English

从阵列复制和粘贴像素

[英]Copy and Paste Pixels from an Array

Trying to fill a 10x10 Array of pixels when I click on a pixel and then paste that into a different area when i click again and keep getting ArrayIndexOutOfBoundsException when I click anywhere on the image. 当我单击像素时尝试填充像素的10x10数组,然后在再次单击时将其粘贴到其他区域中,并在单击图像的任意位置时继续获取ArrayIndexOutOfBoundsException。 Any ideas how to fix it? 任何想法如何解决? This was all done in Processing 这些都是在处理中完成的

final int WIDTH = 1280;
final int HEIGHT = 720;
PImage pim;
int mouX;
int mouY;
color[][] pix = new color[WIDTH][HEIGHT]; 
color[][] temp = new color[10][10];

void setup() {
  size(1280,720);
  pim = loadImage("344625.jpg");
  image(pim, 0, 0);

  for(int r = 0; r < WIDTH; r++) {
    for(int c = 0; c < HEIGHT; c++) {
      pix[r][c] = pim.get(r,c);
    }
  }
}

int p = 0;
void draw() {
  mouX = mouseX;
  mouY = mouseY;
  pasty();
}

void pasty() {
  noStroke();
  if(mousePressed == true && p == 0) {
    for(int i = mouX; i < mouX + temp.length; i++) {
      for(int e = mouY; e < mouY + temp[i].length; e++) {
        temp[i][e] = pix[i][e];
      }
    }
    System.out.println("Copy");
    p = 1;
  }
  else if(mousePressed == true && p == 1) {
    System.out.println("Paste");
    for(int i = mouX; i < mouX + temp.length; i++) {
      for(int e = mouY; e < mouY + temp[i].length; e++) {
        fill(temp[i][e]);
        rect(i,e,1,1);
      }
    }
    p = 0;
  }
}

您正在尝试本质上访问temp [mouX] [mouY],当x或y坐标都超过10时,这将导致异常。您希望i和e从0开始,所以

temp[i-mouX][e-mouY] = pix[i][e];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM