How to go about with real GUI development with Java Swing and MVC?

It took me ages to learn MVC (I got taught incorrect things about it in university, and additionally a lot of online sources at the time were wrong about it). Anyway, the core of you need to do is not have any view information in your model (i.e. , what the player looks like on the screen, the framebuffer, the polygons of your models).

Instead, you create the view and model in separate namespaces, and then use events to link the two together. When sometimes happens in your model, the view gets notified, and changes are made to the view. Additionally, when the mouse is pressed or a key is pressed, the input event gets transformed into another model-oriented event which can take the form of a method call into the model.

Any changes in the model are then fed back onto the view Remember this: the model should be functional without the view being attached, and should not show anything on the screen while executing (except perhaps debug info in the console).

It took me ages to learn MVC (I got taught incorrect things about it in university, and additionally a lot of online sources at the time were wrong about it). Anyway, the core of you need to do is not have any view information in your model (i.e. , what the player looks like on the screen, the framebuffer, the polygons of your models).

Instead, you create the view and model in separate namespaces, and then use events to link the two together. When sometimes happens in your model, the view gets notified, and changes are made to the view. Additionally, when the mouse is pressed or a key is pressed, the input event gets transformed into another model-oriented event which can take the form of a method call into the model.

Any changes in the model are then fed back onto the view. Remember this: the model should be functional without the view being attached, and should not show anything on the screen while executing (except perhaps debug info in the console).

– Myth17 Feb 23 '10 at 18:36 As an example for blackjack: BlackJack (model) and BlackJackApp (view). You attach BlackJackApp to the model (which might invoke a JFrame or something) using the notify/listener pattern, and notify the view when the model changes (create a big listener for the entire game state, initially). Then any standard operations in the view just call methods in the model.I.e.

, Card getCards(Player p), showHand(Player p), with an event like onShowHand(Player p) notifying the view through the listener. You might want a next() method in the model which advances the game – Chris Dennett Feb 23 '10 at 19:45.

Here is a simple example of how this could be split. Presumably, a player's cards are represented as a 'hand' or a similar object (i.e. A collection of cards).

This is your model. So lets call your model: package casino.blackjack. Model; class DealtCards {..} You display your cards using maybe a JPanel or some other Swing construct.So you could put all the objects that actually do the rendering of each card in a seperate package: package casino.blackjack.

View; class DealtCardsView {..} The DealtCards object exists independently of how it is displayed, but its state may change if a user does something on the GUI. For example, asking to be 'hit'. Presumably, there could be a button to do this.

The view is derived from your model. Package casino.blackjack. View; class DealtCardsView { JButton hitMeButton = new JButton("HIT"); DealtCards cards; public DealtCardsView(DealCards myCards) { cards = myCards; renderCards(); } private void renderCards(){.. do something..} } Now, if a player decides to hit, his DealtCards object changes.

So, we want to implement a way in which your model is updated. You can do this using a controller class. The controller class implements the ActionListener interface.

When an action is performed (i.e. User clicks "hit" button), the controller updates the model.So the view cannot directly update the model. It just sends a notification that an 'action' has occurred.

Any interested parties, in this case, our controller, can then take appropriate action. Package casino.blackjack. Controller; class DealtCardsController implements ActionListener { DealtCards cards; DealtCardsView cardView; public DealtCardsController(DealtCards myHand, DealtCardsView myView) { cards = myHand; cardView = myView; cardView.hitMeButton.

AddActionListener(this); } public void actionPerformed(ActionEvent e) { cards.changed(); } } So you split your application in three layers. Your model just contains the current state (or current data), and any validation that goes around it. Your view classes render the model in an appropriate manner.

Any user interaction, on the view, is handled by the controller, whose responsibility is then to update the model. This way, if you want to change your view, (say use an applet instead of a window) your model doesn't care. Sorry about the long winded reply, but hope that helps a little!

EDIT: A nice MVC explanation here: http://stackoverflow.com/questions/2320152/java-gwt-ui-coding-clean-code.

Sorry about the long winded reply, but hope that helps a little!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions