Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:05:25+00:00 2026-06-11T11:05:25+00:00

I’m after opinions please on this attempt at MVC using Java Graphics2D. I wanted

  • 0

I’m after opinions please on this attempt at MVC using Java Graphics2D.
I wanted to get my head round MVC using a simple example.

Have I got it right please?

Docs here.

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

http://www.oracle.com/technetwork/articles/javase/index-142890.html

Feedback appreciated cheers.

Edit: Code below has now been fixed based on feedback by Rasmus and further research.

public class App {

    /*
     * The view registers as a listener on the model. Any changes to the
     * underlying data of the model immediately result in a broadcast change
     * notification, which the view receives.
     * 
     * Note that the model is not aware of the view or the controller -- it
     * simply broadcasts change notifications to all interested listeners.
     * 
     * The controller is bound to the view. This typically means that any user
     * actions that are performed on the view will invoke a registered listener
     * method in the controller class.
     * 
     * The controller is given a reference to the underlying model.
     */
    public static void main(String[] args) {
        Model model = new Model();
        Controller controller = new Controller(model);
        View view = new View(controller);
        model.addModelChangedListener(view);

    }
}

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;

public class Model {

    private ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
    private ArrayList<ModelChangedEventListener> listeners = new ArrayList<ModelChangedEventListener>();

    public void addRectangle(int x, int y, int width, int height) {
        rectangles.add(new Rectangle(x, y, width, height));
        modelChangedEvent();
    }

    public ArrayList<Rectangle> getRectangles() {
        return rectangles;
    }

    public synchronized void addModelChangedListener(
            ModelChangedEventListener listener) {
        listeners.add(listener);
    }

    public synchronized void removeModelChangedListener(
            ModelChangedEventListener listener) {
        listeners.remove(listener);
    }

    private synchronized void modelChangedEvent() {
        ModelChangeEvent event = new ModelChangeEvent(this);
        Iterator<ModelChangedEventListener> i = listeners.iterator();
        while (i.hasNext()) {
            ((ModelChangedEventListener) i.next()).handleModelChange(event);
        }
    }

}

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventObject;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class View extends JFrame implements ModelChangedEventListener {

    private static final int DISPLAY_WIDTH = 300;
    private static final int DISPLAY_HEIGHT = 300;
    private ViewPanel viewPanel;
    private Controller controller;

    public View(Controller controller) {
        this.controller = controller;
        setSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        viewPanel = new ViewPanel();
        contentPane.add(viewPanel);
        setVisible(true);
    }

    @Override
    public void handleModelChange(EventObject e) {
        viewPanel.repaint();
    }

    public class ViewPanel extends JPanel implements MouseListener {

        public ViewPanel() {
            addMouseListener(this);
        }

        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            controller.draw(g2);
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            controller.click(arg0.getX(), arg0.getY());
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }

    }

}

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Controller {

    private Model model;

    public Controller(Model model) {
        this.model = model;
    }

    public void click(int x, int y) {
        model.addRectangle(x, y, 25, 25);
    }

    public void draw(Graphics2D g2) {
        for (Rectangle rectangle : model.getRectangles()) {
            g2.draw(rectangle);
        }
    }
}

import java.util.EventObject;

public interface ModelChangedEventListener {
    public void handleModelChange(EventObject e);
}

public class ModelChangeEvent extends java.util.EventObject {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public ModelChangeEvent(Object source) {
        super(source);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:05:26+00:00Added an answer on June 11, 2026 at 11:05 am

    You got it the wrong way around. One of the points of MVC is to make the model independent of the view. I.e the model shouldn’t know about the view (or rather it shouldn’t need to care). The view on the other hand needs to use model objects to do its thing. I’ve never designed a non-web application with MVC in mind. The view is normally a JSP/HTML/XHTML/XML file, its role is about making the program look in a certain way. So everything regarding rendering, placement, etc. fits into the view. The controllers job is to collect data from the model and pass it along to the view in a form that the view can use in a simple way, and also handle things like navigation between views. It’s the glue that keeps the view and model together. Thus:

    The model depends on nothing.
    The controller depends on the model.
    The view depends on the controller and model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.