Browse Source

Began Multimedia part

cam
Cameron Weinfurt 2 years ago
parent
commit
bca68f1ad9
  1. 11
      window/ChatWindow.java
  2. 28
      window/WindowProtoHandler.java

11
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) {

28
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

Loading…
Cancel
Save