Sign in to follow this  
deej21

[java] graphical state system

Recommended Posts

I'm trying to make a state system, and I'm using it with a game I already made and adding a menu to it. I've never made a state system before, but I know the basic ideas of it, and bringing the ideas to java isn't as easy. The main problems are trying to implement double buffering in the states. I am getting a few problems at times, and if there are things that I'm making unnecessarily complicated, please help me out. I have an abstract MainGame class that extends JFrame keeps a list of all states and allows you to add/change states. The JFrame will contain only one member, a JPanel, which is gotten from State.getPanel(). Perhaps using a more generic Container or Component is better, but at this point, I'm only using JPanels anyway. There is a State interface that just has simple start() stop() update() methods, and also the aforementioned getPanel(). To be able to create graphics easily, I created abstract class GraphicsState extends Canvas implements State. The reason I used Canvas at first is because then I can merely use a BufferStrategy which, besides being easier to implement, is also a lot faster (in my experience) than the basic way of creating an image and drawing to that, then drawing that image to the screen. For getPanel(), I have a JPanel created, and add the GraphicsState to the JPanel. I have two states, a game and a menu. The menu is just a JPanel implementing State that has a button "start game". The game extends GraphicsState and does all its stuff there and has a key that makes it go back to the menu. This is where I am not sure what to do... I can't create a BufferStrategy without a peer. addNotify() is supposed to create a peer, but it doesn't work, so I have to create a JFrame to be a peer and just hide it so it's not shown. Another problem is when I switch from the game to the menu, I get a NullPointedException with the BufferStrategy. I think this is because the thread of game.update() gets paused for a moment when control gets switched over, and when it goes back to finish, some stuff is lost because the JPanel is not controlled by the MainGame's JFrame anymore. The alternative is the method mentioned above of creating a back image and drawing to that. This works, and is fairly fast (not as fast as above). However, this totally defeats the purpose of the Canvas (not a big deal really). I had used to get the same NullPointerException with this implementation, but for some reason when I debugged to find where it occurred, it stopped occuring. It's good that it's gone, but I'm not sure it really is. So, I guess I could just use the second implementation, but it doesn't seem to be the most efficient implementation. Any comments/advice would certainly be appreciated, and I'd be glad to post my code if you need.

Share this post


Link to post
Share on other sites
Ok I think I've got it. I know what your problem is you are trying to call createBufferStrategy() before you have added the Canvas to a JFrame. Try putting the call to createBufferStrategy() to the State.start() method. And remove the addNotify() call.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this