单击像素时尝试填充10x10像素数组,然后再次单击时将其粘贴到其他区域,并在单击图像上的任何位置时继续获取ArrayIndexOutOfBoundsException。任何想法如何解决它? 这一切都在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;
}
}
答案 0 :(得分:1)
您实际上正在尝试访问temp [mouX] [mouY],当x或y坐标超过10时会导致异常。您希望i和e从0开始,所以
temp[i-mouX][e-mouY] = pix[i][e];