0

I don't really understand how to divide a class for the model and the view.

Let's say I have a class Cat which has some parameters like color, age, position on the map and some other stuff. I thought that the best way to create a custom painting is to override a paintComponent method. However the Cat class should be in the model and it shouldn't know anything about the way it is rendered in view.

So how to paint the cat? Let's say it is represented by an array of images and the model has variable direction which can contain an int from 0 to 3 which is related to pictures in array. What a view should do to draw a cat? Should it just have somewhere inside:

drawImage(model.getCat().getArray( model.getCat().getFrame() )) ?

2 Answers 2

2

Let's say the Cat class is represented by an array of images and the model has variable direction which can contain an int from 0 to 3 which is related to pictures in array.

In the class that represents the drawing panel, which extends JPanel, you keep an Image.

You have to pass one or more instances of Cat to the drawing panel class in the constructor.

In your drawing panel class, you would have a setImage method that contains the following line:

this.image = cat.getImage(imageNumber);

In your overriden paintComponent method, you would have the following line

drawImage(image, 0, 0, this);

Take a look at my article, 2048 Game in Java Swing, to see how a model, view, and controllers are created and used in a game.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok. Let's take a Cell class from your article. It contains a paint() method but still is inside a model package. Is it correct? It knows what's a recipe to render itself even though model shouldn't know ( correct me if im wrong ) anything about rendering.
@hejcz: Here's what I said when talking about the Game2048Model class. it also holds true for the Cell class. "This class also draws the game grid. I know, I said earlier that I separate the model from the view. The reason that the drawing code is included in the model is that it’s easier for Java objects to draw themselves. While the drawing code is included in the model, it’s executed as a part of the view."
Ok. So this is not really a STRICT MVC but holds an idea? I appreciate you help and the article. Thank you very much for help.
@hejcz: Go ahead and draw your model in the drawing panel class if you want. As I said, it's easier for objects to draw themselves. The code is bulkier if you draw the model in the drawing panel class.
I wanted to do my project same way you did your version of 2048. However I thought that I won't fill the requirement of MVC ( from my teacher ) :)
0

Let's say that Cat is the model. Change the cat's position with something like Cat.setPosition(int position). Calling that needs to fire an event that the view can listen for. A simple way to set that up is to have Cat extend java.util.Observable and call setChanged() and notifyObservers() from within setPosition().

Now for your view. It will have access to the UI components to be updated/drawn and use the java.util.Observer interface to listen for Cat events. Pass the Cat to your view's constructor and add an observer. You'll have to work out whether the view extends Observer or if you use some other internal or anonymous class. Now when the cat moves, the view will get the event and call getPosition(), which it will use to select the correct image from the array of images. Any images or drawing instructions would be in the view, but based on the Cat model's properties.

So what's the controller? It's whatever is handling user events, like the ActionListener on a button. Maybe you want a user's click on the image of the cat in the view to make the cat move to a new position. The image component click handler is the controller and will call Cat.setPosition(). That will cause the Cat model to fire an event, which the view will observe and then redraw the cat in the new position.

As long as everything is being driven by Swing/AWT component events, you don't have to worry about thread-safety. Otherwise see passing objects to another thread for some ideas.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.