Browse Source

trying to display shit

tarfeef101 6 năm trước cách đây
mục cha
commit
905d02c2f4

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

@@ -5,6 +5,7 @@ import javax.swing.*;
 public class Gallery extends JPanel implements Observer
 {
   private Model model;
+  private ArrayList<Item> items;
 
   // Bob the Builder this shit
   public Gallery(Model model)
@@ -12,7 +13,11 @@ public class Gallery extends JPanel implements Observer
     // Hook up this observer so that it will be notified when the model
     // changes.
     this.model = model;
+    this.setLayout();
     this.setBackground(Color.white);
+    this.items = new ArrayList();
+    reList();
+    reFill(3);
   }
 
   /**
@@ -20,7 +25,62 @@ public class Gallery extends JPanel implements Observer
   */
   public void update(Object observable)
   {
+    reList();
+    reFill(3);
     revalidate();
     repaint();
   }
+  
+  private void reList()
+  {
+    ArrayList<PicData> temp = model.getPics();
+    this.items.clear();
+    for (PicData p: temp)
+    {
+      this.items.add(p);
+    }
+  }
+  
+  private void reFill(int cols)
+  {
+    if (model.getGrid())
+    {
+      this.setLayout(new GridLayout(0, cols));
+    }
+    else
+    {
+      this.setLayout(new GridLayout(0, 1));
+    }
+    
+    this.removeAll();
+    
+    for (Item i: items)
+    {
+      this.add(i);
+    }
+  }
+}
+
+class Item extends JPanel
+{
+  private PicData data;
+  private JLabel pic;
+  private JLabel name;
+  private JLabel rating;
+  private JLabel dateSize;
+
+  public Item(PicData p)
+  {
+    this.data = p;
+    this.setLayout(new GridLayout(0, 1));
+    this.setPreferredSize(100, 150);
+    this.pic = new JLabel(data.getPic());
+    this.name = new JLabel(data.getName());
+    this.rating = new JLabel(data.getRating().toString());
+    this.dateSize = new JLabel(data.getDate().toString() + data.getSize().toString());
+    this.add(pic);
+    this.add(name);
+    this.add(rating);
+    this.add(dateSize);
+  }
 }

+ 35 - 0
assignments/a3/src/main/java/Model.java

@@ -31,6 +31,16 @@ public class Model extends Observable
     notifyObservers();
   }
   
+  public ArrayList<PicData> getPics()
+  {
+    return pics;
+  }
+  
+  public boolean getGrid()
+  {
+    return grid;
+  }
+  
   public void changeFilter(int x)
   {
     this.filter = x;
@@ -93,4 +103,29 @@ class PicData
   {
     this.rating = x;
   }
+  
+  public Image getPic()
+  {
+    return pic;
+  }
+  
+  public int getRating()
+  {
+    return rating;
+  }
+  
+  public long getSize()
+  {
+    return size;
+  }
+  
+  public long getLastmod()
+  {
+    return lastmod;
+  }
+  
+  public String getName()
+  {
+    return name;
+  }
 }