-1

I wanted to try to make a sample traffic light program using paintComponent, but i'm a little confused on the MVC approach for it, 2 questions:

I've made it with a frame(view) engine(model) and a JButton(controller), which will turn the light on/off.

To me, the simplest way to have this done, is having my controller directly communicate to the view, by calling toggle(); (a method which switches a boolean "active" to true/false"), on the view, and then calling repaint(); on the view. This repaint method would check if it's active, then draw something different based on whether active is true or false.

first Question is, is this acceptable by MVC standards? I would say yes as it's just drawing, however I believe there shouldn't be calculations in the views methods, is the checking of "active" considered calculations?

Would a better solution be to create a method in the model that can then call the toggle() method on the view, as it is technically changing data? or is it better to just change it directly from the controller. It's certainly easier in a smaller scenario but I can see if it would be confusing for a larger solution.

Not sure if this question is clear enough, let me know and i'll try provide more information.

1 Answers1

0

To me, the simplest way to have this done, is having my controller directly communicate to the view, by calling toggle(); (a method which switches a boolean "active" to true/false"), on the view, and then calling repaint(); on the view. This repaint method would check if it's active, then draw something different based on whether active is true or false

Not good as this completely removes the model from your program, and the model is the key to MVC. Unless you're saying that the active field is your model in its entirety which trivializes your MVC.

create a method in the model that can then call the toggle() method on the view'

No, this is not acceptable as one key to MVC is that the model must be completely ignorant of the view.

The button should not be the control but rather is definitely part of the view. Its listener however is either the control or communicates to the control that the button has been pushed. The control then notifies the model which decides if it should change state. There should be a listener attached to the model by the control, which then changes the view to reflect the state of the model.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hi, my apologies, I did mean a listener rather than button. w –  Oct 28 '18 at 15:17
  • would the correct solution be: Button listener calling a method on the model which changes some data in the view, then model calling method on the view to repaint/revalidate it –  Oct 28 '18 at 15:22
  • @anders12232323: No. Again as I've stated above the model **should not change the view** directly. When I've done MVC, I've wired the model to allow for listeners (as noted above), and these listeners will be what changes the view. – Hovercraft Full Of Eels Oct 28 '18 at 15:41
  • @anders12232323: give your model a SwingPropertyChangeSupport and allow it to accept listeners as per [this answer](https://stackoverflow.com/questions/25416420/drawing-with-paintcomponent-after-value-of-jbutton-changed-in-another-class/25416492#25416492) – Hovercraft Full Of Eels Oct 28 '18 at 15:43