从Sprite表Java读取图像 [英] Reading images from a sprite sheet Java
本文介绍了从Sprite表Java读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在游戏中使用精灵表,经过研究,我发现了这段代码.
I want to use sprite sheets in my game and with the research I have done found this piece of code.
BufferedImage bigImg = ImageIO.read(new File("sheet.png"));
// The above line throws an checked IOException which must be caught.
final int width = 10;
final int height = 10;
final int rows = 5;
final int cols = 5;
BufferedImage[] sprites = new BufferedImage[rows * cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[(i * cols) + j] = bigImg.getSubimage(
i * width,
j * height,
width,
height
);
}
}
我了解此片段如何将精灵表转换为数组,但是我如何访问该数组.只是sprites[i];
吗?
I understand how this snippet will turn the sprite sheet into an array, but how do I access this array. Is it just sprites[i];
?
还可以使用
int spritename = glgentextures;
{
sprites[i];
}
提前谢谢.
推荐答案
要访问sheet.png中的特定图像,可以使用sprite [rowNum * cols + colNum].
To access a certain image in sheet.png you can use sprite[rowNum*cols + colNum].
这篇关于从Sprite表Java读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文