简体   繁体   English

如何将BufferedImage转换为Texture?

[英]How to convert a BufferedImage to Texture?

I am trying to convert a BufferedImage to a Texture from Slick2D. 我正在尝试从Slick2D将BufferedImage转换为Texture I have tried BufferedImageUtil.getTexture() , but either I am using it wrong, or it's not working (returns only one brown pixel out of a 32x32 image) 我已经尝试过BufferedImageUtil.getTexture() ,但是我使用的是错误的,或者它不起作用(在32x32图像中仅返回一个棕色像素)

How can I convert a BufferedImage to a Texture ? 如何将BufferedImage转换为Texture

You are probably using it wrong, but as you do not provide any source code of your implementation I can only guess. 您可能使用了错误的代码,但是由于您未提供实现的任何源代码,我只能猜测。

The syntax for [BufferedImage.get(...)]( http://slick.ninjacave.com/javadoc-util/org/newdawn/slick/util/BufferedImageUtil.html#getTexture(java.lang.String , java.awt.image.BufferedImage)) is: [BufferedImage.get(...)]( http://slick.ninjacave.com/javadoc-util/org/newdawn/slick/util/BufferedImageUtil.html#getTexture ( java.lang.String ,java。 awt.image.BufferedImage))是:

public static Texture getTexture(java.lang.String resourceName,
                                 java.awt.image.BufferedImage resourceImage)
                          throws java.io.IOException

Parameters:
  resourceName - The location of the resource to load
  resourceImage - The BufferedImage we are converting
Returns:
  The loaded texture

I used this once when setting up my own fonts and I converted the BufferedImages (resulting from a splitImage operation of a bigger image with all characters on it) to Textures. 我在设置自己的字体时使用了一次,然后将BufferedImages(由对所有字符都在上面的较大图像的splitImage操作产生)转换为Textures。 It worked just fine for me, I ignored the resourceName parameter though. 它对我来说工作得很好,但是我忽略了resourceName参数。

So this works for me: 所以这对我有用:

BufferedImage image = ImageIO.read(new File(imagePath));
Texture texture = BufferedImageUtil.getTexture("", image); 

Looking at the source code of this method, the resourceName parameter should not have any impact on the method at all. 查看此方法的源代码,resourceName参数应该完全不影响该方法。

So just try it with my example and then try to implement this in your project, if you have any troubles with that let me know in the comments and I can help you. 因此,只要以我的示例进行尝试,然后尝试在您的项目中实现即可,如果您对此有任何疑问,请在评论中告知我,我可以为您提供帮助。

See 看到

Edit 编辑

There are some fundamental flaws with your code: 您的代码有一些基本缺陷:

  • ( This is why your code doesn't work at all/throws an exception ). 这就是为什么您的代码根本不起作用/抛出异常的原因 )。 You load the texture with BufferedImage texture = ImageIO.read(new File(path+ "/texture.png")); 您使用BufferedImage texture = ImageIO.read(new File(path+ "/texture.png"));加载BufferedImage texture = ImageIO.read(new File(path+ "/texture.png")); in your constructor, which does not change the texture parameter in your block but rather just creates a local unused variable. 在构造函数中,它不会更改块中的texture参数,而只会创建一个本地未使用的变量。 Fix this by removing the BufferedImage in that line. 通过删除该行中的BufferedImage来解决此问题。

  • (Almost) everything there is static. (几乎)所有东西都是静态的。 That makes no sense, as each block for example can have its own textures and parameters. 这没有任何意义,因为例如每个块都可以具有自己的纹理和参数。

  • Please obey the Java naming conventions. 请遵守Java命名约定。 Class names are upper camel case ( MyClass ), variable names lower camel case ( myVariable ), methods are lower camel case ( myMethod ), constants are upper case ( MY_CONSTANT ) and packages are lower case ( mypackage ). 类名是大写驼峰( MyClass ),变量名是小写驼峰( myVariable ),方法是小写驼峰( myMethod ),常量是大写MY_CONSTANTMY_CONSTANT ),包是小写( mypackage )。

Load texture: 加载纹理:

Texture texture; 纹理纹理; texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/image.png"))); texture = TextureLoader.getTexture(“ PNG”,new FileInputStream(new File(“ res / image.png”))));

If it doesn't fix your problem, then it is something wrong with the way you bind it. 如果它不能解决您的问题,则说明您绑定它的方式有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM