Note that there are some explanatory texts on larger screens.

plurals
  1. POSharing objects between multiple classes
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>I've already got a working Server which can handle multiple Client connections! (Tried and Tested).</p> <pre><code>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(); } } </code></pre> <p>Note this code was written for more of a pseudocode reference - didn't want to paste hundreds of lines.</p> <p>Any help would be much appreciated! Thanks alot for reading.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload