0
我試圖使用內置的ImageIO.read(file)方法爲我的lwjgl項目加載紋理。當調用ImageIO.read(文件)時程序塊
創建一個新的File對象起作用,它存在於我有問題的ImageIO.read()方法中。
public Texture(String fileName) {
System.out.println("Texture init called");
try {
File img = new File("brick.png");
System.out.println(img.exists()); //Returns true
System.out.println(img.getAbsolutePath()); //Returns the correct path "/Users/griffinbabe/Documents/workspace/LWJGLTest/brick.png"
System.out.println(img.canRead()); //Returns true
Image im = ImageIO.read(img); //Blocks here, letting no printStackTrace, and letting the program running.
System.out.println("Yes you did it!");
} catch (Exception e) {
e.printStackTrace();
}
這很奇怪我從來沒遇到過這個問題。如果它可以幫助,我在mac OS Sierra上運行這個,我使用的是openGL。
這裏是主類,其中新的Texture對象被初始化。
public Window(int sizeX, int sizeY,Game game) {
this.game = game;
if (!glfwInit()) {
throw new IllegalStateException("GLFW failed to initialize.");
}
window = glfwCreateWindow(sizeX,sizeY,"Game3Graphics",0,0);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
if (window == 0) {
throw new IllegalStateException("Failed to create window.");
}
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (videoMode.width()-sizeX)/2, (videoMode.height()-sizeY)/2);
glfwShowWindow(window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
glEnable(GL_TEXTURE_2D);
Texture texture = new Texture("brick.png"); //Here is the init call.
System.out.println("texture finished loading");
任何想法它可能是什麼?
謝謝。
[編輯]
奇怪的是,ImageIO.read()方法的工作,如果我創建與glfwCreateWindow一個窗口之前調用它()。它與lwjgl有關,而不是文件訪問。
與用戶目錄相關的文件「brick.png」在哪裏?爲什麼你首先使用文件而不是資源? –
@HovercraftFullOfEels我想還有其他的方法來加載圖像。我使用這個,因爲它非常簡單。 brick.png文件放置在eclipse項目文件夾(與'src'相同的文件夾)。我一直這樣做,這是我第一次遇到問題。 – GriffinBabe
@ CraigR8806 LWJGL不再支持SlickUtils,因爲LWJGL 3 – GriffinBabe