Skip to content

Commit

Permalink
added error message on failed connection
Browse files Browse the repository at this point in the history
  • Loading branch information
bazola committed Sep 18, 2014
1 parent c88f3bb commit e273c2f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void acceptIPAndPort(String ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
}
public void connectToGame() {
public boolean connectToGame() {
// this is called on the object from the Game launcher before the scene is displayed
try {
this.socket = new Socket(this.ipAddress, this.port);
Expand All @@ -90,14 +90,12 @@ public void connectToGame() {
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
new Thread(this::listen).start();
} catch (Exception e) {
e.printStackTrace();
}

try {
new Thread(this::play).start();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Connection Failed");
return false;
}

new Thread(this::play).start();
return true;
}
private void play() {
// this method only runs once at the start
Expand Down Expand Up @@ -296,7 +294,7 @@ private void processUseableActionMessage(UseableActionMessage message) {
double paneHeight = actionBox.getHeight();
double paneWidth = actionBox.getWidth();

int maxActions = 5;
int maxActions = 8;
double actionWidth = paneWidth / maxActions;

ActionButton actionButton = new ActionButton(message, this, actionWidth, paneHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,52 @@
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

//This class just loads the FXML document which initializes its DocumentController

public class GameClientLauncherController implements Initializable {

@FXML
private TextField ipAddressBox;

@FXML
private TextField portBox;

@FXML
private Button connectButton;

@FXML
private Label errorMessage;
@FXML private TextField ipAddressBox;
@FXML private TextField portBox;
@FXML private Button connectButton;
@FXML private Label errorMessage;
@FXML private AnchorPane anchorPane;

private String getCharactersFromTextField(TextField textField) {
return textField.getCharacters().toString();
}

private void buttonClick(ActionEvent event) {
//Get values from the TextFields
String ipAddressValue = this.getCharactersFromTextField(ipAddressBox);
int portValue = Integer.parseInt(this.getCharactersFromTextField(portBox));

//Attempt to make a connection
try {
//Send a test to the server, to make sure that it is valid

//if it is valid
errorMessage.setText("Success!");
this.closeWithSuccess(event);
this.switchToMainGameWindow(ipAddressValue, portValue);
} catch (Exception e) {
String message = e.getMessage();
errorMessage.setText(message);
}
this.switchToMainGameWindow(ipAddressValue, portValue);
}

private void closeWithSuccess(ActionEvent event) {
Node source = (Node)event.getSource();
private void closeWithSuccess() {
Node source = anchorPane;
Stage stage = (Stage)source.getScene().getWindow();
stage.close();
}

private void switchToMainGameWindow(String ipAddress, int port) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ClientDocument.fxml"));

Parent root = (Parent)loader.load();

GameClientController controller = loader.<GameClientController>getController();
controller.acceptIPAndPort(ipAddress, port);
controller.connectToGame();

Scene scene = new Scene(root);

Stage gameStage = new Stage();

gameStage.setScene(scene);
//stage.centerOnScreen();
gameStage.show();
if (controller.connectToGame()) {
errorMessage.setText("Success!");
this.closeWithSuccess();

Scene scene = new Scene(root);
Stage gameStage = new Stage();
gameStage.setScene(scene);
gameStage.show();
} else {
errorMessage.setText("Connection Failed!");
}
}
catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -84,7 +66,6 @@ private void switchToMainGameWindow(String ipAddress, int port) {

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
connectButton.setOnAction(this::buttonClick);
ipAddressBox.setText("127.0.0.1");
portBox.setText("4242");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.client.GameClientLauncherController">
<AnchorPane fx:id="anchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.client.GameClientLauncherController">
<children>
<AnchorPane layoutX="182.0" layoutY="25.0" prefHeight="102.0" prefWidth="200.0">
<children>
Expand Down

0 comments on commit e273c2f

Please sign in to comment.