Dylan Meeus
Dylan Meeus

Reputation: 5802

Java drawings MVC

I am trying to create a game in java using several design patterns / principles. One of them being MVC. The situation is like this: Model: Holds all game logic Controller: Button interaction and list of GameElements (see code) View: All GUI stuff including drawing.

Now, my game objects are all located under the Model, but for my drawing I've tried doing this (inside paintComponent)

ArrayList<GameElement> ge = FieldController.getElements(); // This is located under Controller
for(GameElement ge: GameElements)
{
graphics.setColor(ge.getColor());
graphics.fillRect(ge.x,ge.y,ge.width,ge.height);
}

Which works, but my question is: Where should the ArrayList of GameElements really be kept? Is it okay to hold it in the control? Or should it be kept in the view? I'm quite sure it should not be held in the model because then View&Model would be too tightly coupeled.

Upvotes: 1

Views: 80

Answers (1)

trashgod
trashgod

Reputation: 205805

Your List<GameElement> belongs in the model. Upon notification, a listening view should decide how to render the element. In this context, the notion of loose coupling refers to the notification; the view still has to interpret what it learns from the model. In this simplified example, the model manages an abstract Piece having an attribute for color. The GUI view shown renders this attribute as an Icon having the specified color. In contrast, a text view might render the attribute as a String.

Upvotes: 4

Related Questions