Class Tracksail

java.lang.Object
  extended byjava.rmi.server.RemoteObject
      extended byjava.rmi.server.RemoteServer
          extended byjava.rmi.server.UnicastRemoteObject
              extended byTracksail
All Implemented Interfaces:
ClientInterface, java.rmi.Remote, java.io.Serializable

public class Tracksail
extends java.rmi.server.UnicastRemoteObject
implements ClientInterface

Class Tracksail implements the client program of Tracksail.

When the program is started, the user is asked for the player name and host machine. A connection to the host is attempted an if the attempt is successful, we get a server object that implements the ServerInterface. Any calls to the server are done via this interface.

If the connection and permission are received, the client program is initialised and a Swing-based GUI is constructed with a JFrame frame as it's root.

After the program has been started, there are two main states: lobby and race. Depending on the current state, the frame shows either a lobby room or a race room.

- In lobby room we have a largish chat-box in the middle. At the bottom, we have a text field where a message can be written. The message can be sent either by pressing return or by clicking on the 'Say'-button. On the right-hand side there is a race panel, which has a list of the current races, buttons to join an existing race or create a new race and a text field where a name for the race to create is written.

- In race room we have a large sea-view (render_view), where all the race graphics are drawn. At the bottom, there are '<-' and '->' buttons, with which the player can turn his/her boat, and a slider that controls the sail. Under these we have a text field and a 'Say' button, which work as they do in the lobby. In the race room, chat messages are shown in the render_view area, instead of a separate chat area, and only a few of the latest messages are shown at a time.

Tracksail implements the ClientInterface, which is an interface for making method calls from the server to the clients. This client interface object is sent to the server during the intialisation of the connection as one parameter in the server call (see login()).

Version:
1.0
Author:
Stefan Brockmann, Tero Kuusela
See Also:
ClientInterface, Player, Game, Server, Track, Serialized Form

Nested Class Summary
(package private)  class Tracksail.ChangeTrackItemListener
          Handles the changes of the choice in the track list.
(package private)  class Tracksail.CreateGameActionListener
          Handles clicking the 'create game' button.
(package private)  class Tracksail.ExitActionListener
          Handles clicking the 'Exit' button.
(package private)  class Tracksail.JoinGameActionListener
          Handles clicking the 'join game' button.
(package private)  class Tracksail.JRenderView
          Custom GUI component, a drawing area on which all graphics of the game are drawn.
(package private)  class Tracksail.SailChangeListener
          Handles changes in the slider controlling the sail.
(package private)  class Tracksail.SayItActionListener
          Handles events that are generated when the player clicks the 'say' button or hits enter with focus in the chat input text area.
(package private)  class Tracksail.StartGameActionListener
          Handles clicking the 'start game' in a race.
(package private)  class Tracksail.TurnLeftAction
          Handles clicking the '<' button.
(package private)  class Tracksail.TurnRightAction
          Handles clicking the '>' button.
 class Tracksail.WindowCloser
          Handles events that are generated when the player closes the window.
 
Field Summary
(package private)  boolean active
          Tells if we're still executing the program.
(package private)  javax.swing.JTextArea chat_messages
          Multiline text area where chat messages are output to
(package private)  javax.swing.JScrollPane chat_scroll
          This makes the text area to scroll up so only the latest messages are seen at the bottom
private  java.util.ArrayList chat_text
          The latest chat messages.
(package private)  javax.swing.JButton create_game_button
          Button to create a new race
(package private)  javax.swing.JButton exit_button
          Button to leave the program if we're at the lobby room.
protected  javax.swing.JFrame frame
          Main window
(package private)  Game game
          Current state of the race (if we are in one)
private  int game_id
          Unique id of the race the player is in (if we are in one)
(package private)  javax.swing.JList game_list
          List of the races available
(package private)  GameListModel game_list_model
          Model for the race list
(package private)  javax.swing.JTextField game_name_text
          Text field for the name of a new race
private  java.lang.String host_name
          Name of the host
(package private)  javax.swing.JTextField info_text
          Uneditable text field for the program to write information to
(package private)  boolean is_lobby
          Are we in a lobby room or in a race room? True means we're in a lobby, false means we're in a race
private  boolean is_master
          Is this player the master of the current race?
(package private)  javax.swing.JButton join_button
          Button to join a race
(package private)  javax.swing.JPanel lobby_panel
          Panel that's visible when in lobby room
(package private)  javax.swing.JPanel main_panel
          Main panel where all other content is placed
private  int player_id
          Player's unique id
private  java.lang.String player_name
          Player name
(package private)  javax.swing.JPanel race_panel
          This is the panel that's visible when we're racing
(package private)  Tracksail.JRenderView render_view
          Racing view.
(package private)  javax.swing.JSlider sail_slider
          Slider to set the sail control rope tightness
(package private)  javax.swing.JButton say_it
          Button that sends a chat message from this player
(package private)  javax.swing.JTextField say_text
          Text field for the player to write chat messages to
(package private)  javax.swing.JButton start_button
          Button to start the race
(package private)  javax.swing.JComboBox track_list
          List to change the track
(package private)  TrackListModel track_list_model
          Model for the track list
(package private)  ServerInterface ts_server
          An interface used to call the remote methods of the server
(package private)  javax.swing.JButton turn_left_button
          Button to turn the boat portboard
(package private)  javax.swing.JButton turn_right_button
          Button to turn the boat starboard
static java.lang.String version
           
 
Fields inherited from class java.rmi.server.UnicastRemoteObject
 
Fields inherited from class java.rmi.server.RemoteServer
 
Fields inherited from class java.rmi.server.RemoteObject
ref
 
Constructor Summary
Tracksail()
          Constructor.
 
Method Summary
 void connect()
          Establishes a RMI connection to the server.
 void createGUI()
          This method constructs the client GUI.
 java.awt.image.BufferedImage[] createRotatedImages(java.lang.String filename, int num_images)
          Creates a rotated image series.
 void error(java.lang.String str)
          Common error handler.
 void game_loop()
          Game loop.
 void gameStateChange(boolean racing)
          The server tells the player the state of the race when it changes (racing/paused).
 java.awt.image.BufferedImage getBufferedImage(java.lang.String filename)
          Loads an image from a file.
 void init()
          Initialize program.
 void kick(java.lang.String message)
          Kicks out a player.
static void main(java.lang.String[] args)
          This is where the execution begins.
 void sendText(java.lang.String str)
          Sends a message to the client.
 void switchToGame()
          Changes from lobby state to racing state.
 void switchToLobby()
          Changes from racing state to lobby state.
 void uninit()
          Logs a player out from the server.
 void updateGameList(GameInfo[] ri)
          Send an updated race list from the server to the client.
 void updateTrackList(java.lang.String[] tracks)
          Send an updated track list from the server to the client.
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

version

public static final java.lang.String version
See Also:
Constant Field Values

host_name

private java.lang.String host_name
Name of the host


player_name

private java.lang.String player_name
Player name


player_id

private int player_id
Player's unique id


game_id

private int game_id
Unique id of the race the player is in (if we are in one)


game

Game game
Current state of the race (if we are in one)


is_master

private boolean is_master
Is this player the master of the current race?


frame

protected javax.swing.JFrame frame
Main window


is_lobby

boolean is_lobby
Are we in a lobby room or in a race room? True means we're in a lobby, false means we're in a race


active

boolean active
Tells if we're still executing the program. We exit the game loop when this variable is set to false.


chat_text

private java.util.ArrayList chat_text
The latest chat messages. These are for the race room, only the latest messages are shown.


ts_server

ServerInterface ts_server
An interface used to call the remote methods of the server


lobby_panel

javax.swing.JPanel lobby_panel
Panel that's visible when in lobby room


info_text

javax.swing.JTextField info_text
Uneditable text field for the program to write information to


chat_messages

javax.swing.JTextArea chat_messages
Multiline text area where chat messages are output to


say_text

javax.swing.JTextField say_text
Text field for the player to write chat messages to


say_it

javax.swing.JButton say_it
Button that sends a chat message from this player


game_list

javax.swing.JList game_list
List of the races available


join_button

javax.swing.JButton join_button
Button to join a race


create_game_button

javax.swing.JButton create_game_button
Button to create a new race


game_name_text

javax.swing.JTextField game_name_text
Text field for the name of a new race


game_list_model

GameListModel game_list_model
Model for the race list


exit_button

javax.swing.JButton exit_button
Button to leave the program if we're at the lobby room. If we're racing, this button exits to the lobby room.


chat_scroll

javax.swing.JScrollPane chat_scroll
This makes the text area to scroll up so only the latest messages are seen at the bottom


race_panel

javax.swing.JPanel race_panel
This is the panel that's visible when we're racing


render_view

Tracksail.JRenderView render_view
Racing view. This is where we draw the race graphics.


start_button

javax.swing.JButton start_button
Button to start the race


track_list

javax.swing.JComboBox track_list
List to change the track


track_list_model

TrackListModel track_list_model
Model for the track list


turn_left_button

javax.swing.JButton turn_left_button
Button to turn the boat portboard


turn_right_button

javax.swing.JButton turn_right_button
Button to turn the boat starboard


sail_slider

javax.swing.JSlider sail_slider
Slider to set the sail control rope tightness


main_panel

javax.swing.JPanel main_panel
Main panel where all other content is placed

Constructor Detail

Tracksail

public Tracksail()
          throws java.rmi.RemoteException
Constructor.

Method Detail

main

public static void main(java.lang.String[] args)
This is where the execution begins.

Parameters:
args - command line parameters

init

public void init()
          throws java.lang.Exception
Initialize program.

Show startup dialog, construct the GUI and connect to a server.

Throws:
java.lang.Exception

connect

public void connect()
             throws java.lang.Exception
Establishes a RMI connection to the server.

Returns:
true if connection was successfully established, false if the connection could not be established
Throws:
java.lang.Exception

uninit

public void uninit()
Logs a player out from the server.


error

public void error(java.lang.String str)
Common error handler.

Prints out the error message and exits the program (force exit).

Parameters:
str - error message

createGUI

public void createGUI()
This method constructs the client GUI.


switchToGame

public void switchToGame()
Changes from lobby state to racing state.


switchToLobby

public void switchToLobby()
Changes from racing state to lobby state.


game_loop

public void game_loop()
Game loop.


getBufferedImage

public java.awt.image.BufferedImage getBufferedImage(java.lang.String filename)
Loads an image from a file.

Waits until the image is fully loaded before returning it as an image object.

Parameters:
filename - name of the image file to load
Returns:
Image object.

createRotatedImages

public java.awt.image.BufferedImage[] createRotatedImages(java.lang.String filename,
                                                          int num_images)
Creates a rotated image series.

E.g. if an image series contains 36 images, we create 36 images out of the original image, of which the first one is rotated by 0 degrees, next by 10, then by 20 and so on. When done, the series has images to rotate the original by full 360 degrees.

Parameters:
filename - name of the image file to generate the series from
num_images - how many images the series is to contain
Returns:
An array reference to the image series. The array contains num_images of BufferedImage objects.

sendText

public void sendText(java.lang.String str)
              throws java.rmi.RemoteException
Sends a message to the client.

Specified by:
sendText in interface ClientInterface
Parameters:
str - message
Throws:
java.rmi.RemoteException

kick

public void kick(java.lang.String message)
          throws java.rmi.RemoteException
Kicks out a player.

Specified by:
kick in interface ClientInterface
Parameters:
message - informational message printed to the console
Throws:
java.rmi.RemoteException

updateGameList

public void updateGameList(GameInfo[] ri)
                    throws java.rmi.RemoteException
Send an updated race list from the server to the client.

This is called for all players if a new race is created or an existing race is closed. This is also always called for a player when (s)he signs in to the server.

Specified by:
updateGameList in interface ClientInterface
Parameters:
ri - the race list to give
Throws:
java.rmi.RemoteException

updateTrackList

public void updateTrackList(java.lang.String[] tracks)
                     throws java.rmi.RemoteException
Send an updated track list from the server to the client.

This is called for all players always when the track list changes. This is also always called for a player when (s)he signs in to the server.

Specified by:
updateTrackList in interface ClientInterface
Parameters:
tracks - the track list to give
Throws:
java.rmi.RemoteException

gameStateChange

public void gameStateChange(boolean racing)
                     throws java.rmi.RemoteException
The server tells the player the state of the race when it changes (racing/paused).

So the server calls this e.g. when a race begins or ends.

Specified by:
gameStateChange in interface ClientInterface
Parameters:
racing - true if the race begun, false if it ended.
Throws:
java.rmi.RemoteException