1

I am making a game which will have three games implemented in it. I started off my code in a single class, now that I want to move it around into a MVC format I am having errors. I started off with writing all the code in GameView Class, I now want to move some of the code from this class to a SView Class. The problem is that I have intialised the JFrame in GameView class and it cannot find the variable frame when I move my code to SView class. So then I have been told to make it into a panel and then move it but it's just messing up my game.

Umzz Mo
  • 219
  • 1
  • 6
  • 14

2 Answers2

1

You have to organize things a little differently. Some things to consider:

  • Is GameView your main window? Make it so GameView extends JFrame.
  • Is SView a panel? Make it so SView extends Panel.

Another thing to think about: SView is some panel that you want to add to your GameView, but it's not the responsibility of SView to add itself. Instead, simply instantiate and assemble these components somewhere outside both of these classes, f.e.:

GameView gameView = new GameView();
gameView.add(new SView());

You could also have GameView add a new SView() in its constructor code instead.

Anyway, the main point here is that a component should not concern itself with adding itself to its parent, just with creating its own content/children.

Good luck.

Torious
  • 3,364
  • 17
  • 24
1

Some suggestions:

  • The main GUI would use a CardLayout so you can swap JPanels easily.
  • The main function of the select game menu will be to swap JPanels, so the game selected is visualized and then initialize the selected game.
  • Work on each game individually. Gear each individual game class towards produding a JPanel to hold the game. Give each game class a main method for testing that would create a JFrame, create a game class instance, place the instance's JPanel into the JFrame, initialize the game and display it.
  • Once the game is working on its own, try adding it to your larger GUI, the one that can show multiple games.
  • Consider having each game class implement a common interface for common functionality. For example you could give the interface an initialize() method that each game class would override.
  • Each game class would need to be MVC-based so that it has a model, a control class(es) to interact with the model and a view class that displays the state of the model on the JPanel as noted above.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1 Excellent design advice; [`MainPanel`](http://stackoverflow.com/a/3072979/230513) is an example of point three. You can use any number of instances to prototype your larger GUI. – trashgod Apr 14 '12 at 17:42
  • @trashgod: thanks. Here's my link for an example of MVC: [MVC_ProgressBarThread](http://stackoverflow.com/questions/5533497/mvc-progress-bar-threading/5533581#5533581) – Hovercraft Full Of Eels Apr 14 '12 at 17:53
  • Good [example](http://stackoverflow.com/questions/5533497/mvc-progress-bar-threading/5533581#5533581); I cited it [here](http://stackoverflow.com/a/3072979/230513) as a way to implement the observer pattern. Don't hesitate to edit anything of mine as the need arises. – trashgod Apr 14 '12 at 18:01
  • Sorry, I thought I replied. My bad. My question was I dont understand the CardLayout part, how does it work? – Umzz Mo Apr 15 '12 at 17:35
  • @UmzzMo: Check out [CardLayout tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), and in fact check out all of the Swing tutorials found there. – Hovercraft Full Of Eels Apr 15 '12 at 17:45