使用Spritesheet加载图像时出现问题 [英] Problem Loading Image Using Spritesheet
问题描述
大家好日子,
我正在开发游戏,我正在使用spritesheet进行图像加载,但似乎我遇到了问题开发这个。
现在我在JFrame上使用画布,所以如果这对我必须做的事情有所帮助请告诉我。
在我所做的所有阅读中,我得出的结论是我需要使用BufferedImage,我就是这样。
下面是我的两个类,它们与从精灵表中加载图像有关。
ImageLoader.java
Good day everyone,
I am developing a game and I am using a spritesheet for my image loading, however it seems I have run into a problem somewhere while developing this.
Now I am using a canvas on a JFrame, so if that makes a difference in what I have to do let me know please.
In all of the reading that I have done I came to the conclusion that I need to use a BufferedImage, which I am.
Below is my two classes that have to do with loading images from the sprite sheet.
ImageLoader.java
public class ImageLoader {
public BufferedImage load(String path) {
try {
return ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
SpriteSheet.java
SpriteSheet.java
public class SpriteSheet {
private BufferedImage sheet;
public SpriteSheet(BufferedImage sheet) {
this.sheet = sheet;
}
public BufferedImage crop(int col, int row, int w, int h) {
return sheet.getSubimage(col * 16, row * 16, w, h);
}
}
在我的播放器类中,我有一个渲染方法,可以在下面看到。
Player.java(渲染方法)
In my player class I have a render method, which can be seen below.
Player.java (Render Method)
public void render(Graphics g) {
g.drawImage(ss.crop(0, 0, 16, 16), x, y, 16 * OperationXMain.scale, 16 * OperationXMain.scale, null);
}
在我的主课堂上,我正在调用player.render,并传递g(图形) 。这是在我渲染一个JFrame和Canvas的完整大小的矩形之后发生的。
在我的游戏循环之前我调用我的init方法,即创建一个新实例我创建的ImageLoader类,然后我将一个BufferedImage变量设置为ImageLoader.load的值,我在其中传递我的图像的位置。
请注意我已经引用了一个res文件夹到我的java项目,所以我知道它有用。
为了让你更容易理解,我已经包含了我的init方法的代码。
In my main class, I am calling player.render, and passing g (Graphics). This happens after I render a rectangle that is the full size of the JFrame and Canvas.
Before my gameloop I am calling my init method, which is creating a new instance of the ImageLoader class that I created, then I am setting a BufferedImage variable to the value of ImageLoader.load, in which I am passing the location of my image.
Please note that I have referenced a res folder to my java project, so I know that works.
To make it easier for you to understand, I have included the code for my init method.
public void init() {
ImageLoader imageloader = new ImageLoader();
spriteSheet = imageloader.load("res/spritesheet.png");
SpriteSheet ss = new SpriteSheet(spriteSheet);
player = new Player(0, 0, ss);
}
很抱歉这个长期问题,只是想确保我澄清一切,以便有人可以帮帮我。
谢谢!
Sorry for the long question, just wanted to make sure I clarified everything well enough so someone can help me out.
Thanks!
推荐答案
你不想用BufferedImage.getSubImage
对于每一帧,你想用你的精灵表做的是渲染一部分而不是创建一个新的图像
对象。
这样的东西可以提供帮助:
You don't want to useBufferedImage.getSubImage
for every frame, what you want to do with your sprite sheet is to render a portion of is rather than creating a newImage
object.
Something like this can help:
public class SpriteSheet {
private final BufferedImage image;
public SpriteSheet(final String filename) throws IOException {
image = ImageIO.read(new File(filename));
}
public void render(final Graphics graphics, final Rectangle source, final Rectangle destination) {
graphics.drawImage(
image,
destination.x,
destination.y,
destination.x + destination.width,
destination.y + destination.height,
source.x,
source.y,
source.x + source.width,
source.y + source.height,
null);
}
}
public class Player {
private final SpriteSheet sheet;
private final Rectangle source = new Rectangle(0, 0, 16, 16);
private final Rectangle destination = new Rectangle(0, 0, 16, 16);
public Player(final SpriteSheet sheet) {
this.sheet = sheet;
}
public void render(Graphics g) {
destination.x = x;
destination.y = y;
destination.width = 16 * OperationXMain.scale;
destination.height = 16 * OperationXMain.scale;
sheet.render(graphics, source, destination);
}
}
希望这会有所帮助,
Fredrik
Hope this helps,
Fredrik
这篇关于使用Spritesheet加载图像时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!