ソースを参照

changing MVC to reflect a paradigm i find nicer

tarfeef101 6 年 前
コミット
2a3604d82e

+ 24 - 5
assignments/a2/src/main/java/Main.java

@@ -1,7 +1,26 @@
+import javax.swing.*;
+import java.awt.Dimension;
+import java.awt.GridLayout;
+import java.awt.event.*;
 
-public class Main {
-    public static void main(String[] args) {
-        Model model = new Model();
-        View view = new View(model);
-    }
+public class Main
+{
+  public static void main(String[] args)
+  {
+    JFrame window = new JFrame("Paint, but worse");
+    Model model = new Model();
+    View view = new View(model);
+    model.notifyObservers();
+    
+    // create a layout panel to hold the views
+		JPanel mainpanel = new JPanel(new BorderLayout(10, 10));
+		window.getContentPane().add(mainpanel);
+		mainpanel.add(view, BorderLayout.CENTER);
+		
+		// Setup the frame to do frame things
+		frame.setPreferredSize(new Dimension(300,300));
+		frame.pack();
+		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+		frame.setVisible(true);
+  }
 }

+ 28 - 29
assignments/a2/src/main/java/Model.java

@@ -1,37 +1,36 @@
-
+import java.util.Observable;
 import java.util.*;
 
-public class Model {
-    /** The observers that are watching this model for changes. */
-    private List<Observer> observers;
+public class Model extends Observable
+{
+   /** The observers that are watching this model for changes. */
+  private List<Observer> observers;
 
-    /**
-     * Create a new model.
-     */
-    public Model() {
-        this.observers = new ArrayList();
-    }
+  // Constructor
+  public Model()
+  {
+    this.observers = new ArrayList();
+    setChanged();
+  }
 
-    /**
-     * Add an observer to be notified when this model changes.
-     */
-    public void addObserver(Observer observer) {
-        this.observers.add(observer);
-    }
+  // Add observer to be notified on change
+  public void addObserver(Observer observer)
+  {
+    this.observers.add(observer);
+  }
 
-    /**
-     * Remove an observer from this model.
-     */
-    public void removeObserver(Observer observer) {
-        this.observers.remove(observer);
-    }
+  // Remove an observer from opdate list
+  public void removeObserver(Observer observer)
+  {
+    this.observers.remove(observer);
+  }
 
-    /**
-     * Notify all observers that the model has changed.
-     */
-    public void notifyObservers() {
-        for (Observer observer: this.observers) {
-            observer.update(this);
-        }
+   // Notify all observers shit went down
+  public void notifyObservers()
+  {
+    for (Observer observer: this.observers)
+    {
+      observer.update(this);
     }
+  }
 }

+ 20 - 29
assignments/a2/src/main/java/View.java

@@ -1,37 +1,28 @@
-
 import java.io.*;
 import java.util.*;
 import java.awt.*;
 import javax.swing.*;
 
-public class View extends JFrame implements Observer {
-
-    private Model model;
-
-    /**
-     * Create a new View.
-     */
-    public View(Model model) {
-        // Set up the window.
-        this.setTitle("Your program title here!");
-        this.setMinimumSize(new Dimension(128, 128));
-        this.setSize(800, 600);
-        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-        // Hook up this observer so that it will be notified when the model
-        // changes.
-        this.model = model;
-        model.addObserver(this);
+public class View extends JPanel implements Observer
+{
+  private Model model;
 
-        setVisible(true);
-    }
+  // Bob the Builder this shit
+  public View(Model model)
+  {
+    // Hook up this observer so that it will be notified when the model
+    // changes.
+    this.model = model;
+    model.addObserver(this);
+  }
 
-    /**
-     * Update with data from the model.
-     */
-    public void update(Object observable) {
-        // XXX Fill this in with the logic for updating the view when the model
-        // changes.
-        System.out.println("Model changed!");
-    }
+  /**
+  * Update with data from the model.
+  */
+  public void update(Object observable)
+  {
+    System.out.println("Model changed!");
+    revalidate();
+    repaint();
+  }
 }