Subversion Repositories sokoban

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 daniel-mar 1
/*============================================================================*/
2
 
3
package gdi1sokoban.gui;
4
 
5
/*============================================================================*/
6
 
7
import java.awt.event.KeyEvent;
8
import java.awt.event.KeyListener;
9
 
10
import javax.swing.JFrame;
11
 
12
import gdi1sokoban.exceptions.InternalFailureException;
13
import gdi1sokoban.exceptions.ParameterOutOfRangeException;
14
 
15
/*============================================================================*/
16
 
17
/**
18
 * Base class for the main game window. Derive from this class for implementing
19
 * your custom solution.
20
 *
21
 * @author Steven Arzt, Oren Avni
22
 * @version 1.0
23
 */
24
public abstract class GameWindow extends JFrame implements KeyListener {
25
 
26
        /* ======================================================================== */
27
 
28
        private static final long serialVersionUID = -2646785578035515024L;
29
 
30
        /* ======================================================================== */
31
 
32
        private SokobanPanel gamePanel = null;
33
 
34
        //private String skinPath = "";
35
 
36
        /* ======================================================================== */
37
 
38
        /**
39
         * Creates a new instance of the GameWindow class
40
         *
41
         * @param windowTitle The title of the game window
42
         * @throws InternalFailureException
43
         *                         Thrown if an uncorrectable internal error occurs
44
         */
45
        public GameWindow(final String windowTitle) throws InternalFailureException {
46
                super(windowTitle);
47
 
48
                // We may need to correct the window title
49
                if ((windowTitle == null) || windowTitle.equals(""))
50
                        setTitle("Sokoban Student Implementation");
51
 
52
                // Create the game panel
53
                gamePanel = createGamePanel ();
54
                if (gamePanel == null)
55
                        throw new RuntimeException("The game panel may not be null");
56
 
57
                // Configure the frame
58
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
59
                setResizable(false);
60
 
61
                // Draw the first state
62
                gamePanel.redraw();
63
        }
64
 
65
        /* ======================================================================== */
66
 
67
        /**
68
         * Override this method to create your own custom game panel.
69
         *
70
         * @return An instance of the GamePanel you want to use
71
         */
72
        protected abstract SokobanPanel createGamePanel ();
73
 
74
        /* ======================================================================== */
75
 
76
        /**
77
         * Method that is invoked whenever the left arrow key is pressed. Override
78
         * this method to implement your custom solution
79
         */
80
        protected abstract void keyLeftPressed();
81
 
82
        /* ======================================================================== */
83
 
84
        /**
85
         * Method that is invoked whenever the right arrow key is pressed. Override
86
         * this method to implement your custom solution
87
         */
88
        protected abstract void keyRightPressed();
89
 
90
        /* ======================================================================== */
91
 
92
        /**
93
         * Method that is invoked whenever the up arrow key is pressed. Override
94
         * this method to implement your custom solution
95
         */
96
        protected abstract void keyUpPressed();
97
 
98
        /* ======================================================================== */
99
 
100
        /**
101
         * Method that is invoked whenever the down arrow key is pressed. Override
102
         * this method to implement your custom solution
103
         */
104
        protected abstract void keyDownPressed();
105
 
106
        /* ======================================================================== */
107
 
108
        /**
109
         * Method that is invoked whenever the "n" key is pressed. Override this
110
         * method to implement your custom solution
111
         */
112
        protected abstract void keyNewGamePressed();
113
 
114
        /* ======================================================================== */
115
 
116
        /**
117
         * Method that is invoked whenever the "s" key is pressed. Override this
118
         * method to implement your custom solution
119
         */
120
        protected abstract void keySaveGamePressed();
121
 
122
        /* ======================================================================== */
123
 
124
        /**
125
         * Method that is invoked whenever the "l" key is pressed. Override this
126
         * method to implement your custom solution
127
         */
128
        protected abstract void keyLoadGamePressed();
129
 
130
        /* ======================================================================== */
131
 
132
        /* ======================================================================== */
133
 
134
        /**
135
         * Method that is invoked whenever the backspace key is pressed. Override
136
         * this method to implement your custom solution
137
         */
138
        protected abstract void keyUndoPressed();
139
 
140
        /* ======================================================================== */
141
 
142
        /**
143
         * Method that is invoked whenever the return key is pressed. Override this
144
         * method to implement your custom solution
145
         */
146
        protected abstract void keyRedoPressed();
147
 
148
        /* ======================================================================== */
149
 
150
        /**
151
         * Method that is invoked whenever a key that is not explicitly handled has
152
         * been pressed. Override this method to implement your custom solution.
153
         */
154
        protected abstract void keyOtherPressed (KeyEvent key);
155
 
156
        /* ======================================================================== */
157
 
158
        /**
159
         * This method consumes a KeyEvent caused by the user pressing a key. If the
160
         * key is "known", the appropriate method key*Pressed will be called.
161
         *
162
         * @see #keyUpPressed()
163
         * @see #keyLeftPressed()
164
         * @see #keyRightPressed()
165
         * @see #keyDownPressed()
166
         * @see #keyNewGamePressed()
167
         * @see #keyRedoPressed()
168
         * @see #keyUndoPressed()
169
         */
170
        public void keyPressed(final KeyEvent key) {
171
                // retrieve the key code and call the appropriate method, if any
172
                switch (key.getKeyCode()) {
173
                case KeyEvent.VK_LEFT:
174
                        keyLeftPressed();
175
                        break;
176
 
177
                case KeyEvent.VK_UP:
178
                        keyUpPressed();
179
                        break;
180
 
181
                case KeyEvent.VK_RIGHT:
182
                        keyRightPressed();
183
                        break;
184
 
185
                case KeyEvent.VK_DOWN:
186
                        keyDownPressed();
187
                        break;
188
 
189
                case KeyEvent.VK_Q:
190
                        System.exit(0);
191
                        break;
192
 
193
                case KeyEvent.VK_N:
194
                        keyNewGamePressed();
195
                        break;
196
 
197
                case KeyEvent.VK_BACK_SPACE:
198
                        keyUndoPressed();
199
                        break;
200
 
201
                case KeyEvent.VK_ENTER:
202
                        keyRedoPressed();
203
                        break;
204
 
205
                case KeyEvent.VK_ESCAPE:
206
                        keyResetCheat();
207
                        break;
208
 
209
                default:
210
                        keyOtherPressed (key);
211
                        break;
212
                }
213
        }
214
 
215
        /* ======================================================================== */
216
 
217
        public void keyResetCheat()
218
        {
219
                // nothing to be done here              
220
        }
221
 
222
        public void keyReleased(final KeyEvent key) {
223
                // nothing to be done here
224
        }
225
 
226
        /* ======================================================================== */
227
 
228
        public void keyTyped(final KeyEvent key) {
229
                // nothing to be done here
230
        }
231
 
232
        /* ======================================================================== */
233
 
234
        /**
235
         * Returns the game panel used by this game window
236
         *
237
         * @return The game panel used by this game window
238
         */
239
        public SokobanPanel getGamePanel() {
240
                return gamePanel;
241
        }
242
 
243
        /* ======================================================================== */
244
 
245
        /**
246
         * Notifies the game window that a new level has been loaded
247
         *
248
         * @param width
249
         *            The width of the level just loaded
250
         * @param height
251
         *            The height of the level just loaded
252
         * @throws ParameterOutOfRangeException
253
         *             Thrown if one of the parameters falls out of the range of
254
         *             acceptable values
255
         * @throws InternalFailureException
256
         *                         Thrown if an uncorrectable internal error occurs
257
         */
258
        public void notifyLevelLoaded(final int width, final int height)
259
                        throws ParameterOutOfRangeException, InternalFailureException {
260
                // Check the parameters
261
                if (width <= 0)
262
                        throw new ParameterOutOfRangeException("Game Window width invalid");
263
                if (height <= 0)
264
                        throw new ParameterOutOfRangeException("Game Window height invalid");
265
 
266
                // Notify the panel
267
                gamePanel.notifyLevelLoaded(width, height);
268
        }
269
 
270
 
271
 
272
        public void showAbout()
273
        {
274
                // nothing to be done here              
275
        }
276
        /* ======================================================================== */
277
 
278
}
279
 
280
/* ============================================================================ */