我有纹理实体的问题,加载纹理时没有错误或异常,但没有显示纹理。这是我的代码:
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次返回?
答案 1 :(得分:0)
问题解决了 - 在loadTexture方法中,必须在catch
中返回nullprivate 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;
}
}