Title: gameNet Interfaces
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

gameNet Interfaces

Steps to using gameNet

I am going to use the guiChat program to demonstrate examples of gameNet classes described in the above diagram.

1. Create a class which implements Serializable that defines inputs from your GUI to be sent to your Game. Lets call this class MyGameInput. This can be anything you want. The following is what guiChat contains:


package GuiChat;

import java.io.Serializable;

public class MyGameInput implements Serializable
{
    private static final long serialVersionUID = 1L;
    String myMsg;
    String myName;

    public MyGameInput(String n)
    {
       myName = n;
    }

    public void setMsg(String m)
    {
        myMsg = m;
    }
}


2. Create a class which implements Serializable that defines outputs from your Game that need to be sent to your GUI to be drawn. Lets call this class MyGameOutput. This can be anything you want. This is what I have in guiChat:


package GuiChat;
import java.io.Serializable;

public class MyGameOutput  implements Serializable
{
    private static final long serialVersionUID = 1L;
    String sendersMsg;
    String sendersName;

    public void copyMsg(MyGameInput myInput)
    {
        sendersName = myInput.myName;
        sendersMsg = myInput.myMsg;
    }

    public String toString()
    {
        return sendersName+ ": " + sendersMsg ;
    }
}


3. Create a class which extends an abstract class called GameNet_CoreGame and implements Serializable. Lets call this class MyGame. The one abstract method that you must implement is called process. Process takes one input of type Object. Note that this input will actually be your game's MyGameInput class. The method process returns a class of type Object which will actually be your MyGameOutput class. Note that the GameNet_CoreGame abstract class contains your Server Code. The actual definition of process is as follows:

Object process(Object);

Your game class will look something like:

public class MyGame extends GameNet_CoreGame
              implements Serializable
{
    ......
    public Object process(Object inputs)
    {
        MyGameInput myGameInput = (MyGameInput)inputs;
       ......
        //Using the myGameInput received from one your client programs, update the game.
       ......
       MyGameOutput myGameOutput = new MyGameOutput(...);
       ......
       // Send back to all of your client programs an instance of MyGameOutput
       return myGameOutput;
    }
    .....
    // Any supporting routines needed

}
	

In the case of guiChat, this routine is pretty simple:


package GuiChat;

import gameNet.GameNet_CoreGame;

public class MyGame extends GameNet_CoreGame{
    public Object process(Object ob)
    {
        // Process takes the name and msg from the GameInputObj
        // and copies it to the GameOutputObj

        MyGameInput myGameInput = (MyGameInput)ob;
        MyGameOutput myGameOutput = new MyGameOutput();
        myGameOutput.copyMsg(myGameInput); // Copy Input name and message to Output name and Message

        // Broadcast message to All Clients
        return myGameOutput;
    }
}


4. There was a little lie in the above. Your MyGame may not need to implement Serializable. Only MyGameOutput needs to implement Serializable. However, in most of my games, I end up sending back a copy of the MyGame class in the MyGameOutput class which must be Serializable.
5. MyMain - A small class to tie it altogether. This small class will extend GameCreator and will require a method called createGame which will return an instance of CoreGame. This class will also contain main. The code for this small class will almost always look something like:


package GuiChat;

import gameNet.GameCreator;
import gameNet.GameNet_CoreGame;
import gameNet.GameNet_UserInterface;

public class MyMain extends GameCreator{

  public GameNet_CoreGame createGame()
  {
	  return new MyGame();
  }

  public static void main(String[] args)
  {
  	MyMain myMain = new MyMain();
  	GameNet_UserInterface myUserInterface = new MyUserInterface();

  	myMain.enterGame(myUserInterface);
  }// end of main
}// end of class


6. You will need to create a class for your User Interface which implements the GameNet_UserInterface interface. Lets call this class MyUserInterface. This interface has 2 required methods. The first method is called receivedMessage which receives a class of type Object. This class will actually be of type MyGameOutput. This method will be called whenever the process method in GameNet_CoreGame executes and it is sending a MyGameOutput class to all connected User Interface classes. The other method required in the Interface_UserInterface interface is the method startUserInterface. This method is called when the game is ready to roll, and a GamePlayer object is passed into startUserInterface. The GamePlayer object is required for sending user inputs to the Game class. The GameNet_UserInterface interface looks like the following:

public interface GameNet_UserInterface
{
public void receivedMessage(Object ob);
public void startUserInterface (GamePlayer player);
}
When the startUserInterface method is called, you will want to save the player parameter. When your user interface wants to send MyGameInput objects to the MyGame class, the call will look something like:
player.sendMessage(MyGameInput );

If you want to know the name of the Player that is entering the game in startUserInterface it can be found as follows:
String playerName = player.getPlayerName();
The guiChat MyUserInterface is as follows:


package GuiChat;

import gameNet.GameNet_UserInterface;
import gameNet.GamePlayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


class MyUserInterface extends JFrame
    implements GameNet_UserInterface, ActionListener
{
    private GamePlayer myGamePlayer;
    private JTextArea chatStatusTextArea = new JTextArea(20, 70);
    private JTextField chatInputTextField = new JTextField(70);

    private MyGameInput myGameInput;
    private String lineSeparator = System.getProperty("line.separator");

    public MyUserInterface()
    {
        super("Chat Room");
    }


    public void startUserInterface (GamePlayer player)
    {
        myGamePlayer = player;
        myGameInput = new MyGameInput(player.getPlayerName());

        // Boring screen things
        myLayout();

        myGameInput.setMsg("just entered chat room");
        myGamePlayer.sendMessage(myGameInput);
    }


    public void receivedMessage(Object ob)
    {
        chatStatusTextArea.setText( ob.toString()  +
                lineSeparator + chatStatusTextArea.getText());
    }

    public void actionPerformed(ActionEvent e) {
        String str = chatInputTextField.getText();
        if (!myGamePlayer.GameIsAlive() ||
                "quit".equals(str))
        {
            exitProgram();
        }
        else
        {
            myGameInput.setMsg(str);
            myGamePlayer.sendMessage(myGameInput);
            chatInputTextField.setText("");
        }
    }

    // Nice to let people know you are leaving
    private void exitProgram()
    {
        myGameInput.setMsg("exitting chat room");
        myGamePlayer.sendMessage(myGameInput);
        myGamePlayer.doneWithGame();
        System.exit(0);
    }

    private void myLayout()
    {
        setLayout(new BorderLayout());
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(2,1));
        chatInputTextField.setBackground(Color.yellow);
        topPanel.add(new JLabel("Enter Message to Send to other Chatters (quit to exit)"));
        topPanel.add(chatInputTextField);
        add(topPanel, BorderLayout.NORTH);

        chatStatusTextArea.setBackground(Color.lightGray);
        JScrollPane scrolledText = new JScrollPane(chatStatusTextArea);
        scrolledText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrolledText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        add(scrolledText, BorderLayout.CENTER);
        chatInputTextField.addActionListener(this);
        addWindowListener (new Termination());

        setSize(800, 600);
        setVisible(true);
    }

    // Inner Class
    class Termination extends WindowAdapter
    {
        public void windowClosing(WindowEvent e)
        {
            exitProgram();
        }
    }

}


July 22, 2014 9:46 PM