/*============================================================================*/
package gdi1sokoban.gui;
/*============================================================================*/
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import gdi1sokoban.exceptions.InternalFailureException;
import gdi1sokoban.exceptions.ParameterOutOfRangeException;
/*============================================================================*/
/**
* Base class for the main game window. Derive from this class for implementing
* your custom solution.
*
* @author Steven Arzt, Oren Avni
* @version 1.0
*/
/* ======================================================================== */
private static final long serialVersionUID = -2646785578035515024L;
/* ======================================================================== */
private SokobanPanel gamePanel = null;
//private String skinPath = "";
/* ======================================================================== */
/**
* Creates a new instance of the GameWindow class
*
* @param windowTitle The title of the game window
* @throws InternalFailureException
* Thrown if an uncorrectable internal error occurs
*/
public GameWindow
(final String windowTitle
) throws InternalFailureException
{
super(windowTitle);
// We may need to correct the window title
if ((windowTitle == null) || windowTitle.equals(""))
setTitle("Sokoban Student Implementation");
// Create the game panel
gamePanel = createGamePanel ();
if (gamePanel == null)
// Configure the frame
setDefaultCloseOperation
(JFrame.
EXIT_ON_CLOSE);
setResizable(false);
// Draw the first state
gamePanel.redraw();
}
/* ======================================================================== */
/**
* Override this method to create your own custom game panel.
*
* @return An instance of the GamePanel you want to use
*/
protected abstract SokobanPanel createGamePanel ();
/* ======================================================================== */
/**
* Method that is invoked whenever the left arrow key is pressed. Override
* this method to implement your custom solution
*/
protected abstract void keyLeftPressed();
/* ======================================================================== */
/**
* Method that is invoked whenever the right arrow key is pressed. Override
* this method to implement your custom solution
*/
protected abstract void keyRightPressed();
/* ======================================================================== */
/**
* Method that is invoked whenever the up arrow key is pressed. Override
* this method to implement your custom solution
*/
protected abstract void keyUpPressed();
/* ======================================================================== */
/**
* Method that is invoked whenever the down arrow key is pressed. Override
* this method to implement your custom solution
*/
protected abstract void keyDownPressed();
/* ======================================================================== */
/**
* Method that is invoked whenever the "n" key is pressed. Override this
* method to implement your custom solution
*/
protected abstract void keyNewGamePressed();
/* ======================================================================== */
/**
* Method that is invoked whenever the "s" key is pressed. Override this
* method to implement your custom solution
*/
protected abstract void keySaveGamePressed();
/* ======================================================================== */
/**
* Method that is invoked whenever the "l" key is pressed. Override this
* method to implement your custom solution
*/
protected abstract void keyLoadGamePressed();
/* ======================================================================== */
/* ======================================================================== */
/**
* Method that is invoked whenever the backspace key is pressed. Override
* this method to implement your custom solution
*/
protected abstract void keyUndoPressed();
/* ======================================================================== */
/**
* Method that is invoked whenever the return key is pressed. Override this
* method to implement your custom solution
*/
protected abstract void keyRedoPressed();
/* ======================================================================== */
/**
* Method that is invoked whenever a key that is not explicitly handled has
* been pressed. Override this method to implement your custom solution.
*/
protected abstract void keyOtherPressed
(KeyEvent key
);
/* ======================================================================== */
/**
* This method consumes a KeyEvent caused by the user pressing a key. If the
* key is "known", the appropriate method key*Pressed will be called.
*
* @see #keyUpPressed()
* @see #keyLeftPressed()
* @see #keyRightPressed()
* @see #keyDownPressed()
* @see #keyNewGamePressed()
* @see #keyRedoPressed()
* @see #keyUndoPressed()
*/
public void keyPressed
(final KeyEvent key
) {
// retrieve the key code and call the appropriate method, if any
switch (key.getKeyCode()) {
keyLeftPressed();
break;
keyUpPressed();
break;
keyRightPressed();
break;
keyDownPressed();
break;
break;
keyNewGamePressed();
break;
keyUndoPressed();
break;
keyRedoPressed();
break;
keyResetCheat();
break;
default:
keyOtherPressed (key);
break;
}
}
/* ======================================================================== */
public void keyResetCheat()
{
// nothing to be done here
}
public void keyReleased
(final KeyEvent key
) {
// nothing to be done here
}
/* ======================================================================== */
public void keyTyped
(final KeyEvent key
) {
// nothing to be done here
}
/* ======================================================================== */
/**
* Returns the game panel used by this game window
*
* @return The game panel used by this game window
*/
public SokobanPanel getGamePanel() {
return gamePanel;
}
/* ======================================================================== */
/**
* Notifies the game window that a new level has been loaded
*
* @param width
* The width of the level just loaded
* @param height
* The height of the level just loaded
* @throws ParameterOutOfRangeException
* Thrown if one of the parameters falls out of the range of
* acceptable values
* @throws InternalFailureException
* Thrown if an uncorrectable internal error occurs
*/
public void notifyLevelLoaded(final int width, final int height)
throws ParameterOutOfRangeException, InternalFailureException {
// Check the parameters
if (width <= 0)
throw new ParameterOutOfRangeException("Game Window width invalid");
if (height <= 0)
throw new ParameterOutOfRangeException("Game Window height invalid");
// Notify the panel
gamePanel.notifyLevelLoaded(width, height);
}
public void showAbout()
{
// nothing to be done here
}
/* ======================================================================== */
}
/* ============================================================================ */