You are on page 1of 7

GUI.

java
1 package application;
2
3 import java.awt.image.BufferedImage;
42
43 public class GUI extends Application {
44
45 private String DisplayMode = "Message";
46 private ClypeClient client;
47
48 private Label DisplayLabel;
49 private TextField Text;
50 private VBox DisplayBox, FriendBox;
51
52 public static boolean send = false;
53 public static boolean sendPicture = false;
54
55 private boolean isPic = false;
56
57 public void init() {
58
59 client = new ClypeClient(this);
60 client.start();
61
62 }
63 public VBox addFriends(VBox Friends){
64 //FriendLabel
65 Label FriendLabel = new Label();
66 FriendLabel.setText("New Friend");
67 FriendLabel.setStyle("-fx-font: 24 arial;");
68 Friends.getChildren().add(FriendLabel);
69 return Friends;
70 }
71
72 public void close() {
73 Platform.exit();
74 }
75
76 public void start (Stage primaryStage) {
77 try {
78 //SetUpScene
79 GridPane root = new GridPane();
80 root.setStyle("-fx-background-color: #F0F8FF");
81 Scene scene = new Scene(root,900,900);
82
83 //DisplayBox
84 DisplayBox = new VBox();
85 ScrollPane sp1 = new ScrollPane(DisplayBox);
86 sp1.setHbarPolicy(ScrollBarPolicy.NEVER);
87 sp1.setVbarPolicy(ScrollBarPolicy.ALWAYS);
88 VBox.setVgrow(sp1, Priority.ALWAYS);
89 DisplayBox.setPrefWidth(700);
90 DisplayBox.setPrefHeight(800);
91 DisplayBox.setStyle("-fx-background-color: #E0FFFF");
92 DisplayBox.setAlignment(Pos.TOP_CENTER);
93 DisplayLabel = new Label();
94 DisplayLabel.setText("Display Area");
95 DisplayLabel.setStyle("-fx-font: 24 arial;");
96 root.add(sp1 ,1 ,1);
97 DisplayBox.getChildren().add(DisplayLabel);
GUI.java
98
99 //TextBox
100 VBox TextBox = new VBox();
101 TextBox.setPrefWidth(700);
102 TextBox.setPrefHeight(100);
103 TextBox.setStyle("-fx-background-color: #E0FFFF");
104 TextBox.setAlignment(Pos.BOTTOM_RIGHT);
105 Text = new TextField();
106 Text.setText("TEXTBOX");
107 Text.setStyle("-fx-font: 24 arial;");
108 root.add(TextBox,1,2);
109 Text.setPrefWidth(700);
110 Text.setPrefHeight(100);
111 TextBox.getChildren().add(Text);
112
113 //FriendBox
114 FriendBox = new VBox();
115 ScrollPane sp2 = new ScrollPane(FriendBox);
116 sp2.setHbarPolicy(ScrollBarPolicy.NEVER);
117 sp2.setVbarPolicy(ScrollBarPolicy.ALWAYS);
118 VBox.setVgrow(sp2, Priority.ALWAYS);
119 FriendBox.setPrefWidth(200);
120 FriendBox.setPrefHeight(600);
121 FriendBox.setStyle("-fx-background-color: #ADD8E6");
122 FriendBox.setAlignment(Pos.TOP_CENTER);
123 Label FriendLabel = new Label();
124 FriendLabel.setText("Friend List");
125 FriendLabel.setStyle("-fx-font: 24 arial;");
126 root.add(sp2 ,0 ,1);
127 FriendBox.getChildren().add(FriendLabel);
128
129 //ButtonBox
130 VBox ButtonBox = new VBox();
131 ButtonBox.setStyle("-fx-background-color: #F08080");
132 ButtonBox.setAlignment(Pos.BOTTOM_RIGHT);
133 root.add(ButtonBox,0,2);
134
135 Button SendButton = new Button();
136 SendButton.setText("Send");
137 SendButton.setStyle("-fx-font: 24 arial;");
138
139 Button LoadButton = new Button();
140 LoadButton.setText("Load");
141 LoadButton.setStyle("-fx-font: 24 arial;");
142
143 SendButton.setPrefWidth(200);
144 SendButton.setPrefHeight(50);
145
146 LoadButton.setPrefWidth(200);
147 LoadButton.setPrefHeight(50);
148
149 ButtonBox.getChildren().add(SendButton);
150 ButtonBox.getChildren().add(LoadButton);
151
152 //PICTURE
153 Button PictureButton = new Button();
154 PictureButton.setText("Picture");
155 PictureButton.setStyle("-fx-font: 24 arial;");
156 PictureButton.setPrefWidth(200);
GUI.java
157 PictureButton.setPrefHeight(50);
158
159
160
161 //DOCUMENT
162 Button DocumentButton = new Button();
163 DocumentButton.setText("Document");
164 DocumentButton.setStyle("-fx-font: 24 arial;");
165 DocumentButton.setPrefWidth(200);
166 DocumentButton.setPrefHeight(50);
167
168 //FILE
169 Button FileButton = new Button();
170 FileButton.setText("File");
171 FileButton.setStyle("-fx-font: 24 arial;");
172 FileButton.setPrefWidth(200);
173 FileButton.setPrefHeight(50);
174
175
176
177
178
179 //SEND_BUTTON
180 SendButton.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>
()
181 {
182 public void handle(MouseEvent me) {
183
184 System.out.println("isPic: " + isPic);
185
186 if(isPic) {
187 Image i = new Image("file:"+Text.getText());
188 ImageView iv = new ImageView(i);
189 sentPicture(iv);
190 isPic = false;
191 GUI.sendPicture = true;
192 }
193 else {
194 sentMessage(Text.getText());
195 GUI.send = true;
196 }
197 }
198 });
199
200 //lOAD_BUTTON
201 LoadButton.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>
()
202 {public void handle(MouseEvent me) {
203
204 if(DisplayMode != "Load") {
205 //DisplayBox.getChildren().clear();
206 //DisplayBox.getChildren().add(DisplayLabel);
207 DisplayMode = "Load";
208 //DisplayLabel.setText(DisplayMode);
209 ButtonBox.getChildren().add(PictureButton);
210 ButtonBox.getChildren().add(DocumentButton);
211 ButtonBox.getChildren().add(FileButton);
212 }
213 else {
GUI.java
214 //DisplayBox.getChildren().clear();
215 //DisplayBox.getChildren().add(DisplayLabel);
216 DisplayMode = "Messaging";
217 //DisplayLabel.setText(DisplayMode);
218 ButtonBox.getChildren().clear();
219 ButtonBox.getChildren().add(SendButton);
220 ButtonBox.getChildren().add(LoadButton);
221 }
222 }});
223 //PICTURE_BUTTON
224 PictureButton.addEventHandler(MouseEvent.MOUSE_RELEASED, new
EventHandler<MouseEvent>()
225 {public void handle(MouseEvent me) {
226 //DisplayBox.getChildren().clear();
227 //DisplayBox.getChildren().add(DisplayLabel);
228
229 /*for(int i = 0; i < pictures.length; i++) {
230 Label PictureLabel = new Label();
231 PictureLabel.setText(pictures[i]);
232 PictureLabel.setStyle("-fx-font: 24 arial;");
233
234 DisplayBox.getChildren().add(addPic(pictures[i]));
235 DisplayBox.getChildren().add(PictureLabel);
236 }*/
237 //DisplayMode = "Picture";
238 //DisplayLabel.setText(DisplayMode);
239
240
241
242 FileChooser fc = new FileChooser();
243 ExtensionFilter filter = new ExtensionFilter("JPEG", "jpeg", "jpg", "png",
"bmp", "gif");
244 fc.setSelectedExtensionFilter(filter);
245 fc.setTitle("Select Picture");
246
247 File file = fc.showOpenDialog(primaryStage);
248 if (file != null) {
249
250 Text.setText(file.toString());
251 isPic = true;
252 // GUI.sendPicture = true;
253 }
254
255 ButtonBox.getChildren().clear();
256 ButtonBox.getChildren().add(SendButton);
257 ButtonBox.getChildren().add(LoadButton);
258 }});
259 //DOCUMENT_BUTTON
260 DocumentButton.addEventHandler(MouseEvent.MOUSE_RELEASED, new
EventHandler<MouseEvent>()
261 {public void handle(MouseEvent me) {
262 DisplayLabel.setText("Document");
263 }});
264 FileButton.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>
()
265 {public void handle(MouseEvent me) {
266 DisplayLabel.setText("File");
267 }});
268
GUI.java
269
270 primaryStage.setScene(scene);
271 primaryStage.show();
272 }
273 catch(Exception e) {
274 e.printStackTrace();
275 }
276 }
277
278 public synchronized void setLabel(String text) {
279 Platform.runLater(new Runnable() {
280
281 @Override
282 public void run() {
283 // TODO Auto-generated method stub
284 //DisplayLabel.setText(text);
285 Label dLabel = new Label();
286 dLabel.setText(text);
287 dLabel.setPrefWidth(DisplayBox.getWidth());
288 dLabel.setStyle("-fx-font: 24 arial; -fx-background-color: #ffd0cb;");
289 dLabel.setAlignment(Pos.CENTER);
290 DisplayBox.getChildren().add(dLabel);
291 }
292
293 });
294
295 }
296
297 public synchronized void addFriend(String username, int id) {
298 Platform.runLater(new Runnable() {
299
300 @Override
301 public void run() {
302 // TODO Auto-generated method stub
303 //DisplayLabel.setText(text);
304 Label dLabel = new Label();
305 dLabel.setText(username);
306 dLabel.setId(""+id);
307 dLabel.setPrefWidth(DisplayBox.getWidth());
308 dLabel.setStyle("-fx-font: 24 arial; -fx-background-color: green;");
309 dLabel.setAlignment(Pos.CENTER);
310 FriendBox.getChildren().add(dLabel);
311 }
312
313 });
314
315 }
316
317 public synchronized void removeFriend(int id) {
318 Platform.runLater(new Runnable() {
319
320 @Override
321 public void run() {
322 // TODO Auto-generated method stub
323 //DisplayLabel.setText(text);
324 ArrayList<Label> labels = new ArrayList<Label>();
325 for(Node l : FriendBox.getChildren())
326 if(l instanceof Label)
327 if(l.getId().equals(""+id))
GUI.java
328 labels.add((Label) l);
329 for(Label l1 : labels)
330 FriendBox.getChildren().remove(l1);
331 }
332
333 });
334
335 }
336
337 public synchronized void removeAllFriends() {
338 Platform.runLater(new Runnable() {
339
340 @Override
341 public void run() {
342 // TODO Auto-generated method stub
343 //DisplayLabel.setText(text);
344 ArrayList<Label> labels = new ArrayList<Label>();
345 for(Node l : FriendBox.getChildren())
346 if(l instanceof Label)
347 labels.add((Label) l);
348 for(Label l1 : labels)
349 FriendBox.getChildren().remove(l1);
350 }
351
352 });
353
354 }
355
356 public synchronized void sentMessage(String text) {
357 Platform.runLater(new Runnable() {
358
359 @Override
360 public void run() {
361 // TODO Auto-generated method stub
362 //DisplayLabel.setText(text);
363 Label dLabel = new Label();
364 dLabel.setText("Client " + client.getID() + ": " + text);
365 dLabel.setStyle("-fx-font: 24 arial; -fx-background-color: #caffc1;");
366 dLabel.setPrefWidth(DisplayBox.getWidth());
367 dLabel.setAlignment(Pos.CENTER);
368 DisplayBox.getChildren().add(dLabel);
369 }
370
371 });
372 }
373
374 public synchronized void addPicture(ImageView pic) {
375 Platform.runLater(new Runnable() {
376
377 @Override
378 public void run() {
379 System.out.println("ADD PICTURE");
380 // TODO Auto-generated method stub
381 //DisplayLabel.setText(text);
382 Label dLabel = new Label();
383 //dLabel.setText("Client " + client.getID() + ": ");
384 double ratio = pic.getImage().getHeight() / pic.getImage().getWidth();
385 pic.setFitWidth(DisplayBox.getWidth() - 300.0);
386 pic.setFitHeight((DisplayBox.getWidth() - 300.0) * ratio);
GUI.java
387 dLabel.setGraphic(pic);
388
389 dLabel.setStyle("-fx-font: 24 arial; -fx-background-color: #ffd0cb;");
390
391 dLabel.setAlignment(Pos.CENTER);
392 DisplayBox.getChildren().add(dLabel);
393 }
394
395 });
396 }
397
398 public synchronized void sentPicture(ImageView picture) {
399 Platform.runLater(new Runnable() {
400
401 @Override
402 public void run() {
403
404 System.out.println("SENT PICTURE");
405 // TODO Auto-generated method stub
406 //DisplayLabel.setText(text);
407 Label dLabel = new Label();
408 System.out.println("SENT PICTURE1");
409 //dLabel.setText("Client " + client.getID() + ": ");
410
411
412 System.out.println("SENT PICTURE2");
413 double ratio = picture.getImage().getHeight() / picture.getImage().getWidth();
414 picture.setFitWidth(DisplayBox.getWidth() - 300.0);
415 picture.setFitHeight((DisplayBox.getWidth() - 300.0) * ratio);
416
417 dLabel.setGraphic(picture);
418
419 dLabel.setStyle("-fx-font: 24 arial; -fx-background-color: #caffc1;");
420
421 dLabel.setAlignment(Pos.CENTER);
422 DisplayBox.getChildren().add(dLabel);
423 }
424
425 });
426 }
427
428 public String getText() {
429 return Text.getText();
430 }
431
432 public void clearText() {
433 Text.clear();
434 }
435
436 public static void main(String[] args) {
437
438 launch(args);
439
440
441 }
442
443 }

You might also like