1 import java.rmi.*;
2
3 import javafx.application.Application;
4 import javafx.application.Platform;
5 import javafx.stage.Stage;
6 import javafx.scene.Scene;
7 import javafx.scene.control.Label;
8 import javafx.scene.layout.BorderPane;
9 import javafx.scene.layout.GridPane;
10 import javafx.scene.layout.Pane;
11 import javafx.scene.paint.Color;
12 import javafx.scene.shape.Line;
13 import javafx.scene.shape.Ellipse;
14
15 import java.rmi.registry.Registry;
16 import java.rmi.registry.LocateRegistry;
17
18 public class TicTacToeClientRMI extends Application {
19
20 private char marker;
21
22
23 private boolean myTurn = false;
24
25
26 private char whoseTurn = 'X';
27
28
29 private Cell[][] cell = new Cell[3][3];
30
31
32 private Label lblStatus = new Label("X's turn to play");
33
34
35
36 private TicTacToeInterface ticTacToe;
37
38 private Label lblIdentification = new Label();
39
40 @Override
41 public void start(Stage primaryStage) {
42
43 GridPane pane = new GridPane();
44 for (int i = 0; i < 3; i++)
45 for (int j = 0; j < 3; j++)
46 pane.add(cell[i][j] = new Cell(i, j), j, i);
47
48 BorderPane borderPane = new BorderPane();
49 borderPane.setCenter(pane);
50 borderPane.setTop(lblStatus);
51 borderPane.setBottom(lblIdentification);
52
53
54 Scene scene = new Scene(borderPane, 450, 170);
55 primaryStage.setTitle("TicTacToe");
56 primaryStage.setScene(scene);
57 primaryStage.show();
58
59 new Thread( () -> {
60 try {
61 initializeRMI();
62 }
63 catch (Exception ex) {
64 ex.printStackTrace();
65 }}).start();
66 }
67
68
69 protected boolean initializeRMI() throws Exception {
70 String host = "";
71
72 try {
73 Registry registry = LocateRegistry.getRegistry(host);
74 ticTacToe = (TicTacToeInterface)
75 registry.lookup("TicTacToeImpl");
76 System.out.println
77 ("Server object " + ticTacToe + " found");
78 }
79 catch (Exception ex) {
80 System.out.println(ex);
81 }
82
83
84
85 CallBackImpl callBackControl = new CallBackImpl(this);
86
87 if (
88 (marker =
89 ticTacToe.connect((CallBack)callBackControl)) != ' ')
90 {
91 System.out.println("connected as " + marker + " player.");
92 Platform.runLater(() ->
93 lblIdentification.setText("You are player " + marker));
94 return true;
95 }
96 else {
97 System.out.println("already two players connected as ");
98 return false;
99 }
100 }
101
102
103 public void setMyTurn(boolean myTurn) {
104 this.myTurn = myTurn;
105 }
106
107
108 public void setMessage(String message) {
109 Platform.runLater(() -> lblStatus.setText(message));
110 }
111
112
113 public void mark(int row, int column, char token) {
114 cell[row][column].setToken(token);
115 }
116
117
118 public class Cell extends Pane {
119
120 private boolean marked = false;
121
122
123 int row, column;
124
125
126 private char token = ' ';
127
128 public Cell(final int row, final int column) {
129 this.row = row;
130 this.column = column;
131 setStyle("-fx-border-color: black");
132 this.setPrefSize(2000, 2000);
133 this.setOnMouseClicked(e -> handleMouseClick());
134 }
135
136
137 public char getToken() {
138 return token;
139 }
140
141
142 public void setToken(char c) {
143 token = c;
144 marked = true;
145
146 if (token == 'X') {
147 Line line1 = new Line(10, 10,
148 this.getWidth() - 10, this.getHeight() - 10);
149 line1.endXProperty().bind(this.widthProperty().subtract(10));
150 line1.endYProperty().bind(this.heightProperty().subtract(10));
151 Line line2 = new Line(10, this.getHeight() - 10,
152 this.getWidth() - 10, 10);
153 line2.startYProperty().bind(
154 this.heightProperty().subtract(10));
155 line2.endXProperty().bind(this.widthProperty().subtract(10));
156
157
158 Platform.runLater(() ->
159 this.getChildren().addAll(line1, line2));
160 }
161 else if (token == 'O') {
162 Ellipse ellipse = new Ellipse(this.getWidth() / 2,
163 this.getHeight() / 2, this.getWidth() / 2 - 10,
164 this.getHeight() / 2 - 10);
165 ellipse.centerXProperty().bind(
166 this.widthProperty().divide(2));
167 ellipse.centerYProperty().bind(
168 this.heightProperty().divide(2));
169 ellipse.radiusXProperty().bind(
170 this.widthProperty().divide(2).subtract(10));
171 ellipse.radiusYProperty().bind(
172 this.heightProperty().divide(2).subtract(10));
173 ellipse.setStroke(Color.BLACK);
174 ellipse.setFill(Color.WHITE);
175
176 Platform.runLater(() ->
177 getChildren().add(ellipse));
178 }
179 }
180
181
182 private void handleMouseClick() {
183 if (myTurn && !marked) {
184
185 setToken(marker);
186
187
188 try {
189 ticTacToe.myMove(row, column, marker);
190 }
191 catch (RemoteException ex) {
192 System.out.println(ex);
193 }
194 }
195 }
196 }
197
198
202 public static void main(String[] args) {
203 launch(args);
204 }
205 }