Gallery.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import java.util.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. public class Gallery extends JPanel implements Observer
  5. {
  6. private Model model;
  7. private ArrayList<Item> items;
  8. // Bob the Builder this shit
  9. public Gallery(Model model)
  10. {
  11. // Hook up this observer so that it will be notified when the model
  12. // changes.
  13. this.model = model;
  14. this.setLayout();
  15. this.setBackground(Color.white);
  16. this.items = new ArrayList();
  17. reList();
  18. reFill(3);
  19. }
  20. /**
  21. * Update with data from the model.
  22. */
  23. public void update(Object observable)
  24. {
  25. reList();
  26. reFill(3);
  27. revalidate();
  28. repaint();
  29. }
  30. private void reList()
  31. {
  32. ArrayList<PicData> temp = model.getPics();
  33. this.items.clear();
  34. for (PicData p: temp)
  35. {
  36. this.items.add(p);
  37. }
  38. }
  39. private void reFill(int cols)
  40. {
  41. if (model.getGrid())
  42. {
  43. this.setLayout(new GridLayout(0, cols));
  44. }
  45. else
  46. {
  47. this.setLayout(new GridLayout(0, 1));
  48. }
  49. this.removeAll();
  50. for (Item i: items)
  51. {
  52. this.add(i);
  53. }
  54. }
  55. }
  56. class Item extends JPanel
  57. {
  58. private PicData data;
  59. private JLabel pic;
  60. private JLabel name;
  61. private JLabel rating;
  62. private JLabel dateSize;
  63. public Item(PicData p)
  64. {
  65. this.data = p;
  66. this.setLayout(new GridLayout(0, 1));
  67. this.setPreferredSize(100, 150);
  68. this.pic = new JLabel(data.getPic());
  69. this.name = new JLabel(data.getName());
  70. this.rating = new JLabel(data.getRating().toString());
  71. this.dateSize = new JLabel(data.getDate().toString() + data.getSize().toString());
  72. this.add(pic);
  73. this.add(name);
  74. this.add(rating);
  75. this.add(dateSize);
  76. }
  77. }