MainFrame.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.Toolkit;
  5. import java.awt.event.WindowEvent;
  6. import java.awt.event.WindowStateListener;
  7. import javax.swing.JFrame;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.Utility;
  10. /**
  11. * Main class of the GUI, which combines the UserInterface and visualization of the simulation.
  12. *
  13. * @author Andreas T. Meyer-Berg
  14. */
  15. @SuppressWarnings("serial")
  16. public class MainFrame extends JFrame {
  17. /**
  18. * Controller for manipulation of the model
  19. */
  20. private Controller controller;
  21. /**
  22. * MenuBar of the GUI
  23. */
  24. public MenuBar menu;
  25. /**
  26. * Panel which visualizes the devices, connections and links
  27. */
  28. public final VisualizationPanel panel;
  29. /**
  30. * Creates a new Frame for the program, which is the most outer frame of the application
  31. * @param c Controller which handles the user interaction
  32. */
  33. public MainFrame(Controller c) {
  34. //Set and control
  35. this.controller = c;
  36. setTitle("Smart Home Network Simulator");
  37. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. this.setIconImage(Utility.loadFile("images/smartHome_icon.png"));
  39. setLayout(new FlowLayout());
  40. //Set initial size of the frame
  41. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  42. int width = screenSize.width;
  43. int height = screenSize.height;
  44. this.setBounds(width/8, height/8, 6*width/8, 6*height/8);
  45. this.setMinimumSize(new Dimension(640, 480));
  46. /*
  47. * Add Visualization Panel
  48. */
  49. //Could be a more complex panel later on (TabbedPanel, ScrollPanel, SplitPanel
  50. panel = new VisualizationPanel(controller);
  51. this.setContentPane(panel);
  52. c.addObserver(panel);
  53. /*
  54. * Add Menu Bar
  55. */
  56. menu = new MenuBar(controller);
  57. super.setJMenuBar(menu);
  58. //Show Frame
  59. this.setVisible(true);
  60. //Set Dimension of the model to the JPanel size
  61. panel.delayedInit();
  62. //Update components if window was maximized
  63. this.addWindowStateListener(new WindowStateListener() {
  64. @Override
  65. public void windowStateChanged(WindowEvent e) {
  66. if ((e.getNewState() & MainFrame.MAXIMIZED_BOTH) == MainFrame.MAXIMIZED_BOTH){
  67. revalidate();
  68. panel.revalidate();
  69. panel.repaint();
  70. }
  71. }
  72. });
  73. // Panel should get Focus, so Clicks on panel would be recognized
  74. panel.requestFocusInWindow();
  75. }
  76. }