gpt4 book ai didi

java - 在java中垂直和水平重复图像

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:15 25 4
gpt4 key购买 nike

我需要尝试垂直和水平复制由 array(a) 创建的图像,以形成一个充满重复图像的正方形。因此,从 1 个方形图像开始,我想将其水平复制 2 次,垂直复制 2 次,它将创建另一个具有 4 个初始图像的图像,2x2。

public static int[][] replicate(int[][] a) {
int[][] replicated = new int[a.length * 2][a[0].length * 2];

for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i][j + r * a[i].length] = a[i][j];
}
}
}

for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i + r * a.length][j] = a[i][j];
}
}
}
return replicated;
}

这使它们水平穿过 1 行,垂直穿过 1 行,但没有完全填充。所以如果我运行这个,我将丢失 4 个图像的右下角。我无法将它们组合在一起。

最佳答案

public static int[][] replicate(int[][] image) {
int[][] replicated = new int[a.length * 2][];

for (int y=0; y<image.length; y++) {
int[] column = image[y];

replicated[y] = new int[column.length * 2];
replicated[y + image.length] = new int[column.length * 2];

System.arraycopy(column, 0, replicated[y], 0, column.length);
System.arraycopy(column, 0, replicated[y], column.length, column.length);
System.arraycopy(column, 0, replicated[y + image.length], 0, column.length);
System.arraycopy(column, 0, replicated[y + image.length], column.length, column.length);
}

return replicated;
}

这段代码可能存在一些问题,因为我不是在编译器前编写它的,但我认为它明白了要点。

此外,如果这是为了绘制图像,您应该考虑使用TexturePaint

关于java - 在java中垂直和水平重复图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22652676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com