au.com.solidsoftware.multimahjong.shared
Class Tile

java.lang.Object
  |
  +--java.awt.Component
        |
        +--au.com.solidsoftware.multimahjong.shared.Tile
Direct Known Subclasses:
AITile, Set

public class Tile
extends java.awt.Component
implements java.lang.Comparable

Representation of a physical Mahjong tile. The Chinese game of Mahjong is played with a set of 136 tiles. The tiles are arranged into two categories: Suits and Honours. There are 3 Suits: Character, Circle and Bamboo. Each of these has 4 duplicates of the numbers 1 to 9, so each Suit consists of 36 tiles. Honour tiles consist of Winds and Dragons. The Wind tiles consist of East, South, West and North Winds, while the Dragon tiles consist of Green, Red and White Dragons. There are 4 duplicates of each tile.

This class extends the Component class as it has a graphical representation that can be displayed on the screen and that can interact with the user.

This class implements the Comparable interface by defining the compareTo method. This allows an array of tiles to be sorted according to the suit, and then the number, of the tiles.

Author:
Victor H. Leung
See Also:
Serialized Form

Inner classes inherited from class java.awt.Component
java.awt.Component.AWTTreeLock
 
Field Summary
static int BAMBOO
          Symbolic constant used to refer to the Bamboo suit.
static int CHARACTER
          Symbolic constant used to refer to the Character suit.
static int CIRCLE
          Symbolic constant used to refer to the Circle suit.
static int DRAGON
          Symbolic constant used to refer to the Dragon suit.
static int EAST
          Symbolic constant used to refer to the East Wind tile.
static int GREEN
          Symbolic constant used to refer to the Green Dragon tile.
static int NORTH
          Symbolic constant used to refer to the North Wind tile.
protected  int number
          The number of this tile.
static int RED
          Symbolic constant used to refer to the Red Dragon tile.
static int SOUTH
          Symbolic constant used to refer to the South Wind tile.
protected  int suit
          The suit of this tile.
static int TILE_H
          The height of a tile image in pixels.
static int TILE_W
          The width of a tile image in pixels.
private static java.awt.Image[] tileImages
          An array containing the images of all the tiles.
private static java.awt.MediaTracker tracker
          Keeps track of the loading status of the tile images.
static int WEST
          Symbolic constant used to refer to the West Wind tile.
static int WHITE
          Symbolic constant used to refer to the White Dragon tile.
static int WIND
          Symbolic constant used to refer to the Wind suit.
 
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
Tile(int number, int suit)
          Creates an instance of Tile with a specific number and suit.
 
Method Summary
 java.awt.Image backImage()
          Obtains the Image corresponding to the back of a tile.
 int compareTo(java.lang.Object tile)
          Compares this object with the specified object for order.
static void ensureImagesLoaded()
          Waits until all tile images are loaded completely into memory.
 java.awt.Image faceImage()
          Obtains the Image corresponding to the face of this tile.
 int getNumber()
          Access method - returns the number of this Tile object.
 java.lang.String getString()
           
 int getSuit()
          Access method - returns the suit of this Tile object.
static void loadImages(UserGUI userGUI)
          Begin loading all the tile images into memory.
static void main(java.lang.String[] args)
           
 
Methods inherited from class java.awt.Component
, action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, areInputMethodsEnabled, bounds, checkImage, checkImage, coalesceEvents, constructComponentName, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, doLayout, enable, enable, enableEvents, enableInputMethods, eventEnabled, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getToolkitImpl, getTreeLock, getWidth, getWindowForObject, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, initIDs, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isEnabledImpl, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, lightweightPrint, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, postsOldMouseEvents, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, readObject, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, update, validate, writeObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

CHARACTER

public static final int CHARACTER
Symbolic constant used to refer to the Character suit. The Character tiles ranges from 1 to 9.


CIRCLE

public static final int CIRCLE
Symbolic constant used to refer to the Circle suit. The Circle tiles ranges from 1 to 9.


BAMBOO

public static final int BAMBOO
Symbolic constant used to refer to the Bamboo suit. The Bamboo tiles ranges from 1 to 9.


WIND

public static final int WIND
Symbolic constant used to refer to the Wind suit. The Wind tiles is a subset of Honour tiles, and there are four distinct types: East, South, West and North.


DRAGON

public static final int DRAGON
Symbolic constant used to refer to the Dragon suit. The Dragon tiles is a subset of Honour tiles, and there are three distinct types: Green, Red and White.


EAST

public static final int EAST
Symbolic constant used to refer to the East Wind tile.


SOUTH

public static final int SOUTH
Symbolic constant used to refer to the South Wind tile.


WEST

public static final int WEST
Symbolic constant used to refer to the West Wind tile.


NORTH

public static final int NORTH
Symbolic constant used to refer to the North Wind tile.


GREEN

public static final int GREEN
Symbolic constant used to refer to the Green Dragon tile.


RED

public static final int RED
Symbolic constant used to refer to the Red Dragon tile.


WHITE

public static final int WHITE
Symbolic constant used to refer to the White Dragon tile.


TILE_W

public static final int TILE_W
The width of a tile image in pixels.

TILE_H

public static final int TILE_H
The height of a tile image in pixels.

tileImages

private static java.awt.Image[] tileImages
An array containing the images of all the tiles. This is declared static so that the same images are used by every single Tile object, decreasing memory usage and increasing performance.

tracker

private static java.awt.MediaTracker tracker
Keeps track of the loading status of the tile images.

number

protected final int number
The number of this tile. 1 to 9 inclusive for Character, Circle and Bamboo Suits. Symbolic constants are used to refer to the Honour tiles (Winds and Dragons).


suit

protected final int suit
The suit of this tile. Symbolic constants are used to refer to the Character, Circle, Bamboo, Wind and Dragon Suits.

Constructor Detail

Tile

public Tile(int number,
            int suit)
Creates an instance of Tile with a specific number and suit. Both of these fields are declared final and cannot be modified once the object is instantiated.

Parameters:
number - The number of the tile.
suit - The suit of the tile.
Method Detail

loadImages

public static void loadImages(UserGUI userGUI)
Begin loading all the tile images into memory. Warning: since the createImage method is asynchronous, tile images may not be completely loaded when this function returns. This is desirable from a performance point of view.

ensureImagesLoaded

public static void ensureImagesLoaded()
Waits until all tile images are loaded completely into memory.

getNumber

public int getNumber()
Access method - returns the number of this Tile object.

Returns:
The number of this tile.

getSuit

public int getSuit()
Access method - returns the suit of this Tile object.

Returns:
The suit of this tile.

getString

public java.lang.String getString()

faceImage

public java.awt.Image faceImage()
Obtains the Image corresponding to the face of this tile.
Returns:
The Image corresponding to the face of this tile.

backImage

public java.awt.Image backImage()
Obtains the Image corresponding to the back of a tile.
Returns:
The Image corresponding to the back of a tile.

compareTo

public int compareTo(java.lang.Object tile)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. This method defines the natural ordering of Tile objects to enable the automatic sorting of lists and arrays.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
tile - The Object to be compared.
Returns:
A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
ClassCastException - If the specified object's type prevents it from being compared to this Object.

main

public static void main(java.lang.String[] args)