I'm making a game for my APCS class in Java. It uses Algebra and Math, but that's not the problem:
I have a State class instead of a State Enum because for some reason, Enum doesn't work.
public class State {
private int option;
public int getState(){
return option;
}
public void setState(int option){
this.option=option;
}
}
Now, I'm 90% sure this isn't the problem because when I set the state to the actual game state number, It runs fine, except it doesn't display the background.
My Menu class looks like this:
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);
}
You can test it out yourself, but all images are in the correct directory and the res folder has been added to the build path.
The ImageLoader class just includes a method loadImage which returns a BufferedImage.
In my GameMain class, this is my render method which should display the images:
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){
}
}
And here is my init method which (when I'm done) will initialize everything:
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();
}
}
This is the result when I set the state to the Menu state(1): Menu State Game
And this is the result when the State is set to the Game state (Numbers 3-10): Game State Game
EDIT: Someone wanted to see my main method, I guess...
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();
EDIT: Found out why it didn't work. I didn't include a bStrat.show() after my call to the render method in my GameMain render method.
EDIT 2: I looked back at my code and if anyone wants to use it, you should also put g.dispose() before it, so that it frees up resource allocation or something