Search code examples
javaswingmodel-view-controllerjpanelpaintcomponent

Where should I put these fields in the MVC model?


Let's say I have three classes: JPSModel, JPSView extends JPanel, and JPSController. JPSView overrides paintComponent(), which draws an image at a certain zoomFactor at position (renderPositionX, renderPositionY).

Should zoomFactor, renderPositionX, and renderPositionY, which the user can change, be in JPSView or JPSModel? What about methods to calculate zoomFactor, renderPositionX, and renderPositionY like setZoomFit()?


Solution

  • They should all be in JPSView since they are not related to "what" (data) is to be displayed; they all relate to "where" (positions) and "how" (at what zoom factor) the data is to be displayed.

    Remember Model is persisted and you never save view information along with it unless it's a ViewModel. To give you an example, most PDF readers give you an option to reopen the PDF files at the exact same page (as well as the same zoom factor) as it was on the last time you closed it. But, that does not mean that it saves the page number and the zoom factor in the PDF itself.

    To cater to such situations and if you feel like persisting "last viewed as" related view information a separate JPSViewModel should be created that you should ideally persist in your applications local database and not alongside the file it's processing.