pictionary.pictionaryclient
Class PictionaryClientGUI

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Window
              extended by java.awt.Frame
                  extended by javax.swing.JFrame
                      extended by pictionary.pictionaryclient.PictionaryClientGUI
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.RootPaneContainer, javax.swing.WindowConstants, IPictionaryClientCallback

public class PictionaryClientGUI
extends javax.swing.JFrame
implements IPictionaryClientCallback

A simple Pictionary GUI.
This class receives and forwards information to a PictionaryClient

Author:
Kristoffer Nordkvist
See Also:
Serialized Form

Nested Class Summary
private  class PictionaryClientGUI.CloseWindowListener
          A custom class used for overriding the default behaviour when the window is closed.
private  class PictionaryClientGUI.ConnectButtonListener
          Custom listener class for handling the event that the user uses disconnectOrConnectBtn
private  class PictionaryClientGUI.JoinGameButtonListener
          Handles the event that is generated when joinGameBtn is clicked.
private  class PictionaryClientGUI.Paper
          A simple class for rendering a set of points.
private  class PictionaryClientGUI.SendButtonListener
          Handles the event that is generated when the user clicks sendTextBtn
 
Nested classes/interfaces inherited from class javax.swing.JFrame
javax.swing.JFrame.AccessibleJFrame
 
Nested classes/interfaces inherited from class java.awt.Frame
java.awt.Frame.AccessibleAWTFrame
 
Nested classes/interfaces inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  PictionaryClient client
          The PictionaryClient that we receive and forward events from/to.
private  boolean connected
          True if the client is connected to the server, else false.
private  javax.swing.JButton disconnectOrConnectBtn
          The button used for connecting and disconnecting to/from the server, is initially used for connecting.
private  javax.swing.JTextField guessField
          The field where the user inputs guesses when playing a game of pictionary.
private  javax.swing.JTextArea inputArea
          The area where chat text is input.
private  javax.swing.JScrollPane inputAreaScrollPane
          The JScrollPane that inputArea is attached to.
private  javax.swing.JButton joinGameBtn
          The button used when joining or disconnecting from a game.
private  PictionaryClientGUI.Paper paper
          The PictionaryClientGUI.Paper on which the user can draw and also see what other players are drawing.
private  javax.swing.JTextArea receivedText
          The text area that contains received messages from the server and other clients.
private  javax.swing.JScrollPane receivedTextScrollPane
          The JScrollPane that receivedText is attached to.
private  javax.swing.JButton sendTextBtn
          A button that can be used for sending the text in inputArea.
 
Fields inherited from class javax.swing.JFrame
accessibleContext, EXIT_ON_CLOSE, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
PictionaryClientGUI(PictionaryClient client)
          The constructor.
 
Method Summary
 void aClientConnected(java.lang.String client)
          This method is called when a new client has connected to the server.
 void aClientDisconnected(java.lang.String client)
          This method is called when a client has disconnected from the server.
private  void broadCastText()
          Sends the text in inputArea to the server using PictionaryClient.broadcastChatMessage(String).
 void clientBroadcastMessageReceived(java.lang.String client, java.lang.String message)
          This method is called when a message from a client, broadcasted to all other clients, is received.
 void clientStartedDrawing(java.lang.String client)
          This method is called when a client has started drawing.
 void closedByError(java.lang.String error)
          This method is called when the connection to the server has been closed.
 void connectedToServer()
          This method is called when the player successfully connected to a server.
 void correctGuess()
          This method is called when the player has made a correct guess.
 void correctGuessBroadcastReceived(java.lang.String player, java.lang.String guess)
          This method is called to notify the client that a player made a correct guess.
private  void disconnect()
          Disconnects from the server.
 void drawReceived(java.awt.Point point)
          This method is called to notify the client that the drawing player drew at a specific point.
private  void guess(java.lang.String guess)
          Used when the client wants to guess on a word while in a game of Pictionary.
 void joinedNewGame(java.util.List<java.lang.String> players, int gameStatus)
          This method is called when the client successfully joined a game of Pictionary.
static void main(java.lang.String[] args)
          Asks the user for hostname and port, and then creates an instance of #PictionaryClient using these values.
private  void onServerDisconnect()
          Called when the connection to the server is lost.
 void serverDisconnected()
          This method is called when the connection to the server is lost.
 void serverMessageReceived(java.lang.String message)
          This method is called when a message from the server sent to all the clients is received.
 void startDrawing(java.lang.String wordToDraw, int drawTime)
          This method is called when the player should start drawing a word.
 void startGuessing()
          This method is called to notify the client that a player is drawing and that the client can start making guesses.
 void stopDrawing()
          This method is called to notify the client that it should stop drawing.
 void stopGuessing()
          This method is called to notify the client that it should stop guessing, for example when someone made a correct guess or when the drawing player's time is up.
 java.lang.String userNameRequest()
          This method is called when the server has requested the client to respond with an username.
 void wrongGuessBroadcastReceived(java.lang.String player, java.lang.String guess)
          This method notifies the client that a player made an incorrect guess.
 
Methods inherited from class javax.swing.JFrame
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
 
Methods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated
 
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOwnedWindows, getOwner, getOwnerlessWindows, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, paint, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setVisible, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, setBackground, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.MenuContainer
getFont, postEvent
 

Field Detail

client

private PictionaryClient client
The PictionaryClient that we receive and forward events from/to.


receivedText

private javax.swing.JTextArea receivedText
The text area that contains received messages from the server and other clients.


inputArea

private javax.swing.JTextArea inputArea
The area where chat text is input.


receivedTextScrollPane

private javax.swing.JScrollPane receivedTextScrollPane
The JScrollPane that receivedText is attached to.


inputAreaScrollPane

private javax.swing.JScrollPane inputAreaScrollPane
The JScrollPane that inputArea is attached to.


disconnectOrConnectBtn

private javax.swing.JButton disconnectOrConnectBtn
The button used for connecting and disconnecting to/from the server, is initially used for connecting.


connected

private boolean connected
True if the client is connected to the server, else false.


joinGameBtn

private javax.swing.JButton joinGameBtn
The button used when joining or disconnecting from a game.


sendTextBtn

private javax.swing.JButton sendTextBtn
A button that can be used for sending the text in inputArea.


guessField

private javax.swing.JTextField guessField
The field where the user inputs guesses when playing a game of pictionary.


paper

private PictionaryClientGUI.Paper paper
The PictionaryClientGUI.Paper on which the user can draw and also see what other players are drawing.

Constructor Detail

PictionaryClientGUI

public PictionaryClientGUI(PictionaryClient client)
The constructor. Sets up all initializes all the GUI elements.

Parameters:
client - The PictionaryClient, see client.
Method Detail

onServerDisconnect

private void onServerDisconnect()
Called when the connection to the server is lost.


disconnect

private void disconnect()
Disconnects from the server.


broadCastText

private void broadCastText()
Sends the text in inputArea to the server using PictionaryClient.broadcastChatMessage(String).


guess

private void guess(java.lang.String guess)
Used when the client wants to guess on a word while in a game of Pictionary.

Parameters:
guess - The word that is the client's guess.

correctGuessBroadcastReceived

public void correctGuessBroadcastReceived(java.lang.String player,
                                          java.lang.String guess)
Description copied from interface: IPictionaryClientCallback
This method is called to notify the client that a player made a correct guess.

Specified by:
correctGuessBroadcastReceived in interface IPictionaryClientCallback
Parameters:
player - The name of the player that made the correct guess.
guess - The guess that was made.

correctGuess

public void correctGuess()
Description copied from interface: IPictionaryClientCallback
This method is called when the player has made a correct guess.

Specified by:
correctGuess in interface IPictionaryClientCallback

wrongGuessBroadcastReceived

public void wrongGuessBroadcastReceived(java.lang.String player,
                                        java.lang.String guess)
Description copied from interface: IPictionaryClientCallback
This method notifies the client that a player made an incorrect guess.

Specified by:
wrongGuessBroadcastReceived in interface IPictionaryClientCallback
Parameters:
player - The name of the player that made the incorrect guess.
guess - The incorrect guess.

aClientDisconnected

public void aClientDisconnected(java.lang.String client)
Description copied from interface: IPictionaryClientCallback
This method is called when a client has disconnected from the server.

Specified by:
aClientDisconnected in interface IPictionaryClientCallback
Parameters:
client - The name of the client that disconnected.

aClientConnected

public void aClientConnected(java.lang.String client)
Description copied from interface: IPictionaryClientCallback
This method is called when a new client has connected to the server.

Specified by:
aClientConnected in interface IPictionaryClientCallback
Parameters:
client - The name of the new client that connected.

serverMessageReceived

public void serverMessageReceived(java.lang.String message)
Description copied from interface: IPictionaryClientCallback
This method is called when a message from the server sent to all the clients is received.

Specified by:
serverMessageReceived in interface IPictionaryClientCallback
Parameters:
message - The Message sent from the server.

clientBroadcastMessageReceived

public void clientBroadcastMessageReceived(java.lang.String client,
                                           java.lang.String message)
Description copied from interface: IPictionaryClientCallback
This method is called when a message from a client, broadcasted to all other clients, is received.

Specified by:
clientBroadcastMessageReceived in interface IPictionaryClientCallback
Parameters:
client - The name of the client that sent the message.
message - The Message sent from

connectedToServer

public void connectedToServer()
Description copied from interface: IPictionaryClientCallback
This method is called when the player successfully connected to a server.

Specified by:
connectedToServer in interface IPictionaryClientCallback

joinedNewGame

public void joinedNewGame(java.util.List<java.lang.String> players,
                          int gameStatus)
Description copied from interface: IPictionaryClientCallback
This method is called when the client successfully joined a game of Pictionary.

Specified by:
joinedNewGame in interface IPictionaryClientCallback
Parameters:
players - All the players participating in the game.
gameStatus - The status of the game, equal to a status int in PictionaryGame.

startDrawing

public void startDrawing(java.lang.String wordToDraw,
                         int drawTime)
Description copied from interface: IPictionaryClientCallback
This method is called when the player should start drawing a word.

Specified by:
startDrawing in interface IPictionaryClientCallback
Parameters:
wordToDraw - The word that the player should attempt to draw.
drawTime - The amount of time, in seconds, the player can draw before his time is up.

drawReceived

public void drawReceived(java.awt.Point point)
Description copied from interface: IPictionaryClientCallback
This method is called to notify the client that the drawing player drew at a specific point.

Specified by:
drawReceived in interface IPictionaryClientCallback
Parameters:
point - A Point containing the coordinates that were filled.

serverDisconnected

public void serverDisconnected()
Description copied from interface: IPictionaryClientCallback
This method is called when the connection to the server is lost.

Specified by:
serverDisconnected in interface IPictionaryClientCallback

stopDrawing

public void stopDrawing()
Description copied from interface: IPictionaryClientCallback
This method is called to notify the client that it should stop drawing.
This could happen when the draw time is up or when a player made a correct guess.

Specified by:
stopDrawing in interface IPictionaryClientCallback

startGuessing

public void startGuessing()
Description copied from interface: IPictionaryClientCallback
This method is called to notify the client that a player is drawing and that the client can start making guesses.

Specified by:
startGuessing in interface IPictionaryClientCallback

stopGuessing

public void stopGuessing()
Description copied from interface: IPictionaryClientCallback
This method is called to notify the client that it should stop guessing, for example when someone made a correct guess or when the drawing player's time is up.

Specified by:
stopGuessing in interface IPictionaryClientCallback

userNameRequest

public java.lang.String userNameRequest()
Description copied from interface: IPictionaryClientCallback
This method is called when the server has requested the client to respond with an username.
This method will be called until the server has accepted the username.

Specified by:
userNameRequest in interface IPictionaryClientCallback
Returns:
Returns The username that will identify this client, and will be seen by the other connected clients.

clientStartedDrawing

public void clientStartedDrawing(java.lang.String client)
Description copied from interface: IPictionaryClientCallback
This method is called when a client has started drawing.

Specified by:
clientStartedDrawing in interface IPictionaryClientCallback
Parameters:
client - The name of the client that started drawing.

closedByError

public void closedByError(java.lang.String error)
Description copied from interface: IPictionaryClientCallback
This method is called when the connection to the server has been closed.

Specified by:
closedByError in interface IPictionaryClientCallback
Parameters:
error - A description of the cause to the error.

main

public static void main(java.lang.String[] args)
Asks the user for hostname and port, and then creates an instance of #PictionaryClient using these values. Creates an instance of PictionaryClientGUI, passing the aforementioned client to its constructor.

Parameters:
args - Not used, the values are entered in an GUI.