为什么我的渲染方法不显示任何内容?



我正在用Java为我的APCS类制作一个游戏。它使用代数和数学,但这不是问题:

我有一个State类而不是State枚举,因为出于某种原因,枚举不起作用。

public class State {
private int option;
public int getState(){
return option;
}
public void setState(int option){
this.option=option;
}
}

现在,我90%确信这不是问题所在,因为当我将状态设置为实际的游戏状态编号时,它运行良好,只是它不显示背景。

我的菜单类如下:

public Rectangle playButton= new Rectangle(GameMain.WIDTH/2 +120,200,117,49);
public Rectangle helpButton= new Rectangle(GameMain.WIDTH/2 +120,300,117,49);
public Rectangle quitButton= new Rectangle(GameMain.WIDTH/2 +120,400,117,49);
ImageLoader loader = new ImageLoader();
GameMain game;
public BufferedImage bgrd, play,help, quit;
public void init(){
try {
bgrd = loader.loadImage("res/imgs/bgrd.png");
play = loader.loadImage("res/imgs/play.png");
help = loader.loadImage("res/imgs/help.png");
quit = loader.loadImage("res/imgs/exit.png");
} catch (IOException e) {
e.printStackTrace();
}
}

public void render(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g.drawImage(bgrd,0,0, null);
g.drawImage(play,game.WIDTH/2 +120,200,null);
g.drawImage(help,game.WIDTH/2 +120,300,null);
g.drawImage(quit,game.WIDTH/2 +120,400,null);
}

您可以自己测试,但所有图像都在正确的目录中,并且res文件夹已添加到构建路径中。

ImageLoader类只包括一个方法loadImage,它返回一个BufferedImage。

在我的GameMain类中,这是我的渲染方法,应该显示图像:

private void render(){
BufferStrategy bStrat = this.getBufferStrategy();
if(bStrat==null){
createBufferStrategy(3);
return;
}
Graphics g = bStrat.getDrawGraphics();
if(state.getState()==1){
menu.render(g);
}
if(state.getState()==2){
}
if(state.getState()>2 && state.getState()<11){
Font font=new Font("Verdana",Font.BOLD,20);
g.setFont(font);
g.drawImage(bgrd,0,0,this);
g.drawString(gameName, 300, 100);
g.drawImage(wizard,x,y-75,this);
g.drawImage(hdkn,x,y,this);
g.dispose();
bStrat.show();
}
if(state.getState()==11){
}
}

这是我的init方法,当我完成后,它将初始化所有内容:

public void init(){
state=new State();
state.setState(1);
loader = new ImageLoader();
menu=new Menu();
menu.init();
try{
bgrd=loader.loadImage("res/imgs/bgrd.png");
wizard=loader.loadImage("res/sprites/wiz.png");
hdkn=loader.loadImage("res/sprites/hdkn.png");
}catch(IOException e){
e.printStackTrace();
}
}

这是我将状态设置为菜单状态(1)时的结果:菜单状态游戏

这是当State设置为Game状态(数字3-10)时的结果:游戏状态游戏

编辑:有人想看看我的主要方法,我想。。。

game=new GameMain();
game.setPreferredSize(new Dimension(WIDTH,HEIGHT));
game.setMaximumSize(new Dimension(WIDTH,HEIGHT));
game.setMinimumSize(new Dimension(WIDTH,HEIGHT));

JFrame frame = new JFrame("Math Game");
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();

编辑:找出为什么它不起作用。在调用GameMain渲染方法中的渲染方法后,我没有包含bStrat.show()。

编辑2:我回头看了一下我的代码,如果有人想使用它,你也应该把g.dispose()放在它前面,这样它就可以释放资源分配或之类的东西

也许您的Graphic对象g无法立即绘制。尝试使用

g.paintImmediately(0,0, /*your graphic width here*/, /*your graphic height here */)

在你要求画这幅画之后。这将强制图形的UI刷新,而不让系统选择何时刷新,仅限于此时间。

看看这里:https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#paintImmediately(int,%20int,%20iint,%20int)

最新更新