0

I'm currently implementing a card game into java using MVC pattern but I've got lots of difficulties with views. In fact, I've got many models classes and so many views (which are Observers of the Observable models) such as "Player" "Deck" "Card" "Game" etc...

In order to have a graphical user interface I'm asking if I should use only one JFrame (but in this case how can all the views act on the same JFrame ?) or create multiple JFrame such as each view extends JFrame ? It's my first time using MVC pattern and it remains really fuzzy for me :/

Hunteer
  • 1
  • 1

2 Answers2

1

Single frame is enough. In fact one view (Deck extends JPanel) could contains multiple another views (Cards extends JPanel) providing layout and delegating to cards painting and event processing.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Ok, so when you say the Deck "contains", it means this Deck view could have instances of, for ex, the view "HeapView" ? (Heap is where cards are put by players) – Hunteer Dec 26 '14 at 13:06
  • Yes. Deck panel can have heap panel and several card panels added. All you need is to specify desired layout. – StanislavL Dec 26 '14 at 13:19
  • Ok I thought having instance of other views in member variables wasn't really suitable. So in my case i've got a main model class called "Game" which owns players, a card heap, a card stack to pick card : is it correct to have a "GameObserver" class which extends from JFrame, which has PlayerObserver HeapObserver (etc..) instances and also a JPanel in its member variables ? – Hunteer Dec 26 '14 at 14:53
0

Game, Player, Deck, and Card are model classes. I'm assuming that a game can have one or more players, a game can have one or more decks, and a deck can have one or more cards.

You have one JFrame, with one or more JPanels, that represent the game table. The game table will display the player card hands at the appropriate time, and keep score.

In a Java Swing application, here's how you implement a model / view / controller pattern.

  1. The view may read values from the model.
  2. The view may not update values in the model.
  3. The controller will update values in the model, and may revalidate / repaint the view.

Generally, there's not one controller class. There are many small controller classes that handle one part of the view and one part of the model.

For a more complete example, read my 2048 Game in Java Swing article. It's not a card came, but you can see how the MVC pieces fit together.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111