Improved code readability
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / ViewFrame.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.gui;
6
7 import javax.swing.*;
8 import java.awt.*;
9 import java.awt.event.ComponentEvent;
10 import java.awt.event.ComponentListener;
11 import java.awt.event.WindowEvent;
12 import java.awt.event.WindowListener;
13
14 public class ViewFrame extends JFrame implements WindowListener {
15
16     private static final long serialVersionUID = -7037635097739548470L;
17
18     private final ViewPanel viewPanel;
19
20     public ViewFrame() {
21         setTitle("3D engine");
22
23         addWindowListener(new java.awt.event.WindowAdapter() {
24             @Override
25             public void windowClosing(final java.awt.event.WindowEvent e) {
26                 exit();
27             }
28         });
29
30         viewPanel = new ViewPanel();
31
32         add(getViewPanel());
33
34         setSize(800, 600);
35
36         // maximize window
37         setExtendedState(JFrame.MAXIMIZED_BOTH);
38         setVisible(true);
39
40         addResizeListener();
41         addWindowListener(this);
42     }
43
44     private void addResizeListener() {
45         addComponentListener(new ComponentListener() {
46             // This method is called after the component's size changes
47             @Override
48             public void componentHidden(final ComponentEvent e) {
49             }
50
51             @Override
52             public void componentMoved(final ComponentEvent e) {
53             }
54
55             @Override
56             public void componentResized(final ComponentEvent evt) {
57
58                 final Component c = (Component) evt.getSource();
59
60                 // Get new size
61                 final Dimension newSize = c.getSize();
62
63                 boolean sizeFixed = false;
64
65                 if (newSize.width < 400) {
66                     newSize.width = 400;
67                     sizeFixed = true;
68                 }
69
70                 if (newSize.height < 400) {
71                     newSize.height = 400;
72                     sizeFixed = true;
73                 }
74
75                 if (sizeFixed)
76                     setSize(newSize);
77
78             }
79
80             @Override
81             public void componentShown(final ComponentEvent e) {
82                 viewPanel.repaintDuringNextViewUpdate();
83             }
84
85         });
86     }
87
88     public void exit() {
89         if (getViewPanel() != null) {
90             getViewPanel().stop();
91             getViewPanel().setEnabled(false);
92             getViewPanel().setVisible(false);
93         }
94         dispose();
95     }
96
97     @Override
98     public java.awt.Dimension getPreferredSize() {
99         return new java.awt.Dimension(640, 480);
100     }
101
102     /**
103      * @return the view
104      */
105     public ViewPanel getViewPanel() {
106         return viewPanel;
107     }
108
109     @Override
110     public void windowActivated(final WindowEvent e) {
111         viewPanel.repaintDuringNextViewUpdate();
112     }
113
114     @Override
115     public void windowClosed(final WindowEvent e) {
116     }
117
118     @Override
119     public void windowClosing(final WindowEvent e) {
120     }
121
122     @Override
123     public void windowDeactivated(final WindowEvent e) {
124     }
125
126     @Override
127     public void windowDeiconified(final WindowEvent e) {
128         viewPanel.repaintDuringNextViewUpdate();
129     }
130
131     @Override
132     public void windowIconified(final WindowEvent e) {
133     }
134
135     @Override
136     public void windowOpened(final WindowEvent e) {
137         viewPanel.repaintDuringNextViewUpdate();
138     }
139
140 }