瀏覽代碼

following my mvc style from the previous assignment more closely, adding some bare classes

tarfeef101 6 年之前
父節點
當前提交
a5482da437

+ 114 - 0
assignments/a3/src/main/java/Controls.java

@@ -0,0 +1,114 @@
+import java.io.*;
+import java.util.*;
+import java.awt.*;
+import javax.swing.*;
+import javax.swing.event.*;
+import java.awt.event.*;
+import javax.swing.ImageIcon;
+
+public class Controls extends JPanel implements Observer
+{
+  private Model model;
+  private playSlider playback;
+  private JButton play;
+  private JButton rewind;
+  private JButton reset;
+  private JButton clear;
+
+  // Bob the Builder this shit
+  public Controls(Model model, Handler handler)
+  {
+    // Hook up this observer so that it will be notified when the model
+    // changes.
+    this.model = model;
+    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
+    this.setPreferredSize(new Dimension(1280,75));
+    this.setMinimumSize(new Dimension(320, 75));
+    //ImageIcon icon1 = new ImageIcon("/src/main/resources/playbutton.jpg");
+    this.play = new JButton("Play");
+    play.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        
+      }
+    });
+    this.rewind = new JButton("Rewind");
+    rewind.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        
+      }
+    });
+    this.playback = new playSlider(0, 0, 0);
+    playback.addChangeListener(new ChangeListener()
+    {
+      public void stateChanged(ChangeEvent e)
+      {
+        if (!playback.getSettingLen() && playback.getValue() != model.getTotalVisibleLen())
+        {
+        }
+      }
+    });
+    this.playback.setPreferredSize(new Dimension(280, 75));
+    this.reset = new JButton("End");
+    reset.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        //playback.setValue(playback.getMaximum());
+      }
+    });
+    this.clear = new JButton("Start");
+    clear.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        //playback.setValue(playback.getMinimum());
+      }
+    });
+    this.add(play);
+    this.add(rewind);
+    this.add(playback);
+    this.add(clear);
+    this.add(reset);
+    model.addObserver(this);
+  }
+
+  public void update(Object observable)
+  {
+    int len = model.getTotalLen();
+    if (len > 0)
+    {
+      playback.toggleSettingLen();
+      playback.setMaximum(len);
+      playback.toggleSettingLen();
+      playback.setValue(model.getTotalVisibleLen());
+    }
+    this.setBackground(model.controlColour);
+    revalidate();
+    repaint();
+  }
+}
+
+class playSlider extends JSlider
+{
+  private boolean settingLen;
+  
+  public playSlider(int min, int max, int val)
+  {
+    super(min, max, val);
+    this.settingLen = false;
+  }
+  
+  public void toggleSettingLen()
+  {
+    this.settingLen = !settingLen;
+  }
+
+  public boolean getSettingLen()
+  {
+    return settingLen;
+  }
+}

+ 26 - 0
assignments/a3/src/main/java/Gallery.java

@@ -0,0 +1,26 @@
+import java.util.*;
+import java.awt.*;
+import javax.swing.*;
+
+public class Gallery extends JPanel implements Observer
+{
+  private Model model;
+
+  // Bob the Builder this shit
+  public Gallery(Model model)
+  {
+    // Hook up this observer so that it will be notified when the model
+    // changes.
+    this.model = model;
+    this.setBackground(Color.white);
+  }
+
+  /**
+  * Update with data from the model.
+  */
+  public void update(Object observable)
+  {
+    revalidate();
+    repaint();
+  }
+}

+ 25 - 5
assignments/a3/src/main/java/Main.java

@@ -1,7 +1,27 @@
+import javax.swing.*;
+import java.awt.*;
 
-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("Fotag!");
+    Model model = new Model();
+    Gallery gallery = new Gallery(model)
+    Controls controls = new Controls(model);
+    model.notifyObservers();
+    
+    // create a layout panel to hold the views
+    JPanel mainpanel = new JPanel(new BorderLayout(0, 0));
+    window.getContentPane().add(mainpanel);
+    mainpanel.add(controls, BorderLayout.CENTER);
+    mainpanel.add(controls, BorderLayout.PAGE_START);
+
+    // Setup the frame to do frame things
+    window.setPreferredSize(new Dimension(1280,720));
+    window.setMinimumSize(new Dimension(320, 180));
+    window.pack();
+    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+    window.setVisible(true);
+  }
 }

+ 56 - 29
assignments/a3/src/main/java/Model.java

@@ -1,37 +1,64 @@
-
+import java.util.Observable;
 import java.util.*;
+import java.awt.*;
+import javax.swing.*;
+import java.io.*;
 
-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 ArrayList<Observer> observers;
+  private ArrayList<PicData> pics;
 
-    /**
-     * Create a new model.
-     */
-    public Model() {
-        this.observers = new ArrayList();
-    }
+  // Constructor
+  public Model()
+  {
+    this.observers = new ArrayList();
+    this.pics = new ArrayList();
+    setChanged();
+  }
+  
+  public void newPic()
+  {
+    PicData temp = new PicData();
+    pics.add(temp);
+    setChanged();
+    notifyObservers();
+  }
 
-    /**
-     * 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);
     }
+  }
+}
+
+class PicData
+{
+  private int rating;
+
+  public PicData()
+  {
+    this.rating = 0;
+  }
+  
+  public void setRating(int x)
+  {
+    this.rating = x;
+  }
 }

+ 2 - 1
assignments/a3/src/main/java/Observer.java

@@ -3,7 +3,8 @@
  * An interface that allows an object to receive updates from the object
  * they listen to.
  */
-interface Observer {
+interface Observer
+{
     void update(Object observable);
 }
 

+ 0 - 37
assignments/a3/src/main/java/View.java

@@ -1,37 +0,0 @@
-
-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);
-
-        setVisible(true);
-    }
-
-    /**
-     * 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!");
-    }
-}