au.com.solidsoftware.multimahjong.mmc.usergui
Class PlayArea

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JLayeredPane
                          |
                          +--au.com.solidsoftware.multimahjong.mmc.usergui.PlayArea

public class PlayArea
extends javax.swing.JLayeredPane
implements java.awt.event.MouseListener

A panel that contains the graphical representation of the current state of the Wall, the last discard, as well as the declared sets, concealed tiles and drawn tile for each of the four players. In addition, the wind, icon and name of each player will be displayed. A graphic will also be displayed next to the player's icon if the player is fishing for Mahjong. A wind-of-the-round indicator will be displayed next to the player who is East at the very start of the game.

The wall in the centre of the PlayArea will serve to inform the user of how far the game has progressed by showing a graphical representation of the current state of the Wall. When dealing out the tiles at the start each hand, the players may draw from the front of the wall either four, two or one tile(s). However, during actual game play, the user may draw only one tile from either the front or back of the wall.

The last discard (the most recently discarded tile) is also displayed on the inner side of the Wall. This must be updated whenever a player discards a tile and cleared whenever a player picks up the discard and incorporates it into a declared set.

The user is seated at the bottom edge of the PlayArea. This portion of the screen will contain the user's declared sets, concealed tiles and drawn tile. When it is the user's turn to discard, they may click on a concealed tile or the drawn tile, selecting the tile. The user may not select tiles that have already been declared. Once a tile has been selected, the user can then perform additional game actions such as "Discard" and "Reveal Kong" in the ActionPane. The user's wind, icon and name is displayed on the upper-right of the user's concealed hand.

Each of the other three players will be seated along the left, top and right edges of the screen. Each player's declared sets will be visible. However, only the backs of their concealed tiles and drawn tile may be seen. Selecting tiles in another player's hand is not permitted.

Author:
Victor H. Leung
See Also:
Serialized Form

Inner Class Summary
private  class PlayArea.WallTile
          An inner class used primarily for storing the x- and y-coordinate of a wall tile, relative to the top-left corner of the PlayArea.
 
Inner classes inherited from class javax.swing.JLayeredPane
javax.swing.JLayeredPane.AccessibleJLayeredPane
 
Inner classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent, javax.swing.JComponent.IntVector, javax.swing.JComponent.KeyboardBinding, javax.swing.JComponent.KeyboardState
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AWTTreeLock
 
Field Summary
private  java.awt.image.BufferedImage[] concealedImages
          Images of the entire set of concealed tiles for each of the four players.
private  java.awt.image.BufferedImage[] declaredImages
          Images of the entire set of declared tiles for each of the four players.
private  java.awt.Image discardImage
          Image of the last discard.
private  java.awt.Image[] fishImages
          Images of the fishing indicator of each of the four players.
private static double H_GAP
          A horizontal gap (between icons and their labels, between concealed tiles and drawn tile, etc.).
private  int hostID
          The array index that refers to the host's entry in players array.
private static double ICON_H
          The height of an icon image in pixels.
private static double ICON_W
          The width of an icon image in pixels.
private  java.awt.Image[] iconImages
          Images of the icons of each of the four players.
private  int loosePos
          The array index in wallTiles that refers to the next loose tile in the wall to be picked up.
private  java.lang.String[] names
          The names of each of the four players.
private  java.awt.Image[] roundWindImages
          Images of the round wind indicator of each of the four players.
private  boolean selectionEnabled
          Whether the user is permitted to select tiles from their concealed hand by clicking on the tiles within it.
private  Tile[] userConcealed
          The user's concealed hand.
private  java.awt.image.BufferedImage userConcealedImage
          Image of the user's concealed hand.
private  UserGUI userGUI
          A reference to the parent UserGUI object.
private  int userID
          The array index that refers to the user's entry in players array.
private static double V_GAP
          A vertical gap (between declared sets and concealed tiles, between concealed tiles and icons, etc.).
private static int WALL_SIZE
          The number of tiles in the complete wall.
private  int wallPos
          The array index in wallTiles that refers to the next tile in the wall to be picked up.
private static int WALLTILE_H
          The height of a wall tile image in pixels.
private static int WALLTILE_W
          The width of a wall tile image in pixels.
private  PlayArea.WallTile[] wallTiles
          An array containing the tiles in the wall.
private  java.awt.Image[] windImages
          Images of the wind discs of each of the four players.
 
Fields inherited from class javax.swing.JLayeredPane
componentToLayer, DEFAULT_LAYER, DRAG_LAYER, FRAME_CONTENT_LAYER, LAYER_PROPERTY, MODAL_LAYER, optimizedDrawingPossible, PALETTE_LAYER, POPUP_LAYER
 
Fields inherited from class javax.swing.JComponent
_bounds, accessibleContext, alignmentX, alignmentY, ANCESTOR_USING_BUFFER, ancestorNotifier, autoscroller, border, changeSupport, clientProperties, flags, HAS_FOCUS, IS_DOUBLE_BUFFERED, IS_OPAQUE, IS_PAINTING_TILE, KEYBOARD_BINDINGS_KEY, listenerList, maximumSize, minimumSize, NEXT_FOCUS, paintImmediatelyClip, preferredSize, readObjectCallbacks, REQUEST_FOCUS_DISABLED, tmpRect, TOOL_TIP_TEXT_KEY, ui, uiClassID, UNDEFINED_CONDITION, vetoableChangeSupport, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
component, containerListener, containerSerializedDataVersion, dispatcher, layoutMgr, maxSize, ncomponents, serialVersionUID
 
Fields inherited from class java.awt.Component
actionListenerK, adjustmentListenerK, appContext, assert, background, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, changeSupport, componentListener, componentListenerK, componentOrientation, componentSerializedDataVersion, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, font, foreground, hasFocus, height, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, name, nameExplicitlySet, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, RIGHT_ALIGNMENT, serialVersionUID, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowListenerK, x, y
 
Constructor Summary
PlayArea(UserGUI userGUI, Player[] players, int userID)
          Creates an instance of PlayArea showing the wind, icon and name of each of the players in the game.
 
Method Summary
private  void autoSelect()
          Signals to the player that the game is now ready to accept a discard by pre-selecting the right-most tile for the user to discard.
private  void highlightSelected(int tileID)
          Highlights the specified tile in the user's concealed hand to inform the user that a tile has been selected.
 void initWall()
          Initialises the graphical representation of the Wall by building a new, complete wall, and randomly generates a break position.
static void main(java.lang.String[] args)
          Driver code used only for unit testing.
 void mouseClicked(java.awt.event.MouseEvent e)
          This function is invoked whenever the user clicks a mouse button while the cursor is in the PlayArea.
 void mouseEntered(java.awt.event.MouseEvent e)
          Empty function body required to implement MouseListener.
 void mouseExited(java.awt.event.MouseEvent e)
          Empty function body required to implement MouseListener.
 void mousePressed(java.awt.event.MouseEvent e)
          Empty function body required to implement MouseListener.
 void mouseReleased(java.awt.event.MouseEvent e)
          Empty function body required to implement MouseListener.
 void paint(java.awt.Graphics g)
          Paints the contents of the entire PlayArea to the screen.
 void pickUpLoose()
          Updates the graphical representation of the wall, showing that a single tile has been picked up from the back of the wall.
 void pickUpTile()
          Updates the graphical representation of the wall, showing that a single tile has been picked up from the front of the wall.
 void setFishing(boolean isFishing, int playerID)
          Shows/hides the fishing graphic for the specified player.
 void tileSelection(boolean selectionEnabled)
          Sets whether the user is permitted to select tiles from their concealed hand by clicking on the tiles within it.
 void updateConcealed(Tile[] tiles, boolean drawn, int playerID)
          Updates the concealed tiles for the player seated in position.
 void updateDeclared(Set[] sets, int playerID)
          Updates the declared sets for the player seated in position.
 void updateDiscard(Tile tile)
          Updates the most recently discarded tile displayed on the inner side of the Wall.
 void updateRoundWind(int wind)
          Update the round wind indicated associated with the host of the game (that is, the playerID of the player whose wind was initially East).
 void updateWind(int wind, int playerID)
          Update the wind disc graphic for the specified player.
private  int wallDecrement(int position)
          Decrements a wall position in a "wrap-around" manner.
private  int wallIncrement(int position)
          Increments a wall position in a "wrap-around" manner.
 
Methods inherited from class javax.swing.JLayeredPane
, addImpl, getAccessibleContext, getComponentCountInLayer, getComponentsInLayer, getComponentToLayer, getIndexOf, getLayer, getLayer, getLayeredPaneAbove, getObjectForLayer, getPosition, highestLayer, insertIndexForLayer, isOptimizedDrawingEnabled, lowestLayer, moveToBack, moveToFront, paramString, putLayer, remove, setLayer, setLayer, setPosition, validateOptimizedDrawing
 
Methods inherited from class javax.swing.JComponent
_paintImmediately, addAncestorListener, addNotify, addPropertyChangeListener, addVetoableChangeListener, adjustPaintFlags, alwaysOnTop, bindingForKeyStroke, computeVisibleRect, computeVisibleRect, contains, createToolTip, enableSerialization, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperties, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getFlag, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getUIClassID, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isOpaque, isPaintingTile, isRequestFocusEnabled, isValidateRoot, keyboardBindings, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paintWithBuffer, processComponentKeyEvent, processFocusEvent, processKeyBinding, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseMotionEvent, putClientProperty, readObject, rectangleIsObscured, registerKeyboardAction, registerKeyboardAction, registerWithKeyboardManager, removeAncestorListener, removeNotify, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFlag, setFont, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVisible, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, unregisterWithKeyboardManager, update, updateUI, writeObject
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyOrientation, countComponents, deliverEvent, dispatchEventImpl, dispatchEventToSelf, doLayout, eventEnabled, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents_NoClientCode, getComponents, getLayout, getMouseEventTarget, getWindow, initIDs, insets, invalidate, invalidateTree, isAncestorOf, layout, lightweightPrint, list, list, locate, minimumSize, nextFocus, paintComponents, postProcessKeyEvent, postsOldMouseEvents, preferredSize, preProcessKeyEvent, print, printComponents, printOneComponent, processContainerEvent, processEvent, proxyEnableEvents, proxyRequestFocus, remove, removeAll, removeContainerListener, setFocusOwner, setLayout, transferFocus, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, areInputMethodsEnabled, bounds, checkImage, checkImage, coalesceEvents, constructComponentName, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getSize, getToolkit, getToolkitImpl, getTreeLock, getWindowForObject, gotFocus, handleEvent, hide, imageUpdate, inside, isDisplayable, isEnabled, isEnabledImpl, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

ICON_W

private static final double ICON_W
The width of an icon image in pixels.

ICON_H

private static final double ICON_H
The height of an icon image in pixels.

WALLTILE_W

private static final int WALLTILE_W
The width of a wall tile image in pixels.

WALLTILE_H

private static final int WALLTILE_H
The height of a wall tile image in pixels.

H_GAP

private static final double H_GAP
A horizontal gap (between icons and their labels, between concealed tiles and drawn tile, etc.).

V_GAP

private static final double V_GAP
A vertical gap (between declared sets and concealed tiles, between concealed tiles and icons, etc.).

userGUI

private UserGUI userGUI
A reference to the parent UserGUI object.

hostID

private int hostID
The array index that refers to the host's entry in players array. Since there are a maximum of four players in a game of Mahjong, this value is in the range [0,3]. The initial value of -1 serves as a sentinel to indicate that the hostID has not been set previously.

userID

private int userID
The array index that refers to the user's entry in players array. Since there are a maximum of four players in a game of Mahjong, this value is in the range [0,3].

userConcealed

private Tile[] userConcealed
The user's concealed hand. This is updated by the updateConcealed method and used by the mouseClicked method to set the selected tile in the ActionPane.

userConcealedImage

private java.awt.image.BufferedImage userConcealedImage
Image of the user's concealed hand. This is used by the setSelected method to restore the original appearance of the user's concealed hand (before highlighting the selected tile).

selectionEnabled

private boolean selectionEnabled
Whether the user is permitted to select tiles from their concealed hand by clicking on the tiles within it.

names

private java.lang.String[] names
The names of each of the four players.

iconImages

private java.awt.Image[] iconImages
Images of the icons of each of the four players.

windImages

private java.awt.Image[] windImages
Images of the wind discs of each of the four players.

fishImages

private java.awt.Image[] fishImages
Images of the fishing indicator of each of the four players.

roundWindImages

private java.awt.Image[] roundWindImages
Images of the round wind indicator of each of the four players.

declaredImages

private java.awt.image.BufferedImage[] declaredImages
Images of the entire set of declared tiles for each of the four players.

concealedImages

private java.awt.image.BufferedImage[] concealedImages
Images of the entire set of concealed tiles for each of the four players.

WALL_SIZE

private static final int WALL_SIZE
The number of tiles in the complete wall.

wallTiles

private PlayArea.WallTile[] wallTiles
An array containing the tiles in the wall.

wallPos

private int wallPos
The array index in wallTiles that refers to the next tile in the wall to be picked up.

loosePos

private int loosePos
The array index in wallTiles that refers to the next loose tile in the wall to be picked up.

discardImage

private java.awt.Image discardImage
Image of the last discard.
Constructor Detail

PlayArea

public PlayArea(UserGUI userGUI,
                Player[] players,
                int userID)
Creates an instance of PlayArea showing the wind, icon and name of each of the players in the game. The wind-of-the-round indicator will also be displayed next to the player who is East at the start of the game. All other elements, that is, the Wall, the last discard, and the declared sets, concealed tiles and drawn tile for each of the players are not visible.

Parameters:
userGUI - A reference to the parent UserGUI object.
players - An array which stores information specific for each of the players. In this method, we are interested only in extracting the players' icons and names.
userID - The array index that refers to the user's entry in players array. Since there are a maximum of four players in a game of Mahjong, this value is in the range [0,3].
Method Detail

updateWind

public void updateWind(int wind,
                       int playerID)
Update the wind disc graphic for the specified player.

Parameters:
wind - The wind of the specified player. This is distinct from the "round wind".
playerID - The array index that refers to the player's entry in players array. Since there are a maximum of four players in a game of Mahjong, this value is in the range [0,3].

updateRoundWind

public void updateRoundWind(int wind)
Update the round wind indicated associated with the host of the game (that is, the playerID of the player whose wind was initially East).

Parameters:
wind - The round wind. This is distinct from the wind associated with each player.

setFishing

public void setFishing(boolean isFishing,
                       int playerID)
Shows/hides the fishing graphic for the specified player.

Parameters:
isFishing - A flag indicating whether a player is fishing for Mahjong. The fishing graphic is displayed if this flag is set to true.
playerID - The array index that refers to the player's entry in players array. Since there are a maximum of four players in a game of Mahjong, this value is in the range [0,3].

updateDeclared

public void updateDeclared(Set[] sets,
                           int playerID)
Updates the declared sets for the player seated in position.

Parameters:
sets - The sets that have been declared by the player. If this is null, the player's declared sets are cleared from the display.
playerID - The array index that refers to the player's entry in players array. Since there are a maximum of four players in a game of Mahjong, this value is in the range [0,3].

updateConcealed

public void updateConcealed(Tile[] tiles,
                            boolean drawn,
                            int playerID)
Updates the concealed tiles for the player seated in position.

Parameters:
tiles - The tiles in the players concealed hand. If this is null, all of the player's concealed tiles are cleared from the display.
drawn - Whether the last concealed tile is a drawn tile.
playerID - The array index that refers to the player's entry in players array. Since there are a maximum of four players in a game of Mahjong, this value is in the range [0,3].

updateDiscard

public void updateDiscard(Tile tile)
Updates the most recently discarded tile displayed on the inner side of the Wall.

Parameters:
tile - The most recently discarded tile. If this is null, last discard is cleared from the display.

initWall

public void initWall()
Initialises the graphical representation of the Wall by building a new, complete wall, and randomly generates a break position. The screen coordinates of each of the 136 tiles in the wall are determined and stored in the inner class WallTile.


pickUpTile

public void pickUpTile()
Updates the graphical representation of the wall, showing that a single tile has been picked up from the front of the wall.

pickUpLoose

public void pickUpLoose()
Updates the graphical representation of the wall, showing that a single tile has been picked up from the back of the wall.

wallIncrement

private int wallIncrement(int position)
Increments a wall position in a "wrap-around" manner.

Parameters:
position - The array index that refers to a wall tile in the wallTiles array. Since there are a maximum of 136 tiles in a game of Mahjong, this value is in the range [0,135].
Returns:
The position referring to the next tile along the wall. This is calculated in a "wrap-around" manner (that is, 134 -> 135 -> 0 -> 1).

wallDecrement

private int wallDecrement(int position)
Decrements a wall position in a "wrap-around" manner.

Parameters:
position - The array index that refers to a wall tile in the wallTiles array. Since there are a maximum of 136 tiles in a game of Mahjong, this value is in the range [0,135].
Returns:
The position referring to the previous tile along the wall. This is calculated in a "wrap-around" manner (that is, 4 -> 5 -> 2 -> 3 -> 0 -> 1 -> 134 -> 135).

paint

public void paint(java.awt.Graphics g)
Paints the contents of the entire PlayArea to the screen.

Overrides:
paint in class javax.swing.JLayeredPane

autoSelect

private void autoSelect()
Signals to the player that the game is now ready to accept a discard by pre-selecting the right-most tile for the user to discard.


highlightSelected

private void highlightSelected(int tileID)
Highlights the specified tile in the user's concealed hand to inform the user that a tile has been selected.

Parameters:
tileID - The position of the tile (the leftmost tile being 0) to highlight in the user's concealed hand. If this is a negative number, the original concealed hand image is restored, removing any existing highlights.

tileSelection

public void tileSelection(boolean selectionEnabled)
Sets whether the user is permitted to select tiles from their concealed hand by clicking on the tiles within it.

Parameters:
selectionEnabled - if true, then the user is permitted to select tiles from their concealed hand.

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
This function is invoked whenever the user clicks a mouse button while the cursor is in the PlayArea. If they click on a tile in their concealed hand, the tile will become highlighted. The selected tile is also set in the ActionPane. Otherwise, any existing highlighting is removed and the selected tile in the ActionPane is set to null.

Specified by:
mouseClicked in interface java.awt.event.MouseListener
Parameters:
e - A mouse action that occurred in the component.

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Empty function body required to implement MouseListener.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Empty function body required to implement MouseListener.

Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Empty function body required to implement MouseListener.

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

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Empty function body required to implement MouseListener.

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

main

public static void main(java.lang.String[] args)
Driver code used only for unit testing.