/** * This method sets up the panels used in this GUI and adds the appropriate listeners to the * components that require them. */ private void setupPanels() { serverSettings = new ServerSettings(); serverSettings.connectButton.addActionListener(listener); serverSettings.disconnectButton.addActionListener(listener); serverSettings.help.addActionListener(listener); dungeonPanel = new DungeonPanel(); dungeonPanel.addKeyListener(listener); dungeonPanel.addMouseListener(listener); dungeonPanelOverlay = new DungeonPanelOverlay(); dungeonPanelOverlay.endTurn.addMouseListener(listener); chatPanel = new ChatPanel(); chatPanel.send.addActionListener(listener); chatPanel.message.addKeyListener(listener); }
/** * This method updates the relevant sections of the GUI when a new LookReply and RenderHint are * received from the server. */ public void updateGUI(char[][] newCells, String[] newRenderHint, boolean ourTurn) { char[] playerDirections = processRenderHint(newRenderHint); boolean hasLantern = (newCells[0].length == 7); // the size of the lookreply determines whether // the player has a lantern dungeonPanel.updateCells(newCells, hasLantern, playerDirections); // updates the map panel int goldHeld = client.getGoldHeld(); int goldNeeded = client.getGoldNeeded(); dungeonPanelOverlay.updateOverlay(hasLantern, ourTurn, goldHeld, goldNeeded); // updates the overlay }
/** * This method handles the client pressing the Connect button. It catches the possible errors if * there were any, and connects the player to the server if there were none. */ public void connectToServer() { String hostname = serverSettings.hostnameField.getText(); String portNum = serverSettings.portNumField.getText(); String playerName = serverSettings.playerNameField.getText(); try { client = new Client(hostname, portNum, this); if (!playerName.equals("")) { client.sendHello(playerName); } // if no name was entered, the server will pick a default so we don't send HELLO toggleGUIStates(true); // let the GUI know we're now connected dungeonPanel.requestFocusInWindow(); // set the focus on the DungeonPanel so its KeyListeners // work without a click from the user } catch (NumberFormatException e) { showErrorMessage("Could not connect", "Invalid port number."); } catch (IOException e) { // if the server isn't available, because eg it is currently offline // or a mistake in the hostname/port number was made String errorMessage = "The server at " + hostname + ":" + portNum + " could not be reached."; showErrorMessage("Could not connect", errorMessage); } }
/** * This method changes all of the GUI components depending on whether the player is connected to a * server, because different functionality is needed when we're connected from when we're not. */ public void toggleGUIStates(boolean gameRunning) { serverSettings.flipButtonStates(gameRunning); chatPanel.setConnected(gameRunning); dungeonPanelOverlay.toggleGameStarted(gameRunning); dungeonPanel.setVisible(gameRunning); }