Java Reference
In-Depth Information
32
// Create and initialize a title label
33
private Label lblTitle = new Label();
34
35
// Create and initialize a status label
36
private Label lblStatus = new Label();
37
38
// Indicate selected row and column by the current move
39
private int rowSelected;
40
private int columnSelected;
41
42
// Input and output streams from/to server
43
private DataInputStream fromServer;
44
private DataOutputStream toServer;
45
46
// Continue to play?
47
private boolean continueToPlay = true ;
48
49
// Wait for the player to mark a cell
50
private boolean waiting = true ;
51
52
// Host name or ip
53
private String host = "localhost" ;
54
55 @Override // Override the start method in the Application class
56 public void start(Stage primaryStage) {
57 // Pane to hold cell
58 GridPane pane = new GridPane();
59 for ( int i = 0 ; i < 3 ; i++)
60 for ( int j = 0 ; j < 3 ; j++)
61 pane.add(cell[i][j] = new Cell(i, j), j, i);
62
63 BorderPane borderPane = new BorderPane();
64 borderPane.setTop(lblTitle);
65 borderPane.setCenter(pane);
66 borderPane.setBottom(lblStatus);
67
68 // Create a scene and place it in the stage
69 Scene scene = new Scene(borderPane, 320 , 350 );
70 primaryStage.setTitle( "TicTacToeClient" ); // Set the stage title
71 primaryStage.setScene(scene); // Place the scene in the stage
72 primaryStage.show(); // Display the stage
73
74
create UI
// Connect to the server
75
connectToServer();
connect to server
76 }
77
78
private void connectToServer() {
79
try {
80
// Create a socket to connect to the server
81
Socket socket = new Socket(host, 8000 );
82
83
// Create an input stream to receive data from the server
84
fromServer = new DataInputStream(socket.getInputStream());
input from server
85
86
// Create an output stream to send data to the server
87
toServer = new DataOutputStream(socket.getOutputStream());
output to server
88 }
89 catch (Exception ex) {
90 ex.printStackTrace();
91 }
 
Search WWH ::




Custom Search