diff --git a/window/ChatWindow.java b/window/ChatWindow.java index f7c7d1d..f577146 100644 --- a/window/ChatWindow.java +++ b/window/ChatWindow.java @@ -11,6 +11,8 @@ import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; import javafx.scene.input.InputMethodEvent; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; @@ -69,6 +71,15 @@ public class ChatWindow extends Application { }); } + public void addImage(String user, Image image) { + addMessage(user, ""); + Platform.runLater(() -> { + ImageView imageView = new ImageView(); + imageView.setImage(image); + messagePane.getChildren().add(imageView); + }); + } + public void addMessage(String user, String message) { Platform.runLater(() -> addMessage(user, message, true)); } public void addMessage(String user, String message, boolean doTimestamp) { diff --git a/window/WindowProtoHandler.java b/window/WindowProtoHandler.java index 8140673..ed86e46 100644 --- a/window/WindowProtoHandler.java +++ b/window/WindowProtoHandler.java @@ -2,12 +2,10 @@ package window; import javafx.application.Platform; import javafx.scene.control.Alert; +import javafx.scene.image.Image; import proto.ProtoHandler; import java.io.*; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.security.AlgorithmConstraints; import java.util.ArrayList; public class WindowProtoHandler implements ProtoHandler { @@ -59,15 +57,23 @@ public class WindowProtoHandler implements ProtoHandler { @Override public void handleFile(String username, String filename, byte[] data) { - Platform.runLater(() -> { - try { - chatWindow.addMessage("Server", "User [" + username + "] has sent the file \"" + filename + "\"."); - FileOutputStream fileOutputStream = new FileOutputStream(filename); - fileOutputStream.write(data); - } catch (IOException e) { - e.printStackTrace(); - } + if (filename.endsWith(".png") || filename.endsWith(".jpg")) { + Platform.runLater(() -> { + Image image = new Image(new ByteArrayInputStream(data)); + chatWindow.addImage(username, image); + }); + } + else { + Platform.runLater(() -> { + try { + chatWindow.addMessage("Server", "User [" + username + "] has sent the file \"" + filename + "\"."); + FileOutputStream fileOutputStream = new FileOutputStream(filename); + fileOutputStream.write(data); + } catch (IOException e) { + e.printStackTrace(); + } }); + } } @Override