Advertisement
Guest User

BACKUP349

a guest
Feb 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. // HelloMVC: a simple MVC example
  4. // the model is just a counter
  5. // inspired by code by Joseph Mack, http://www.austintek.com/mvc/
  6.  
  7. // View interface
  8. interface IView {
  9. public void updateView();
  10. }
  11.  
  12. public class Model {
  13. private boolean currentMode; //true select, false draw
  14. private String currentTool; //Line, ellipse etc.
  15. private int currentThickness; //Stroke thickness.
  16.  
  17.  
  18.  
  19. private
  20.  
  21.  
  22. // the data in the model, just a counter
  23. private int counter;
  24. // all views of this model
  25. private ArrayList<IView> views = new ArrayList<IView>();
  26.  
  27. //set whether we are drawing or selecting
  28. public void setMode(String s) {
  29. if(hdrawmap.get(s) == 0) {
  30. hdrawmap.put(s, hdrawmap.get(s) + 1);
  31. }
  32. //change all other values to 0
  33. for (String key : hdrawmap.keySet()) {
  34. if(key != s) {
  35. hdrawmap.put(key, 0);
  36. }
  37. }
  38. }
  39.  
  40. //set what the current drawing tool is
  41. public void setTool(String s) {
  42. if(htoolMap.get(s) == 0) {
  43. htoolMap.put(s, htoolMap.get(s) + 1);
  44. }
  45. //change all other values to 0
  46. for (String key : htoolMap.keySet()) {
  47. if(key != s) {
  48. htoolMap.put(key, 0);
  49. }
  50. }
  51. }
  52.  
  53. // set the view observer
  54. public void addView(IView view) {
  55. views.add(view);
  56. // update the view to current state of the model
  57. view.updateView();
  58. }
  59.  
  60. public int getCounterValue() {
  61. return counter;
  62. }
  63.  
  64. public void incrementCounter() {
  65. if (counter < 5) {
  66. counter++;
  67. System.out.println("Model: increment counter to " + counter);
  68. notifyObservers();
  69. }
  70. }
  71.  
  72. // notify the IView observer
  73. private void notifyObservers() {
  74. for (IView view : this.views) {
  75. System.out.println("Model: notify View");
  76. view.updateView();
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement