Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从像素阵列中的索引获取x和y_Java_Arrays_Pixel - Fatal编程技术网

Java 从像素阵列中的索引获取x和y

Java 从像素阵列中的索引获取x和y,java,arrays,pixel,Java,Arrays,Pixel,所以我有一个像素数组来表示一个图像。我目前正在尝试从像素阵列的元素中获取x和y值,但我只能成功获取x 我当前的代码: public int[] draw(int[] pixels, int index, int xOffs, int yOffs) { int x0 = index % width; int y0 = (index * x0) / height; for (int y = y0 * size; y < y0 * size + size; y++)

所以我有一个像素数组来表示一个图像。我目前正在尝试从像素阵列的元素中获取x和y值,但我只能成功获取x

我当前的代码:

 public int[] draw(int[] pixels, int index, int xOffs, int yOffs) {

    int x0 = index % width;
    int y0 = (index * x0) / height;

    for (int y = y0 * size; y < y0 * size + size; y++) {
        int yPix = y + yOffs - (size * y0);
        if (yPix < 0 || yPix >= Game.height) continue;

        for (int x = x0 * size; x < x0 * size + size; x++) {
            int xPix = x + xOffs - (size * x0);
            if (xPix < 0 || xPix >= Game.width) continue;

            int src = this.pixels[x + y * width];
            pixels[xPix + yPix * Game.width] = src;
        }
    }
    return pixels;
}
public int[]绘图(int[]像素,int索引,int xOffs,int yOffs){
int x0=指数%宽度;
int y0=(指数*x0)/高度;
对于(int y=y0*size;y=游戏高度)继续;
对于(int x=x0*size;x=Game.width)继续;
int src=this.pixels[x+y*宽度];
像素[xPix+yPix*Game.width]=src;
}
}
返回像素;
}
x0返回正确的值,例如,如果索引为4,则返回0。 y0总是返回0

基本上,我试图实现的是一个精灵表使用像素操纵。
谢谢。

我不是专家,但我认为
inty0=(index*x0)/height应为
int y0=索引/宽度

注:为什么要将
(y0*size)
添加到
y
,然后在
yPix
中再次减去它,这似乎是不必要的,而且会使代码有点不可读