thegame.client
Class GameClient

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Panel
                    |
                    +--java.applet.Applet
                          |
                          +--javax.swing.JApplet
                                |
                                +--thegame.client.GameClient
All Implemented Interfaces:
javax.accessibility.Accessible, GameClientConstants, java.awt.image.ImageObserver, java.awt.MenuContainer, javax.swing.RootPaneContainer, java.io.Serializable

public class GameClient
extends javax.swing.JApplet
implements GameClientConstants

The main class for the game client. Masters the connection to the server, the game and start screens. Provides some methods.

Version:
0.99 (2000/08/28)
Author:
Erki Suurjaak
See Also:
Serialized Form

Inner Class Summary
protected  class GameClient.GameScreen
          The game screen.
protected  class GameClient.StartScreen
          The start screen where the user can select a name and create or join a game.
 
Inner classes inherited from class javax.swing.JApplet
javax.swing.JApplet.AccessibleJApplet
 
Inner classes inherited from class java.applet.Applet
java.applet.Applet.AccessibleApplet
 
Inner classes inherited from class java.awt.Panel
java.awt.Panel.AccessibleAWTPanel
 
Inner classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent
 
Inner classes inherited from class thegame.client.GameClientConstants
GameClientConstants.GameScreenConstants, GameClientConstants.StartScreenConstants
 
Field Summary
protected static GameClient applet
          References the GameClient.
protected  GameClientConnection connection
          The connection to the server.
protected  GameClient.GameScreen gameScreen
          The game screen.
protected  java.lang.String host
          The IP address of the server.
protected  boolean isAnApplet
          Indicates whether the GameClient is running as an applet or as an application.
protected  java.lang.String localPlayerName
          The name of the local player.
protected  java.lang.String[] mainArgs
          Contains the command-line arguments passed to the GameClient in case it was run as an application.
protected  int port
          The TCP port of the server.
protected  GameClient.StartScreen startScreen
          The start screen.
 
Fields inherited from class javax.swing.JApplet
accessibleContext, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface thegame.client.GameClientConstants
HOST_PARAMETER_NAME, HOST_PARAMETER_NOT_FOUND_TEXT, NORMAL_TERMINATION_TEXT, PORT_PARAMETER_NAME, SERVER_SHUTDOWN_TEXT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
GameClient()
           
 
Method Summary
 void bringUpGameScreen(boolean isLocalPlayerDrawingCircles, boolean isConnectedToAPlayer, java.lang.String remotePlayerName)
          Empties the applet window and brings up a GameScreen.
 void bringUpStartScreen(boolean isPlayerAlreadyNamed)
          Empties the applet window and brings up a StartScreen.
 void destroy()
          Cleans up the data members that should be cleaned (connection to the server).
 void fatalError(java.lang.String message)
          Shuts down the GameClient.
static javax.swing.ImageIcon getImageIcon(java.lang.String path)
          Returns an ImageIcon constructed from an image file.
 void handleMessage(java.lang.String message)
          Based on the type identifier of the message, calls the needed methods from either StartScreen or GameScreen.
 void init()
          Reads the required parameters, connects to the server and brings up a StartScreen.
static void main(java.lang.String[] args)
          Launches GameClient as an application.
 java.lang.String readParameter(java.lang.String parameterName)
          Reads the required parameter from the applet HTML page source or, if GameClient is run as an application, from the commandline arguments.
 void readParameters()
          Reads the HOST and PORT parameters from the applet HTML page source, or, if GameClient is run as an application, from the commandline arguments.
 void terminate()
          Shuts down the GameClient.
 void tryAndSend(int id)
          Calls send(int) from GameClientConnection.
 void tryAndSend(int id, boolean body)
          Calls send(int, boolean) from GameClientConnection.
 void tryAndSend(int id, int[] body)
          Calls send(int, int[][]) from GameClientConnection.
 void tryAndSend(int id, int[][] body)
          Calls send(int, int[][]) from GameClientConnection.
 void tryAndSend(int id, java.lang.String body)
          Calls send(int, String) from GameClientConnection.
 
Methods inherited from class javax.swing.JApplet
addImpl, createRootPane, getAccessibleContext, getContentPane, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isRootPaneCheckingEnabled, paramString, processKeyEvent, remove, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, update
 
Methods inherited from class java.applet.Applet
getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop
 
Methods inherited from class java.awt.Panel
addNotify
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, removeNotify, setFont, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getInputContext, getInputMethodRequests, getLocation, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

applet

protected static GameClient applet
References the GameClient. Is used by getImageIcon(String).

connection

protected GameClientConnection connection
The connection to the server.

gameScreen

protected GameClient.GameScreen gameScreen
The game screen.

host

protected java.lang.String host
The IP address of the server.

isAnApplet

protected boolean isAnApplet
Indicates whether the GameClient is running as an applet or as an application.

localPlayerName

protected java.lang.String localPlayerName
The name of the local player.

mainArgs

protected java.lang.String[] mainArgs
Contains the command-line arguments passed to the GameClient in case it was run as an application.

port

protected int port
The TCP port of the server.

startScreen

protected GameClient.StartScreen startScreen
The start screen.
Constructor Detail

GameClient

public GameClient()
Method Detail

bringUpGameScreen

public void bringUpGameScreen(boolean isLocalPlayerDrawingCircles,
                              boolean isConnectedToAPlayer,
                              java.lang.String remotePlayerName)
Empties the applet window and brings up a GameScreen.
Parameters:
isLocalPlayerDrawingCircles - true if the local player is drawing circles; false if the local player is drawing crosses
isConnectedToAPlayer - true if there is a remote player present; false otherwise
remotePlayerName - the name of the remote player to be joined; or null

bringUpStartScreen

public void bringUpStartScreen(boolean isPlayerAlreadyNamed)
Empties the applet window and brings up a StartScreen.
Parameters:
isPlayerAlreadyNamed - true if the local player already has a name; false otherwise

destroy

public void destroy()
Cleans up the data members that should be cleaned (connection to the server).
Overrides:
destroy in class java.applet.Applet

fatalError

public void fatalError(java.lang.String message)
Shuts down the GameClient. The window is emptied and message is shown or, if GameClient is run as an application, prints message to the standard error output stream and exits. Called in case of a fatal error that does not allow to proceed normally.
Parameters:
message - the errormessage to be shown

getImageIcon

public static javax.swing.ImageIcon getImageIcon(java.lang.String path)
Returns an ImageIcon constructed from an image file.
Parameters:
path - the full path and filename of the image file
Returns:
an ImageIcon constructed from an image file

handleMessage

public void handleMessage(java.lang.String message)
Based on the type identifier of the message, calls the needed methods from either StartScreen or GameScreen.
Parameters:
message - the message to be handled

init

public void init()
Reads the required parameters, connects to the server and brings up a StartScreen.
Overrides:
init in class java.applet.Applet

main

public static void main(java.lang.String[] args)
Launches GameClient as an application. Possible arguments include :
HOST address,
where address is the IP address of the host. Is compulsory.
PORT port,
where port is the port of the host. Optional - if not specified, the default port will be used.
Parameters:
args - the command-line arguments

readParameter

public java.lang.String readParameter(java.lang.String parameterName)
Reads the required parameter from the applet HTML page source or, if GameClient is run as an application, from the commandline arguments.
Parameters:
parameterName - the case-insensitive name of the parameter
Returns:
the value of the specified parameter

readParameters

public void readParameters()
Reads the HOST and PORT parameters from the applet HTML page source, or, if GameClient is run as an application, from the commandline arguments.

terminate

public void terminate()
Shuts down the GameClient. Informs the server of quitting, empties the window and prints a quit message or, if GameClient is run as an application, exits. Called in case of a normal shutdown progress.

tryAndSend

public void tryAndSend(int id)
Calls send(int) from GameClientConnection. If an IOException is thrown, calls fatalError(String).
Parameters:
id - the type identifier of the message

tryAndSend

public void tryAndSend(int id,
                       boolean body)
Calls send(int, boolean) from GameClientConnection. If an IOException is thrown, calls fatalError(String).
Parameters:
id - the type identifier of the message
body - the body of the message

tryAndSend

public void tryAndSend(int id,
                       int[] body)
Calls send(int, int[][]) from GameClientConnection. If an IOException is thrown, calls fatalError(String).
Parameters:
id - the type identifier of the message
body - the body of the message

tryAndSend

public void tryAndSend(int id,
                       int[][] body)
Calls send(int, int[][]) from GameClientConnection. If an IOException is thrown, calls fatalError(String).
Parameters:
id - the type identifier of the message
body - the body of the message

tryAndSend

public void tryAndSend(int id,
                       java.lang.String body)
Calls send(int, String) from GameClientConnection. If an IOException is thrown, calls fatalError(String).
Parameters:
id - the type identifier of the message
body - the body of the message