mhframework
Class MHScreen

java.lang.Object
  extended by mhframework.MHScreen
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener, MHRenderable
Direct Known Subclasses:
MHGUIDialogScreen

public abstract class MHScreen
extends java.lang.Object
implements MHRenderable, java.awt.event.ActionListener

This is an abstract base class for deriving game screens. It provides the following required pieces:

How to create a new game screen:

  1. Create a class that extends MHScreen. Recommendation: Derive a fairly generic class from MHScreen, then use that class as your base for the specific game screens. This will provide the flexibility of processing events to satisfy your application's needs, while maintaining the reusability of this MHScreen class.
  2. Define the following methods:
  3. Add the screen to the MHScreenManager object for your application.

And that's it! The screen manager (MHScreenManager) will hold the screen while the game class (MHGame) runs it. Note that neither MHScreenManager nor MHGame need to be extended. They were designed to work autonomously in their current forms.

How to change screens in an application:

  1. Define multiple subclasses of MHScreen.
  2. When it is time to change to the next screen, call setNextScreen() and pass it a reference to the next screen to be displayed. Then do this: setFinished(true);
  3. To go back to the previous screen:
    setNextScreen(null);
    setFinished(true);

Author:
Michael Henson

Constructor Summary
MHScreen()
          Default constructor.
 
Method Summary
 void add(MHGUIComponent c)
          Adds an MHGUIComponent object to the screen.
 void advance()
          Tells an object to update its data.
 void centerText(java.awt.Graphics g, java.lang.String text, int y, java.awt.Color c, boolean shadowed, int shadowDistance)
           
 MHGUIComponentList getComponentList()
           
 MHGUIComponent getFocusedComponent()
          Return a reference to the GUI component that currently has the focus.
 MHScreen getNextScreen()
          Returns a reference to the next screen to be pushed.
 MHScreen getPreviousScreen()
           
 void hideErrorMessage()
           
 boolean isDisposable()
           
 boolean isFinished()
          Returns the value of the "finished" flag.
 void keyPressed(java.awt.event.KeyEvent e)
          Delivers Key Pressed events to the GUI components.
 void keyReleased(java.awt.event.KeyEvent e)
          Delivers Key Released events to the GUI components.
 void keyTyped(java.awt.event.KeyEvent e)
          Delivers Key Typed events to the GUI components.
abstract  void load()
          Set up a screen and prepare it to run.
 void mouseClicked(java.awt.event.MouseEvent e)
          Delivers Mouse Clicked events to the GUI components.
 void mouseMoved(java.awt.event.MouseEvent e)
          Delivers Mouse Moved events to the GUI components.
 void mousePressed(java.awt.event.MouseEvent e)
          Delivers Mouse Pressed events to the GUI components.
 void mouseReleased(java.awt.event.MouseEvent e)
          Delivers Mouse Released events to the GUI components.
 void nextFocusableComponent()
          Sets the focus to the next component capable of receiving focus.
 void prevFocusableComponent()
          Sets the focus to the previous component capable of receiving focus.
 void remove(MHGUIComponent c)
          Removes an MHGUIComponent object from the screen.
 void render(java.awt.Graphics2D g)
          Draws the GUI components onto the sent Graphics object.
 void setDisposable(boolean disposable)
           
 void setFinished(boolean f)
          Sets the value of the "finished" flag to the input parameter.
 void setNextScreen(MHScreen nextScreen)
          Sets a reference to the next screen to be pushed when this one is finished.
 void setPreviousScreen(MHScreen previous)
           
 void showErrorMessage(java.lang.String text)
           
static void tileImage(java.awt.Graphics2D g, java.awt.Image image, int x, int y)
           
abstract  void unload()
          Perform termination housekeeping or reset the screen variables when a screen has finished executing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.awt.event.ActionListener
actionPerformed
 

Constructor Detail

MHScreen

public MHScreen()
Default constructor.

Method Detail

getComponentList

public MHGUIComponentList getComponentList()

load

public abstract void load()
Set up a screen and prepare it to run. This method is called before every time this screen is displayed.


unload

public abstract void unload()
Perform termination housekeeping or reset the screen variables when a screen has finished executing.


add

public void add(MHGUIComponent c)
Adds an MHGUIComponent object to the screen.

Parameters:
c - The component to be added

remove

public void remove(MHGUIComponent c)
Removes an MHGUIComponent object from the screen.

Parameters:
c - The component to be removed

getFocusedComponent

public MHGUIComponent getFocusedComponent()
Return a reference to the GUI component that currently has the focus.


nextFocusableComponent

public void nextFocusableComponent()
Sets the focus to the next component capable of receiving focus. If the search for this component reaches the end of the component list, it wraps around to the beginning and continues until a focusable component is located.


prevFocusableComponent

public void prevFocusableComponent()
Sets the focus to the previous component capable of receiving focus. If the search for this component reaches the beginning of the component list, it wraps around to the end and continues until a focusable component is located.


setNextScreen

public void setNextScreen(MHScreen nextScreen)
Sets a reference to the next screen to be pushed when this one is finished. If the application should return to the previous screen, the next screen can be set to null to indicate this.

Parameters:
nextScreen - The next screen to be displayed

getNextScreen

public MHScreen getNextScreen()
Returns a reference to the next screen to be pushed. If this method returns null, this will cause the current screen to be popped from the stack and the previous screen will be displayed.


render

public void render(java.awt.Graphics2D g)
Draws the GUI components onto the sent Graphics object. This method is intended to be overridden by methods that do their own specialized rendering, then call this base-class version to draw the components.

Specified by:
render in interface MHRenderable
Parameters:
g - The Graphics object on which this object is to draw itself.

advance

public void advance()
Description copied from interface: MHRenderable
Tells an object to update its data.

Specified by:
advance in interface MHRenderable

centerText

public void centerText(java.awt.Graphics g,
                       java.lang.String text,
                       int y,
                       java.awt.Color c,
                       boolean shadowed,
                       int shadowDistance)

showErrorMessage

public void showErrorMessage(java.lang.String text)

hideErrorMessage

public void hideErrorMessage()

setFinished

public void setFinished(boolean f)
Sets the value of the "finished" flag to the input parameter.


isFinished

public boolean isFinished()
Returns the value of the "finished" flag.

Returns:
True if the screen has finished executing, false otherwise.

isDisposable

public boolean isDisposable()
Returns:
A boolean indicating whether this screen is disposable.

setDisposable

public void setDisposable(boolean disposable)
Parameters:
disposable - A flag to indicate that screen can be deallocated.

tileImage

public static void tileImage(java.awt.Graphics2D g,
                             java.awt.Image image,
                             int x,
                             int y)

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Delivers Key Pressed events to the GUI components. This method is intended to be overridden in derived classes which may handle the event on their own, then optionally call this version.

Parameters:
e - The event that triggered a call to this method.

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Delivers Key Released events to the GUI components. This method is intended to be overridden in derived classes which may handle the event on their own, then optionally call this version.

Parameters:
e - The event that triggered a call to this method.

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Delivers Key Typed events to the GUI components. This method is intended to be overridden in derived classes which may handle the event on their own, then optionally call this version.

Parameters:
e - The event that triggered a call to this method.

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Delivers Mouse Pressed events to the GUI components. This method is intended to be overridden in derived classes which may handle the event on their own, then optionally call this version.

Parameters:
e - The event that triggered a call to this method.

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Delivers Mouse Released events to the GUI components. This method is intended to be overridden in derived classes which may handle the event on their own, then optionally call this version.

Parameters:
e - The event that triggered a call to this method.

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Delivers Mouse Clicked events to the GUI components. This method is intended to be overridden in derived classes which may handle the event on their own, then optionally call this version.

Parameters:
e - The event that triggered a call to this method.

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Delivers Mouse Moved events to the GUI components. This method is intended to be overridden in derived classes which may handle the event on their own, then optionally call this version.

Parameters:
e - The event that triggered a call to this method.

getPreviousScreen

public MHScreen getPreviousScreen()
Returns:
the previous

setPreviousScreen

public void setPreviousScreen(MHScreen previous)
Parameters:
previous - the previous to set