这是MVC吗?简单的Java Graphics2D绘图程序

时间:2012-09-17 05:18:25

标签: java model-view-controller graphics2d

我是在使用Java Graphics2D对MVC的尝试发表意见。 我想用一个简单的例子来了解MVC。

我说得对吗?

文档在这里。

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

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

反馈赞赏欢呼。

编辑:以下代码现已根据Rasmus的反馈和进一步的研究得到修复。

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 个答案:

答案 0 :(得分:3)

你得到了错误的方法。 MVC的一个要点是使模型独立于视图。即模型不应该知道视图(或者更确切地说它不需要关心)。另一方面,视图需要使用模型对象来完成它的工作。我从未设计过一个关于MVC的非Web应用程序。视图通常是JSP / HTML / XHTML / XML文件,其作用是使程序以某种方式显示。所以关于渲染,放置等的所有内容都适合视图。控制器的工作是从模型中收集数据,并以视图可以以简单方式使用的形式将其传递给视图,并处理视图之间的导航等操作。它是将视图和模型保持在一起的粘合剂。因此:

该模型取决于什么。 控制器取决于型号。 视图取决于控制器和型号。