getSubimage 方法将获取 x、y 以及 width 和 height得到所需的子图像,从而获得所需的精灵。 由于大多数精灵似乎大小相同,因此我认为大多数精灵都可以通过嵌套的 for 循环来检索以迭代大图像。
例如,如果使用 ImageIO 类(例如 read 方法),每个精灵的大小为 10 像素 x 10 像素,其中 是 5 行 x 5 列的精灵,精灵可以通过以下方式获得:
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(
j * width,
i * height,
width,
height
);
}
}
If the sprites area read into a BufferedImage, the getSubimage method can be used to get a subimage of the sprite sheet.
The getSubimage method will take the x, y, and the width and height of the desired subimage, so the desired sprite can be obtained. Since most of the sprites seem to be the same size, I would think most of them can be retrieved by a nested for loop to iterate through the large image.
For example, if the sprite image is loaded using the ImageIO class (such as the read method), and each sprite is 10 pixels by 10 pixels in size, where are 5 rows by 5 columns of sprites, the sprites can be obtained by the following:
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(
j * width,
i * height,
width,
height
);
}
}
The catch is, of course, the above code will only work if all the sprites are the same size, so there will need to be some adjustment performed in order to work for the given sprite sheet. (As the top right-hand corner seems to be different in size from the others.)
void drawSpriteFrame(Image source, Graphics2D g2d, int x, int y,
int columns, int frame, int width, int height)
{
int frameX = (frame % columns) * width;
int frameY = (frame / columns) * height;
g2d.drawImage(source, x, y, x+width, y+height,
frameX, frameY, frameX+width, frameY+height, this);
}
columns 是多少列你的精灵表中有。 该方法的前两行计算工作表中 sprite 帧的 x 任意 y 位置。
If you just want to draw the sprites, Java's Graphics class has a drawImage method that will pull a specific area of the image out for you. You just have to specify the source image, where you want to draw the sprite on your Graphics object (x, y, width, height), and in what frame of the image the sprite is located (x, y, width, height).
Assuming the width and the height of the sprite are the same width and height that you want to draw on the drawing area, you could define your own method to draw a sprite frame as follows
void drawSpriteFrame(Image source, Graphics2D g2d, int x, int y,
int columns, int frame, int width, int height)
{
int frameX = (frame % columns) * width;
int frameY = (frame / columns) * height;
g2d.drawImage(source, x, y, x+width, y+height,
frameX, frameY, frameX+width, frameY+height, this);
}
columns is how many columns there are in your sprite sheet. The first two lines of the method calculate the x any y position of the sprite frame in your sheet.
Those big sprites in your sheet will require special handling. You could draw them with tiles (so you'd be drawing four sprites for each of the big images in this case), or you could manually figure out what x, y, width, and height, to use for those sprites.
If your sprite sheet were a regular sheet (all sprites the same size) and it was arranged in a 5 x 15 pattern as yours is, you would draw the 20th frame with the following method call
Here, x and y are the position you want to draw the sprite on your Graphics object, 15 is the number of columns in your sprite sheet, 19 is the frame (numbering starts at 0), and 25 is the width and height of each sprite (I approximated).
发布评论
评论(2)
如果精灵区域读入
BufferedImage
,getSubimage
方法可用于获取精灵表的子图像。getSubimage
方法将获取x
、y
以及width
和height
得到所需的子图像,从而获得所需的精灵。 由于大多数精灵似乎大小相同,因此我认为大多数精灵都可以通过嵌套的 for 循环来检索以迭代大图像。例如,如果使用
ImageIO
类(例如read
方法),每个精灵的大小为 10 像素 x 10 像素,其中 是 5 行 x 5 列的精灵,精灵可以通过以下方式获得:当然,问题是,上述代码仅在所有精灵尺寸相同时才有效,因此需要执行一些调整才能适用于给定的精灵表。 (因为右上角的大小似乎与其他角落不同。)
If the sprites area read into a
BufferedImage
, thegetSubimage
method can be used to get a subimage of the sprite sheet.The
getSubimage
method will take thex
,y
, and thewidth
andheight
of the desired subimage, so the desired sprite can be obtained. Since most of the sprites seem to be the same size, I would think most of them can be retrieved by a nestedfor
loop to iterate through the large image.For example, if the sprite image is loaded using the
ImageIO
class (such as theread
method), and each sprite is 10 pixels by 10 pixels in size, where are 5 rows by 5 columns of sprites, the sprites can be obtained by the following:The catch is, of course, the above code will only work if all the sprites are the same size, so there will need to be some adjustment performed in order to work for the given sprite sheet. (As the top right-hand corner seems to be different in size from the others.)
如果您只想绘制精灵,Java 的 Graphics 类有一个 drawImage方法将为您提取图像的特定区域。 您只需指定源图像、要在 Graphics 对象上绘制精灵的位置(x、y、宽度、高度)以及精灵位于图像的哪个帧中(x、y、宽度、高度) 。
假设精灵的宽度和高度与您要在绘图区域上绘制的宽度和高度相同,您可以定义自己的方法来绘制精灵框架,如下所示
columns
是多少列你的精灵表中有。 该方法的前两行计算工作表中 sprite 帧的 x 任意 y 位置。工作表中的那些大精灵需要特殊处理。 您可以使用图块来绘制它们(因此在本例中您将为每个大图像绘制四个精灵),或者您可以手动计算出用于这些精灵的 x、y、宽度和高度。
如果您的精灵表是常规表(所有精灵大小相同)并且它像您一样以 5 x 15 的图案排列,您将使用以下方法调用绘制第 20 帧
这里,x 和 y 是您想要的位置要在 Graphics 对象上绘制精灵,15 是精灵表中的列数,19 是帧(编号从 0 开始),25 是每个精灵的宽度和高度(我近似)。
If you just want to draw the sprites, Java's Graphics class has a drawImage method that will pull a specific area of the image out for you. You just have to specify the source image, where you want to draw the sprite on your Graphics object (x, y, width, height), and in what frame of the image the sprite is located (x, y, width, height).
Assuming the width and the height of the sprite are the same width and height that you want to draw on the drawing area, you could define your own method to draw a sprite frame as follows
columns
is how many columns there are in your sprite sheet. The first two lines of the method calculate the x any y position of the sprite frame in your sheet.Those big sprites in your sheet will require special handling. You could draw them with tiles (so you'd be drawing four sprites for each of the big images in this case), or you could manually figure out what x, y, width, and height, to use for those sprites.
If your sprite sheet were a regular sheet (all sprites the same size) and it was arranged in a 5 x 15 pattern as yours is, you would draw the 20th frame with the following method call
Here, x and y are the position you want to draw the sprite on your Graphics object, 15 is the number of columns in your sprite sheet, 19 is the frame (numbering starts at 0), and 25 is the width and height of each sprite (I approximated).