Main.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6. import java.io.*;
  7. import java.lang.Throwable;
  8. import javax.imageio.ImageIO;
  9. public class Main
  10. {
  11. public static Model model;
  12. public static Gallery gallery;
  13. static class myAdapter extends ComponentAdapter
  14. {
  15. public void componentResized(ComponentEvent ce)
  16. {
  17. boolean temp = model.getLayout();
  18. model.switchLayout(!temp);
  19. model.switchLayout(temp);
  20. }
  21. }
  22. public static myAdapter listener;
  23. public static void main(String[] args)
  24. {
  25. JFrame window = new JFrame("Fotag!");
  26. model = new Model();
  27. listener = new myAdapter();
  28. window.addComponentListener(listener);
  29. try
  30. {
  31. FileInputStream file = new FileInputStream("savestate.gal");
  32. ObjectInputStream object = new ObjectInputStream(file);
  33. model = (Model)object.readObject();
  34. object.close();
  35. for (PicData p: model.getPics())
  36. {
  37. String filepath = p.getFile();
  38. Image img = null;
  39. try
  40. {
  41. img = ImageIO.read(new File(filepath));
  42. } catch(IOException e) {}
  43. img = img.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH);
  44. p.setPic(img);
  45. }
  46. }
  47. catch (Exception e) {System.out.println("Playing baseball");}
  48. gallery = new Gallery(model);
  49. Controls controls = new Controls(model);
  50. model.notifyObservers();
  51. // create a layout panel to hold the views
  52. JPanel mainpanel = new JPanel(new BorderLayout(0, 0));
  53. window.getContentPane().add(mainpanel);
  54. JScrollPane scrollGallery = new JScrollPane(gallery);
  55. scrollGallery.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  56. scrollGallery.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  57. mainpanel.add(scrollGallery, BorderLayout.CENTER);
  58. mainpanel.add(controls, BorderLayout.PAGE_START);
  59. // Setup the frame to do frame things
  60. window.setPreferredSize(new Dimension(1280,720));
  61. window.setMinimumSize(new Dimension(500, 475));
  62. window.pack();
  63. //window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64. window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  65. window.addWindowListener(new WindowAdapter()
  66. {
  67. public void windowClosing(WindowEvent we)
  68. {
  69. String ObjButtons[] = {"Yes","No"};
  70. int PromptResult = JOptionPane.showOptionDialog(null,"Do you want to save your files?","",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,ObjButtons,ObjButtons[1]);
  71. if(PromptResult==JOptionPane.YES_OPTION)
  72. {
  73. try
  74. {
  75. ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("savestate.gal"));
  76. output.writeObject(model);
  77. output.close();
  78. } catch (Exception e)
  79. {
  80. System.out.println("Screw you ankil");
  81. e.printStackTrace();
  82. }
  83. }
  84. System.exit(0);
  85. }
  86. });
  87. window.setVisible(true);
  88. }
  89. }