9
9
package Controller ;
10
10
11
11
import animatefx .animation .FadeIn ;
12
+ import javafx .application .Platform ;
12
13
import javafx .embed .swing .SwingFXUtils ;
13
14
import javafx .event .ActionEvent ;
14
15
import javafx .fxml .Initializable ;
15
- import javafx .geometry .NodeOrientation ;
16
+ import javafx .geometry .Pos ;
16
17
import javafx .scene .Node ;
17
18
import javafx .scene .control .Button ;
18
19
import javafx .scene .control .Label ;
19
- import javafx .scene .control .TextArea ;
20
20
import javafx .scene .control .TextField ;
21
21
import javafx .scene .image .Image ;
22
22
import javafx .scene .image .ImageView ;
23
23
import javafx .scene .input .KeyEvent ;
24
24
import javafx .scene .input .MouseEvent ;
25
+ import javafx .scene .layout .HBox ;
25
26
import javafx .scene .layout .Pane ;
27
+ import javafx .scene .layout .VBox ;
26
28
import javafx .scene .paint .Color ;
27
29
import javafx .scene .paint .ImagePattern ;
28
30
import javafx .scene .shape .Circle ;
29
- import javafx .scene .shape .Rectangle ;
31
+ import javafx .scene .text .Text ;
32
+ import javafx .scene .text .TextFlow ;
30
33
import javafx .stage .FileChooser ;
31
34
import javafx .stage .Stage ;
32
35
@@ -50,22 +53,9 @@ public class Client_Room extends Thread implements Initializable {
50
53
public Label gender ;
51
54
public ImageView proImage ;
52
55
public TextField fileChoosePath ;
53
- public TextArea msgRoom ;
54
56
public TextField msgField ;
55
57
public Pane chat ;
56
- public Button a ;
57
- public Button b ;
58
- public Button c ;
59
- public Button d ;
60
- public Button e ;
61
- public Button f ;
62
- public Button g ;
63
- public Button h ;
64
- public Button i ;
65
- public Button j ;
66
- public Button k ;
67
- public Button l ;
68
- public Rectangle emojiBox ;
58
+
69
59
70
60
public boolean toggleChat = false , toggleProfile = false ;
71
61
@@ -74,6 +64,8 @@ public class Client_Room extends Thread implements Initializable {
74
64
*/
75
65
76
66
public boolean saveControl = false ;
67
+ public VBox vbox ;
68
+
77
69
78
70
BufferedReader reader ;
79
71
PrintWriter writer ;
@@ -96,24 +88,11 @@ public void initialize(URL location, ResourceBundle resources) {
96
88
clientName .setText (Login_Signup .username );
97
89
connectSocket ();
98
90
99
- emojiBox .setVisible (false );
100
- a .setVisible (false );
101
- b .setVisible (false );
102
- c .setVisible (false );
103
- d .setVisible (false );
104
- e .setVisible (false );
105
- f .setVisible (false );
106
- g .setVisible (false );
107
- h .setVisible (false );
108
- i .setVisible (false );
109
- j .setVisible (false );
110
- k .setVisible (false );
111
- l .setVisible (false );
112
91
}
113
92
114
93
public void connectSocket () {
115
94
try {
116
- socket = new Socket ("localhost" , 5006 );
95
+ socket = new Socket ("localhost" , 5007 );
117
96
System .out .println ("Socket is connected with server!" );
118
97
reader = new BufferedReader (new InputStreamReader (socket .getInputStream ()));
119
98
writer = new PrintWriter (socket .getOutputStream (), true );
@@ -126,6 +105,121 @@ public void connectSocket() {
126
105
127
106
@ Override
128
107
public void run () {
108
+ try {
109
+ while (true ) {
110
+
111
+
112
+ String msg = reader .readLine ();
113
+ String [] tokens = msg .split (" " );
114
+ String cmd = tokens [0 ];
115
+
116
+ // txtTextArea.appendText(cmd+"\n");
117
+ StringBuilder fullMsg = new StringBuilder ();
118
+ for (int i = 1 ; i < tokens .length ; i ++) {
119
+ fullMsg .append (tokens [i ]);
120
+ }
121
+
122
+
123
+ String [] msgToAr = msg .split (" " );
124
+ String st = "" ;
125
+ for (int i = 0 ; i < msgToAr .length - 1 ; i ++) {
126
+ st += msgToAr [i + 1 ] + " " ;
127
+ }
128
+ //======================================================================
129
+
130
+
131
+ Text text = new Text (st );
132
+ String firstChars = "" ;
133
+ if (st .length () > 3 ) {
134
+ firstChars = st .substring (0 , 3 );
135
+
136
+ }
137
+
138
+
139
+ if (firstChars .equalsIgnoreCase ("img" )) {
140
+ //for the Images
141
+
142
+ st = st .substring (3 , st .length () - 1 );
143
+
144
+
145
+ File file = new File (st );
146
+ Image image = new Image (file .toURI ().toString ());
147
+
148
+ ImageView imageView = new ImageView (image );
149
+
150
+ imageView .setFitHeight (75 );
151
+ imageView .setFitWidth (150 );
152
+
153
+
154
+ HBox hBox = new HBox (10 );
155
+ hBox .setAlignment (Pos .BOTTOM_RIGHT );
156
+
157
+
158
+ if (!cmd .equalsIgnoreCase (Login_Signup .username )) {
159
+
160
+ vbox .setAlignment (Pos .TOP_LEFT );
161
+ hBox .setAlignment (Pos .CENTER_LEFT );
162
+
163
+
164
+ Text text1 = new Text (" " + cmd + " :" );
165
+ hBox .getChildren ().add (text1 );
166
+ hBox .getChildren ().add (imageView );
167
+
168
+ } else {
169
+ hBox .setAlignment (Pos .BOTTOM_RIGHT );
170
+ hBox .getChildren ().add (imageView );
171
+ Text text1 = new Text (": Me " );
172
+ hBox .getChildren ().add (text1 );
173
+
174
+ }
175
+
176
+ Platform .runLater (() -> vbox .getChildren ().addAll (hBox ));
177
+
178
+
179
+ } else {
180
+ //For the Text
181
+ /*text.setFill(Color.WHITE);*/
182
+ text .getStyleClass ().add ("message" );
183
+ TextFlow tempFlow = new TextFlow ();
184
+
185
+ if (!cmd .equalsIgnoreCase (Login_Signup .username + ":" )) {
186
+ Text txtName = new Text (cmd + " " );
187
+ txtName .getStyleClass ().add ("txtName" );
188
+ tempFlow .getChildren ().add (txtName );
189
+ }
190
+
191
+ tempFlow .getChildren ().add (text );
192
+ tempFlow .setMaxWidth (200 ); //200
193
+
194
+ TextFlow flow = new TextFlow (tempFlow );
195
+
196
+ HBox hBox = new HBox (12 ); //12
197
+
198
+ //=================================================
199
+
200
+
201
+ if (!cmd .equalsIgnoreCase (Login_Signup .username + ":" )) {
202
+
203
+ vbox .setAlignment (Pos .TOP_LEFT );
204
+ hBox .setAlignment (Pos .CENTER_LEFT );
205
+ hBox .getChildren ().add (flow );
206
+
207
+ } else {
208
+ // text.setFill(Color.WHITE);
209
+
210
+ hBox .setAlignment (Pos .BOTTOM_RIGHT );
211
+ hBox .getChildren ().add (flow );
212
+ }
213
+ hBox .getStyleClass ().add ("hbox" );
214
+ Platform .runLater (() -> vbox .getChildren ().addAll (hBox ));
215
+ }
216
+ }
217
+
218
+ } catch (Exception e ) {
219
+ e .printStackTrace ();
220
+ }
221
+ }
222
+ /* public void run() {
129
223
try {
130
224
while (true) {
131
225
String msg = reader.readLine();
@@ -150,7 +244,7 @@ public void run() {
150
244
} catch (Exception e) {
151
245
e.printStackTrace();
152
246
}
153
- }
247
+ }*/
154
248
155
249
public void handleProfileBtn (ActionEvent event ) {
156
250
if (event .getSource ().equals (profileBtn ) && !toggleProfile ) {
@@ -193,8 +287,11 @@ public void handleSendEvent(MouseEvent mouseEvent) {
193
287
public void send () {
194
288
msg = msgField .getText ();
195
289
writer .println (Login_Signup .username + ": " + msg );
196
- msgRoom .setNodeOrientation (NodeOrientation .LEFT_TO_RIGHT );
197
- msgRoom .appendText ("Me: " + msg + "\n " );
290
+
291
+ /* msgRoom.setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
292
+ msgRoom.appendText("Me: " + msg + "\n");*/
293
+
294
+
198
295
msgField .setText ("" );
199
296
if (msg .equalsIgnoreCase ("BYE" ) || (msg .equalsIgnoreCase ("logout" ))) {
200
297
System .exit (0 );
@@ -232,84 +329,12 @@ public void sendMessageByKey(KeyEvent event) {
232
329
}
233
330
234
331
235
- public void mouseClickedAnotherArea (MouseEvent mouseEvent ) {
236
- emojiBox .setVisible (false );
237
- a .setVisible (false );
238
- b .setVisible (false );
239
- c .setVisible (false );
240
- d .setVisible (false );
241
- e .setVisible (false );
242
- f .setVisible (false );
243
- g .setVisible (false );
244
- h .setVisible (false );
245
- i .setVisible (false );
246
- j .setVisible (false );
247
- k .setVisible (false );
248
- l .setVisible (false );
249
- }
250
-
251
332
public void cameraIconMouseClicked (MouseEvent mouseEvent ) {
252
- emojiBox .setVisible (true );
253
- a .setVisible (true );
254
- b .setVisible (true );
255
- c .setVisible (true );
256
- d .setVisible (true );
257
- e .setVisible (true );
258
- f .setVisible (true );
259
- g .setVisible (true );
260
- h .setVisible (true );
261
- i .setVisible (true );
262
- j .setVisible (true );
263
- k .setVisible (true );
264
- l .setVisible (true );
265
- }
266
-
267
- public void clickEmoji1 (MouseEvent mouseEvent ) {
268
- msgField .setText (msg + a .getText ());
269
- }
270
-
271
- public void clickEmoji2 (MouseEvent mouseEvent ) {
272
- msgField .setText (msg + b .getText ());
273
- }
274
333
275
- public void clickEmoji3 (MouseEvent mouseEvent ) {
276
- msgField .setText (msg + c .getText ());
277
- }
278
-
279
- public void clickEmoji4 (MouseEvent mouseEvent ) {
280
- msgField .setText (msg + d .getText ());
281
- }
282
-
283
- public void clickEmoji5 (MouseEvent mouseEvent ) {
284
- msgField .setText (msg + e .getText ());
285
- }
286
-
287
- public void clickEmoji6 (MouseEvent mouseEvent ) {
288
- msgField .setText (msg + f .getText ());
289
- }
290
-
291
- public void clickEmoji7 (MouseEvent mouseEvent ) {
292
- msgField .setText (msg + g .getText ());
293
- }
294
-
295
- public void clickEmoji8 (MouseEvent mouseEvent ) {
296
- msgField .setText (msg + h .getText ());
297
- }
298
-
299
- public void clickEmoji9 (MouseEvent mouseEvent ) {
300
- msgField .setText (msg + i .getText ());
301
- }
302
-
303
- public void clickEmoji10 (MouseEvent mouseEvent ) {
304
- msgField .setText (msg + j .getText ());
305
- }
306
-
307
- public void clickEmoji11 (MouseEvent mouseEvent ) {
308
- msgField .setText (msg + k .getText ());
309
- }
310
-
311
- public void clickEmoji12 (MouseEvent mouseEvent ) {
312
- msgField .setText (msg + l .getText ());
334
+ Stage stage = (Stage ) ((Node ) mouseEvent .getSource ()).getScene ().getWindow ();
335
+ fileChooser = new FileChooser ();
336
+ fileChooser .setTitle ("Open Image" );
337
+ this .filePath = fileChooser .showOpenDialog (stage );
338
+ writer .println (Login_Signup .username + " " + "img" + filePath .getPath ());
313
339
}
314
-
315
340
}
0 commit comments