1 / 5

An MVC Example

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)

colm
Télécharger la présentation

An MVC Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An MVC Example

  2. 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

  3. 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

  4. 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)

  5. 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)

More Related