50 likes | 133 Vues
An MVC Example. The thief and house program we used last time is refactored to follow the MVC pattern Now it consists of four parts: AScene (model) AThiefController (controller 1) AThiefMouseController (controller 2) ASceneView (view)
E N D
The thief and house program we used last time is refactored to follow the MVC pattern • Now it consists of four parts: • AScene (model) • AThiefController (controller 1) • AThiefMouseController (controller 2) • ASceneView (view) Note that at this point we do not use objectEditor to display the scene. Thief and House Refactored
View uses Java. awt to draw lines, images, or texts. • View extends Component . • The second property of view, BasicStrokedotted, determines the style of the output • Pay attention to the paint() method and different draw() method and their relations. • Notice that the view is an observer of the scene, so that it can update any change in the scene. • repaint() method call in propertyChange will draw the whole scene again. View
The button just move the thief to the right. • Notice that the button object is not the same thing as the button controller. You need to manually set the button controller as the action listener of the button. So the controller also implements ActionListener. • Once the button is pressed, it automatically call the actionPerformed() method to move the thief Button Controller (AThiefController)
The mouse controller moves the thief to where the mouse click in the view. • aView.addMouseListener(this); This line enable the controller to listen to the mouse event in the view. As a Component, view naturally support addMouseListener() method. • There are a couple of different mouse events available. This program just uses mouse click event. • The event e, which is the argument of the method, contains information of the event. For example, in this program use it to retrieve the position of click. Mouse Controller (AThiefMouseController)