1

I'm implementing a Blackjack game and I'm trying to allow multiple classes - (ChatGUI and GameGUI respectively) to use my "Client" class, which effectively is the doorway to the server.

I've tried creating these classes with the Client class as an argument to each constructor, but it appears as if my Client class cannot send information back to the other classes- ChatGUI/ GameGUI.

I've already got a working Server which can handle multiple Client connections! (Tried and Tested).

public class BlackJack{
    private Client client;
    private ChatGUI chatgui;
    private GameGUI gamegui;

    public BlackJack(){
        // Setup Client class, which will be passed to all other classes
        client = new Client(server, port, username, chatgui, gamegui);

        // Setup other classes, which will be given Client class
        chatgui = new ChatGUI(client);
        gamegui = new GameGUI(client); 

    }   
}

public class Client{
    private ChatGUI chatgui;
    private GameGUI gamegui;

    Client(String server, int port, String username, ChatGUI cg, GameGUI gamegui){
        // catch these arguments and assign them to variables     
    }

    void display(String msg){
        // Method to display incoming messages to the chat screen
        // Using ChatGUI method (*** not working? ***)
        chatgui.append(msg + "\n");
    }
}

public class ChatGUI{
    private JTextArea textarea;
    private Client client;

    public ChatGUI(Client c){
        client = c;
    }

    // ChatGUI can use client methods
    void sendMessage(String msg){
        client.sendChat(msg);
     }

    void append(String msg){
        textarea.append(msg);
}

public class GameGUI{
    private Client client;

    public GameGUI(Client c){
        client = c;
    }

    // GameGUI can use client methods
    void playGame(){
        client.playGame();
    }
}

Note this code was written for more of a pseudocode reference - didn't want to paste hundreds of lines.

Any help would be much appreciated! Thanks alot for reading.

1
  • Are your client classes/methods running on separate threads from the server class/method? If not, then your server won't get any data from the clients without explicitly calling them Commented Apr 24, 2013 at 15:38

3 Answers 3

2

Assuming I am understanding your question correctly, I think it would be beneficial for you to structure your classes a little differently...

Each BlackJack object has a client, e.g.

public class BlackJack{
    private Client client;

    public BlackJack(){
        // Setup Client class, which will be passed to all other classes
        client = new Client(server, port, username);

    }  

Then each Client object has an instance of the GUI classes, e.g.

public class Client{
    private ChatGUI chatgui;
    private GameGUI gamegui;

    Client(String server, int port, String username){
             chatgui = new ChatGUI(this);
             gamegui = new GameGUI(this);
    }

    //handle messages from server
    void onMessageRecieved(String msg){
            if(/* the message pertains to the game gui */ )
                    gamegui.newMessage(msg);
            else if( /* the message pertains to the chat gui */ )
                    chatgui.newMessage(msg);
    }

    //here you can add functions for the gui classes to call
    public void sendChat(String chat){

    }
}

then your gui classes could look like...

public class ChatGUI{
    private JTextArea textarea;
    private Client client;

    public ChatGUI(Client c){
        client = c;
    }

    //receive message/command from server
    public void newMessage(String msg){
        //perform the desired action based on the command
    }

    public void sendChat(String msg){
        client.sendChat(msg);
    }
}

public class GameGUI{
    private Client client;

    public GameGUI(Client c){
        client = c;
    }

    //receive message/command from server
    public void newMessage(String msg){
        //perform the desired action based on the command
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

you are passing a reference of chatGUI to your client class and then after doing that actually instantiating chatGUI. So the reference passed earlier is incorrect. You need to first instantiate chatGUI and gameGUI objects. Then create the client object and set the references correctly. Don't try to pass the references in the constructor since at that time they are not valid.

Comments

0

hum, I wonder if I understand correctly, but is it that what you do want is : Client1 send a chat message to Client2 ?

in other word : chat1-->Client1-->client2-->gui2

If this is correct, then how the message is transmitted to client2 ?

1 Comment

This question should be a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.