[英]Map character to X and Y in png - JAVA
我有这个png图片:
和一个字符串,说“ Hello World”。 为了映射LWJGL的纹理坐标,我需要知道PNG中每个16x16字符的X和Y位置。 我完全不知道该怎么做。有人吗?
首先是这样的:
final int spriteWidth = 16;
final int spriteHeight = 16;
...
int rows = sheet.getWidth()/spriteWidth;
int cols = sheet.getHeight()/spriteHeight;
BufferedImage sheet = ImageIO.read(new File("\\a\b\\c\\sprite_file.png"));
BufferedImage[] images = new BufferedImage[rows * cols];
for(int y = 0; y < cols; y++) {
for(int x = 0; x < rows; x++) {
images[y * x] = sheet.getSubImage(x * rows, y * cols, spriteWidth, spriteHeight);
}
}
然后像这样制作最终的int变量:
public static final int SPRITE_0 = 0;
public static final int SPRITE_1 = 1;
...
并像这样访问:
images[SPRITE_0]
编辑:
考虑到@MadProgrammer所说的内容,我建议您将图像分为两部分,如下所示:
(在红线处分割)
然后只需更改代码即可处理两个不同的部分。 除了变量final int spriteWidth
和final int spriteHeight
外,代码将保持不变。 我相信您可以自己解决这个问题。
编辑2:
如果只需要每个子画面左上角的x和y坐标,请执行以下操作:
final int spriteWidth = 16;
final int spriteHeight = 16;
...
int rows = sheet.getWidth()/spriteWidth;
int cols = sheet.getHeight()/spriteHeight;
Point[] spriteTopLeftCorner = new Point[rows * cols];
for(int y = 0; y < sheet.getHeight(); y += spriteHeight) {
for(int x = 0; x < sheet.getWidth(); x += spriteWidth) {
spriteTopLeftCorner[y/spriteHeight * x/spriteWidth] = new Point(y, x);
}
}
当然,您仍然需要使变量代表此Array
每个精灵的索引,否则您将不知道要取出哪个精灵。
这样做:
public static final int SPRITE_0 = 0;
public static final int SPRITE_1 = 1;
...
并像这样访问:
spriteTopLeftCorner[SPRITE_0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.