2013-01-17 50 views
0

我有紋理實體的問題,我沒有錯誤或異常時加載紋理但紋理未顯示。這裏是我的代碼:如何設置實體紋理

public class StartClass { 

private static class Button extends AbstractButtonEntity { 

    public Button(int x, int y, int width, int height, Texture texture) { 
     super(x, y, width, height, texture); 

    } 

    @Override 
    public void draw() { 

     texture.bind(); 
     glBegin(GL_QUADS); 

     glTexCoord2f(0, 0); 
     glVertex2i(x, y); 

     glTexCoord2f(1, 0); 
     glVertex2i(x + width, y); 

     glTexCoord2f(1, 1); 
     glVertex2i(x + width, y + height); 

     glTexCoord2f(0, 1); 
     glVertex2i(x, y + height); 

     glEnd(); 

    } 

} 

Texture background, logo; 

public StartClass() { 

    try { 
     Display.setDisplayMode(new DisplayMode(800, 600)); 
     Display.setTitle("Space Project"); 
     Display.create(); 
    } catch (LWJGLException e) { 
     e.printStackTrace(); 
    } 

    background = loadTexture("main_menu/bg"); 
    logo = loadTexture("main_menu/logo"); 

    Button ngButton = new Button(300, 200, 200, 45, 
      loadTexture("main_menu/ng_button")); 

    glMatrixMode(GL_PROJECTION); 
    GL11.glViewport(0, 0, 800, 600); 
    glLoadIdentity(); 
    glOrtho(0, 800, 600, 0, 1, -1); 
    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_TEXTURE_2D); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    while (!Display.isCloseRequested()) { 

     glClear(GL_COLOR_BUFFER_BIT); 

     ngButton.draw(); 

     drawBackground(); 

     Display.update(); 
     Display.sync(60); 
    } 
    Display.destroy(); 
} 

private Texture loadTexture(String key) { 
    try { 
     return TextureLoader.getTexture("PNG", new FileInputStream(
       new File("res/" + key + ".png"))); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

public void drawBackground() { 

    background.bind(); 

    glBegin(GL_QUADS); 

    glTexCoord2f(0, 0); 
    glVertex2i(0, 0); 

    glTexCoord2f(1, 0); 
    glVertex2i(800, 0); 

    glTexCoord2f(1, 1); 
    glVertex2i(800, 600); 

    glTexCoord2f(0, 1); 
    glVertex2i(0, 600); 

    glEnd(); 

    logo.bind(); 

    glBegin(GL_QUADS); 

    glTexCoord2f(0, 0); 
    glVertex2i(44, 44); 

    glTexCoord2f(1, 0); 
    glVertex2i(756, 44); 

    glTexCoord2f(1, 1); 
    glVertex2i(756, 115); 

    glTexCoord2f(0, 1); 
    glVertex2i(44, 115); 

    glEnd(); 

} 

public static void main(String[] args) { 
    new StartClass(); 
} 

    } 

這裏是AbstractButtonEntity:

public abstract class AbstractButtonEntity implements ButtonEntity { 

protected int x, y, width, height; 

protected String key; 

protected Texture texture; 

protected Rectangle hitbox = new Rectangle(); 

public AbstractButtonEntity(int x, int y, int width, 
     int height, Texture texture) { 

    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height; 
    this.texture = texture; 
} 



@Override 
public void setLocation(int x, int y) { 
    this.x = x; 
    this.y = y; 

} 

@Override 
public void setX(int x) { 
    this.x = x; 
} 

@Override 
public void setY(int y) { 
    this.y = y; 

} 

@Override 
public void setWidth(int width) { 
    this.width = width; 

} 

@Override 
public void setHeight(int height) { 
    this.height = height; 

} 

@Override 
public int getX() { 
    return x; 
} 

@Override 
public int getY() { 
    return y; 
} 

@Override 
public int getWidth() { 
    return width; 
} 

@Override 
public int getHeight() { 
    return height; 
} 


@Override 
public boolean intersects(ButtonEntity other) { 
    hitbox.setBounds(x, y, width, height); 
    return hitbox.intersects(other.getX(), other.getY(), other.getWidth(), 
      other.getHeight()); 
} 

} 

很抱歉,如果是新手的問題,但我是新手在Java

+0

似乎完全不可能在這裏 –

回答

0

爲什麼你有方法loadTexture 2回?

+0

如果紋理加載未全成,回報是空 – kubaj

+0

不應該說是在收穫? – w2lf

+0

如果代碼被讀取。那麼首先它使用try/catch中的返回值,然後返回值嘗試捕獲。所以你的回報=每次都是空的。但是如果它在catch中,那麼如果你有錯誤,它將返回null。 – w2lf

0

問題解決了 - 在loadTexture方法必須在抓地返回null

private Texture loadTexture(String key) { 
try { 
    return TextureLoader.getTexture("PNG", new FileInputStream(
      new File("res/" + key + ".png"))); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    return null; 
} catch (IOException e) { 
    e.printStackTrace(); 
    return null; 
} 

}