Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- // HelloMVC: a simple MVC example
- // the model is just a counter
- // inspired by code by Joseph Mack, http://www.austintek.com/mvc/
- // View interface
- interface IView {
- public void updateView();
- }
- public class Model {
- private boolean currentMode; //true select, false draw
- private String currentTool; //Line, ellipse etc.
- private int currentThickness; //Stroke thickness.
- private
- // the data in the model, just a counter
- private int counter;
- // all views of this model
- private ArrayList<IView> views = new ArrayList<IView>();
- //set whether we are drawing or selecting
- public void setMode(String s) {
- if(hdrawmap.get(s) == 0) {
- hdrawmap.put(s, hdrawmap.get(s) + 1);
- }
- //change all other values to 0
- for (String key : hdrawmap.keySet()) {
- if(key != s) {
- hdrawmap.put(key, 0);
- }
- }
- }
- //set what the current drawing tool is
- public void setTool(String s) {
- if(htoolMap.get(s) == 0) {
- htoolMap.put(s, htoolMap.get(s) + 1);
- }
- //change all other values to 0
- for (String key : htoolMap.keySet()) {
- if(key != s) {
- htoolMap.put(key, 0);
- }
- }
- }
- // set the view observer
- public void addView(IView view) {
- views.add(view);
- // update the view to current state of the model
- view.updateView();
- }
- public int getCounterValue() {
- return counter;
- }
- public void incrementCounter() {
- if (counter < 5) {
- counter++;
- System.out.println("Model: increment counter to " + counter);
- notifyObservers();
- }
- }
- // notify the IView observer
- private void notifyObservers() {
- for (IView view : this.views) {
- System.out.println("Model: notify View");
- view.updateView();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement