gpt4 book ai didi

java - 如何有选择地重置数组的维度?

转载 作者:行者123 更新时间:2023-11-30 07:39:22 24 4
gpt4 key购买 nike

我有一个抽象类Sprite ,它在其构造函数中为各个 Sprite 创建一个数组。构造函数采用 spritesheet 的路径、作为枚举的 spritesheet 类型以及各个 sprite 的宽度和高度。构造函数获取 spritesheet 的路径,将其加载到 BufferedImage 中,并从图像中抓取各个 sprite 以将它们加载到数组中。这是类(class):

package exosoft.Zerfall;

public abstract class Sprite extends Main {
BufferedImage spriteSheet = null;
BufferedImage[] sprites;
int spriteWidth = 0;
int spriteHeight = 0;

public enum SheetType {
SINGLE,
HORIZONTAL,
VERTICAL,
RECTANGULAR
}
public Sprite(SheetType type, String sheetPath, int spriteWidth, int spriteHeight) {
try {
spriteSheet = ImageIO.read(new File(sheetPath));
} catch (IOException e) {
e.printStackTrace();
}
switch(type) {
case HORIZONTAL:
sprites = new BufferedImage[spriteSheet.getWidth() / spriteWidth];
for (int i = 0; i < spriteSheet.getWidth() / spriteWidth; i++) {
sprites[i] = spriteSheet.getSubimage(i * spriteWidth, 0, spriteWidth, spriteHeight);
}
break;
case RECTANGULAR:
break;
case VERTICAL:
sprites = new BufferedImage[spriteSheet.getHeight() / spriteHeight];
for (int i = 0; i < spriteSheet.getHeight() / spriteHeight; i++) {
sprites[i] = spriteSheet.getSubimage(0, i * spriteHeight, spriteWidth, spriteHeight);
}
break;
default:
break;
}
}
}

我想做的是允许 BufferedImage[] sprites数组以根据给定的 SheetType 添加或删除维度。例如,这是我希望它起作用的 switch 语句:

switch(type) {
case HORIZONTAL:
sprites = new BufferedImage[spriteSheet.getWidth() / spriteWidth];
//load sprites into one-dimensional array
break;
case RECTANGULAR:
sprites = new BufferedImage[spriteSheet.getWidth() / spriteWidth][spriteSheet.getHeight / spriteHeight];
//load sprites into two-dimensional array
break;
case VERTICAL:
sprites = new BufferedImage[spriteSheet.getHeight() / spriteHeight];
//load sprites into one-dimensional array
break;
case SINGLE:
sprites = new BufferedImage();
//load sprite into zero-dimensional BufferedImage
break;
default:
break;
}

目标是最终得到一个名为 sprites 的数组包含各个 Sprite ,并根据需要具有尺寸。这是可能吗?谢谢。

最佳答案

你所做的是这样的:

BufferedImage[][] sprites=null;

case 0-dimension: sprites=new BufferedImage[1][1];
case 1-dimension: sprites=new BufferedImage[n][1]
case 2-dimension: sprites=new BufferedImage[n][m]

然后

case 0-dimension: sprites[0][0]=new BufferedImage(....);
case 1-dimension: for(i) sprites[i][0]=new BufferedImage()
case 2-dimension: for(i) for(j) sprites[i][j]=new BufferedImage()

关于java - 如何有选择地重置数组的维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34913770/

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