/** * 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); } }