mhframework.gui
Class MHGUIButton

java.lang.Object
  extended by mhframework.gui.MHGUIComponent
      extended by mhframework.gui.MHGUIButton
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.MouseListener, java.util.EventListener, MHRenderable

public class MHGUIButton
extends MHGUIComponent

This class provides graphical image-based buttons within my game framework.

How to add buttons to a game screen:

  1. Create a game screen by following the instructions in the MHScreen documentation.
  2. Declare an MHGUIButton object (or possibly several of them) as a data member in your screen class.
  3. In the screen's constructor, instantiate the MHGUIButton object(s) and set the properties you're interested in, especially its size, position, and caption text. Set the screen object as the action listener for the button like so: myButton.addActionListener(this);
  4. In the actionPerformed() method of your screen class, include a condition for each of your buttons like this: if (e.getSource() == myButton) ... Within the code blocks following each condition, write the code that will execute when each button is clicked.

KNOWN ISSUE: Text buttons (TYPE_TEXT_BUTTON) do not render properly in windowed mode.


Field Summary
protected  java.awt.event.ActionListener actionListener
          ActionEvent listener
static int BUTTON_DOWN
          Indicates button is pressed down
static int BUTTON_NORMAL
          Indicates button is in normal state
static int BUTTON_OVER
          Indicates that mouse is over the button
protected  MHGUILabel caption
          The text caption on the button
static java.awt.Color DOWN_COLOR
           
protected  java.awt.Color downColor
          Color of down state
protected  java.awt.Image icon
          The icon on the button
static java.lang.String IMAGE_TYPE_GIF
          Indicates that button image files are in GIF format.
static java.lang.String IMAGE_TYPE_JPG
          Indicates that button image files are in JPEG (JPG) format.
static java.lang.String IMAGE_TYPE_PNG
          Indicates that button image files are in PNG format.
protected  java.awt.Image[] images
          Array of button images for BUTTON_NORMAL, BUTTON_DOWN, and BUTTON_OVER states.
protected  java.awt.event.KeyListener keyListener
           
protected  java.awt.event.MouseListener mouseListener
           
protected  java.awt.event.MouseMotionListener mouseMotionListener
           
static java.awt.Color NORMAL_COLOR
           
protected  java.awt.Color normalColor
          Color of normal state
static java.awt.Color OVER_COLOR
           
protected  java.awt.Color overColor
          Color of mouseover state
protected  int type
          Indicates type of button (See TYPE_ constants)
static int TYPE_ICON_BUTTON
          Indicates button with icon and text.
static int TYPE_IMAGE_BUTTON
          Indicates that the entire button is a single image.
static int TYPE_TEXT_BUTTON
          Indicates button with text only.
 
Fields inherited from class mhframework.gui.MHGUIComponent
focusable, height, state, width
 
Constructor Summary
MHGUIButton()
          Default constructor which creates a text-only button.
MHGUIButton(java.awt.Image normal, java.awt.Image down, java.awt.Image over)
          Default constructor which creates a text-only button.
MHGUIButton(java.lang.String filenamebase)
          Overloaded constructor which creates an image button from GIF files.
MHGUIButton(java.lang.String filenamebase, java.lang.String imagetype)
          Overloaded constructor which creates an image button from files of a specified type.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
           
 void addActionListener(java.awt.event.ActionListener listener)
           
 void addKeyListener(java.awt.event.KeyListener listener)
           
 void addMouseListener(java.awt.event.MouseListener listener)
           
 void addMouseMotionListener(java.awt.event.MouseMotionListener listener)
           
 void advance()
          Tells an object to update its data.
 java.lang.String getCaptionText()
           
 java.awt.Color getDownColor()
          Returns the downColor.
 java.awt.Font getFont()
           
 java.awt.Image getIcon()
          Returns the icon.
 java.awt.Color getNormalColor()
          Returns the normalColor.
 java.awt.Color getOverColor()
          Returns the overColor.
 int getType()
          Returns the type.
 void keyPressed(java.awt.event.KeyEvent e)
           
 void keyReleased(java.awt.event.KeyEvent e)
           
 void keyTyped(java.awt.event.KeyEvent e)
           
 void mouseClicked(java.awt.event.MouseEvent e)
           
 void mouseMoved(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
          Sets the button to its "pressed" state if it is enabled and the click event took place over the button.
 void mouseReleased(java.awt.event.MouseEvent e)
          Simulates the "clicking" of a button by informing the action listener that a click event took place.
 void render(java.awt.Graphics2D g)
          Tells an object to draw itself onto the sent Graphics object.
 void setDownColor(java.awt.Color downColor)
          Sets the downColor.
 void setFont(java.awt.Font f)
           
 void setForeColor(java.awt.Color c)
           
 void setIcon(java.awt.Image icon)
          Sets the icon.
 void setNormalColor(java.awt.Color normalColor)
          Sets the normalColor.
 void setOverColor(java.awt.Color overColor)
          Sets the overColor.
 void setText(java.lang.String text)
           
 void setType(int type)
          Sets the type.
 void showBounds(boolean show)
           
protected  java.awt.geom.Rectangle2D updateBounds()
           
 
Methods inherited from class mhframework.gui.MHGUIComponent
getBounds, getHeight, getWidth, getX, getY, hasFocus, isEnabled, isVisible, mouseDragged, mouseEntered, mouseExited, paint, setEnabled, setFocus, setFocusable, setHeight, setPosition, setSize, setVisible, setWidth, setX, setY
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_TEXT_BUTTON

public static final int TYPE_TEXT_BUTTON
Indicates button with text only.

See Also:
Constant Field Values

TYPE_ICON_BUTTON

public static final int TYPE_ICON_BUTTON
Indicates button with icon and text.

See Also:
Constant Field Values

TYPE_IMAGE_BUTTON

public static final int TYPE_IMAGE_BUTTON
Indicates that the entire button is a single image.

See Also:
Constant Field Values

IMAGE_TYPE_GIF

public static final java.lang.String IMAGE_TYPE_GIF
Indicates that button image files are in GIF format.

See Also:
Constant Field Values

IMAGE_TYPE_PNG

public static final java.lang.String IMAGE_TYPE_PNG
Indicates that button image files are in PNG format.

See Also:
Constant Field Values

IMAGE_TYPE_JPG

public static final java.lang.String IMAGE_TYPE_JPG
Indicates that button image files are in JPEG (JPG) format.

See Also:
Constant Field Values

BUTTON_NORMAL

public static final int BUTTON_NORMAL
Indicates button is in normal state

See Also:
Constant Field Values

BUTTON_DOWN

public static final int BUTTON_DOWN
Indicates button is pressed down

See Also:
Constant Field Values

BUTTON_OVER

public static final int BUTTON_OVER
Indicates that mouse is over the button

See Also:
Constant Field Values

NORMAL_COLOR

public static final java.awt.Color NORMAL_COLOR

DOWN_COLOR

public static final java.awt.Color DOWN_COLOR

OVER_COLOR

public static final java.awt.Color OVER_COLOR

caption

protected MHGUILabel caption
The text caption on the button


normalColor

protected java.awt.Color normalColor
Color of normal state


downColor

protected java.awt.Color downColor
Color of down state


overColor

protected java.awt.Color overColor
Color of mouseover state


icon

protected java.awt.Image icon
The icon on the button


images

protected java.awt.Image[] images
Array of button images for BUTTON_NORMAL, BUTTON_DOWN, and BUTTON_OVER states.


type

protected int type
Indicates type of button (See TYPE_ constants)


actionListener

protected java.awt.event.ActionListener actionListener
ActionEvent listener


keyListener

protected java.awt.event.KeyListener keyListener

mouseListener

protected java.awt.event.MouseListener mouseListener

mouseMotionListener

protected java.awt.event.MouseMotionListener mouseMotionListener
Constructor Detail

MHGUIButton

public MHGUIButton()
Default constructor which creates a text-only button.


MHGUIButton

public MHGUIButton(java.awt.Image normal,
                   java.awt.Image down,
                   java.awt.Image over)
Default constructor which creates a text-only button.


MHGUIButton

public MHGUIButton(java.lang.String filenamebase)
Overloaded constructor which creates an image button from GIF files.

The file names of the images are generated by appending the digits 0, 1, and 2 along with a .gif extension to the input parameter, and then loading an image file by that name. The 0 is for the image displayed in BUTTON_NORMAL state, the 1 is for the BUTTON_DOWN state, and 2 is for the BUTTON_OVER state.

Parameters:
filenamebase - The base of the filename for loading images.

MHGUIButton

public MHGUIButton(java.lang.String filenamebase,
                   java.lang.String imagetype)
Overloaded constructor which creates an image button from files of a specified type. See the constants defined in this class for a list of valid file types. FIXME This constructor DOES NOT WORK! It claims that the image files do not exist even when they do.

Parameters:
filenamebase - The base of the filename for loading images.
imagetype - A string specifying the image file format of the button image buttons.
Method Detail

getCaptionText

public java.lang.String getCaptionText()

getFont

public java.awt.Font getFont()

render

public void render(java.awt.Graphics2D g)
Description copied from interface: MHRenderable
Tells an object to draw itself onto the sent Graphics object.

Parameters:
g - The Graphics object on which this object is to draw itself.

setFont

public void setFont(java.awt.Font f)

setText

public void setText(java.lang.String text)

setForeColor

public void setForeColor(java.awt.Color c)

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Sets the button to its "pressed" state if it is enabled and the click event took place over the button.

Specified by:
mousePressed in interface java.awt.event.MouseListener
Specified by:
mousePressed in class MHGUIComponent

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Simulates the "clicking" of a button by informing the action listener that a click event took place. If the mouse was released not while over the button, the button is returned to its normal state and no action is performed.

Specified by:
mouseReleased in interface java.awt.event.MouseListener
Specified by:
mouseReleased in class MHGUIComponent

addActionListener

public void addActionListener(java.awt.event.ActionListener listener)

addKeyListener

public void addKeyListener(java.awt.event.KeyListener listener)

addMouseListener

public void addMouseListener(java.awt.event.MouseListener listener)

addMouseMotionListener

public void addMouseMotionListener(java.awt.event.MouseMotionListener listener)

advance

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


actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Specified by:
actionPerformed in interface java.awt.event.ActionListener
Overrides:
actionPerformed in class MHGUIComponent

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Specified by:
mouseClicked in interface java.awt.event.MouseListener
Specified by:
mouseClicked in class MHGUIComponent

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in class MHGUIComponent

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Specified by:
keyPressed in class MHGUIComponent

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Specified by:
keyReleased in class MHGUIComponent

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Specified by:
keyTyped in class MHGUIComponent

getDownColor

public java.awt.Color getDownColor()
Returns the downColor.

Returns:
Color

getNormalColor

public java.awt.Color getNormalColor()
Returns the normalColor.

Returns:
Color

getOverColor

public java.awt.Color getOverColor()
Returns the overColor.

Returns:
Color

setDownColor

public void setDownColor(java.awt.Color downColor)
Sets the downColor.

Parameters:
downColor - The downColor to set

setNormalColor

public void setNormalColor(java.awt.Color normalColor)
Sets the normalColor.

Parameters:
normalColor - The normalColor to set

setOverColor

public void setOverColor(java.awt.Color overColor)
Sets the overColor.

Parameters:
overColor - The overColor to set

getIcon

public java.awt.Image getIcon()
Returns the icon.

Returns:
Image

setIcon

public void setIcon(java.awt.Image icon)
Sets the icon.

Parameters:
icon - The icon to set

getType

public int getType()
Returns the type.

Returns:
int

setType

public void setType(int type)
Sets the type.

Parameters:
type - The type to set

updateBounds

protected java.awt.geom.Rectangle2D updateBounds()
Overrides:
updateBounds in class MHGUIComponent

showBounds

public void showBounds(boolean show)