[英]How to convert a BufferedImage to Texture?
我正在嘗試從Slick2D將BufferedImage
轉換為Texture
。 我已經嘗試過BufferedImageUtil.getTexture()
,但是我使用的是錯誤的,或者它不起作用(在32x32圖像中僅返回一個棕色像素)
如何將BufferedImage
轉換為Texture
?
您可能使用了錯誤的代碼,但是由於您未提供實現的任何源代碼,我只能猜測。
[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
我在設置自己的字體時使用了一次,然后將BufferedImages(由對所有字符都在上面的較大圖像的splitImage操作產生)轉換為Textures。 它對我來說工作得很好,但是我忽略了resourceName
參數。
所以這對我有用:
BufferedImage image = ImageIO.read(new File(imagePath));
Texture texture = BufferedImageUtil.getTexture("", image);
查看此方法的源代碼,resourceName參數應該完全不影響該方法。
因此,只要以我的示例進行嘗試,然后嘗試在您的項目中實現即可,如果您對此有任何疑問,請在評論中告知我,我可以為您提供幫助。
看到
編輯
您的代碼有一些基本缺陷:
( 這就是為什么您的代碼根本不起作用/拋出異常的原因 )。 您使用BufferedImage texture = ImageIO.read(new File(path+ "/texture.png"));
加載BufferedImage texture = ImageIO.read(new File(path+ "/texture.png"));
在構造函數中,它不會更改塊中的texture參數,而只會創建一個本地未使用的變量。 通過刪除該行中的BufferedImage
來解決此問題。
(幾乎)所有東西都是靜態的。 這沒有任何意義,因為例如每個塊都可以具有自己的紋理和參數。
請遵守Java命名約定。 類名是大寫駝峰( MyClass
),變量名是小寫駝峰( myVariable
),方法是小寫駝峰( myMethod
),常量是大寫MY_CONSTANT
( MY_CONSTANT
),包是小寫( mypackage
)。
加載紋理:
紋理紋理; texture = TextureLoader.getTexture(“ PNG”,new FileInputStream(new File(“ res / image.png”))));
如果它不能解決您的問題,則說明您綁定它的方式有問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.