Tirei interações com o socket e regras de negócio da interface e juntei tudo numa classe controladora.

This commit is contained in:
bradoki 2026-07-10 12:59:11 -03:00
parent 8930ba9bf3
commit cea72533e2
9 changed files with 189 additions and 101 deletions

View File

@ -57,14 +57,6 @@ public class ChatWindow extends javax.swing.JDialog {
jPanel1.setLayout(new java.awt.BorderLayout());
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
@Override
public void keyTyped(java.awt.event.KeyEvent evt) {
if (evt.getKeyChar() == '\n') {
sendMessage();
}
}
});
jButton1.setText("Send");
jScrollPane1.setViewportView(jEditorPane1);
@ -73,9 +65,6 @@ public class ChatWindow extends javax.swing.JDialog {
jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
jButton1.addActionListener((java.awt.event.ActionEvent evt) -> {
sendMessage();
});
jPanel2.add(jButton1);
jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);
jSplitPane1.setRightComponent(jPanel1);
@ -83,15 +72,21 @@ public class ChatWindow extends javax.swing.JDialog {
pack();
}
private void sendMessage() {
if (jTextArea1.getText().trim().length() == 0) {
return;
}
mainWindow.sendChatMessage(jTextArea1.getText());
jTextArea1.setText("");
}
public void receiveChatMessage(String message) {
jEditorPane1.setText(message + jEditorPane1.getText());
}
public javax.swing.JButton getButton(){
return this.jButton1;
}
public javax.swing.JTextArea getJTextArea(){
return this.jTextArea1;
}
public String getText(){
String text = jTextArea1.getText();
jTextArea1.setText("");
return text;
}
}

View File

@ -5,6 +5,7 @@
*/
package br.edu.ufsj.sms.GUI;
import br.edu.ufsj.sms.GUI.MenuBar;
import br.edu.ufsj.sms.control.AlarmClock;
import br.edu.ufsj.sms.control.ImageTransformation;
import br.edu.ufsj.sms.control.ScreenShot;
@ -36,26 +37,20 @@ public class MainWindow extends javax.swing.JFrame {
private javax.swing.JPanel statusPanel;
private javax.swing.JTabbedPane tabbedPane;
private ChatWindow chatWindow;
private Socket socket;
private AlarmClock alarmClock;
private Timer timer;
public final static int TIME = 1000;
private MenuBar menuBar;
private String name;
private int port;
private float compressRatio;
public MainWindow(String name, int port) throws UnknownHostException {
public MainWindow(String name, int port) {
this.name = name;
this.port = port;
this.compressRatio = 0.5f;
this.initComponents();
}
private void initComponents() throws UnknownHostException {
this.setJMenuBar(new MenuBar(this));
private void initComponents() {
this.menuBar = new MenuBar(this);
this.setJMenuBar(this.menuBar);
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setTitle("Share My Sheet");
@ -74,14 +69,18 @@ public class MainWindow extends javax.swing.JFrame {
this.pack();
this.socket = new Socket(this);
this.timer = new Timer();
this.chatWindow = new ChatWindow(this);
this.socket.start();
this.setSize(new java.awt.Dimension(1024, 768));
}
public ChatWindow getChatWindow(){
return this.chatWindow;
}
public MenuBar getSMSMenuBar(){
return this.menuBar;
}
public void showChatWindow() {
this.chatWindow.setVisible(true);
}
@ -90,16 +89,6 @@ public class MainWindow extends javax.swing.JFrame {
}
public void shareMySheet(boolean status) {
if (status) {
this.timer = new Timer();
this.alarmClock = new AlarmClock(this);
this.timer.scheduleAtFixedRate(this.alarmClock, 0, MainWindow.TIME);
} else {
this.timer.cancel();
}
}
public void closeCurrentTab() {
tabbedPane.remove(tabbedPane.getSelectedIndex());
}
@ -110,37 +99,7 @@ public class MainWindow extends javax.swing.JFrame {
this.dispose();
System.exit(0);
}
public void sendChatMessage(String text) {
ChatMessage message = new ChatMessage(this.name, text);
try {
this.socket.send(message.toByteArray());
} catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void sendScreenCastMessage() {
byte[] msg = null;
try {
BufferedImage image = ScreenShot.takeAShot();
BufferedImage rescaled = ImageTransformation.resize(image, 1024, 768);
byte[] byteImage = ImageTransformation.compress(rescaled, compressRatio);
ScreenCastMessage message = new ScreenCastMessage(this.name, byteImage);
msg = message.toByteArray();
} catch (AWTException | IOException ex) {
Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex);
}
if (msg != null) {
try {
this.socket.send(msg);
} catch (IOException ex) {
Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex);
this.compressRatio -= 0.05f;
}
}
}
public void receiveScreenCastMessage(ScreenCastMessage message) {
ByteArrayInputStream bais = new ByteArrayInputStream(message.getImageByte());
BufferedImage image;

View File

@ -55,13 +55,11 @@ public class MenuBar extends JMenuBar {
editMenu.setText("Edit");
shareMySheetMenu.setText("Share My Sheet!");
shareMySheetMenu.addActionListener(this::shareMySheetMenuActionPerformed);
editMenu.add(shareMySheetMenu);
this.add(editMenu);
viewMenu.setText("View");
showChatWindowMenu.setText("Chat Window");
showChatWindowMenu.addActionListener(this::showChatWindowMenuActionPerformed);
viewMenu.add(showChatWindowMenu);
this.add(viewMenu);
@ -72,23 +70,18 @@ public class MenuBar extends JMenuBar {
this.add(helpMenu);
}
private void showChatWindowMenuActionPerformed(java.awt.event.ActionEvent evt) {
mainWindow.showChatWindow();
public javax.swing.JMenuItem getShowChatWindowMenu(){
return this.showChatWindowMenu;
}
public javax.swing.JCheckBoxMenuItem getShareMySheetMenu(){
return this.shareMySheetMenu;
}
private void aboutMenuActionPerformed(java.awt.event.ActionEvent evt) {
mainWindow.showAboutWindow();
}
private void shareMySheetMenuActionPerformed(java.awt.event.ActionEvent evt) {
mainWindow.shareMySheet(shareMySheetMenu.isSelected());
if (shareMySheetMenu.isSelected()) {
shareMySheetMenu.setText("Stop sharing it!");
} else {
shareMySheetMenu.setText("Share My Sheer!");
}
}
private void closeTabMenuActionPerformed(java.awt.event.ActionEvent evt) {
mainWindow.closeCurrentTab();
}

View File

@ -6,6 +6,8 @@
package br.edu.ufsj.sms;
import br.edu.ufsj.sms.GUI.Login;
import br.edu.ufsj.sms.net.Socket;
import br.edu.ufsj.sms.control.ShareMySheetCtr;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -43,6 +45,9 @@ public class Main {
}
//</editor-fold>
Socket socket = new Socket();
socket.run();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
@ -55,6 +60,7 @@ public class Main {
login.dispose();
br.edu.ufsj.sms.GUI.MainWindow main;
main = new br.edu.ufsj.sms.GUI.MainWindow(name, port);
ShareMySheetCtr control = new ShareMySheetCtr(name, port, socket, main);
main.setVisible(true);
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

View File

@ -14,14 +14,14 @@ import java.util.TimerTask;
*/
public class AlarmClock extends TimerTask {
private final MainWindow mainWindow;
private final ShareMySheetCtr controller;
public AlarmClock(MainWindow mainWindow) {
this.mainWindow = mainWindow;
public AlarmClock(ShareMySheetCtr controller) {
this.controller = controller;
}
@Override
public void run() {
this.mainWindow.sendScreenCastMessage();
this.controller.sendScreenCastMessage();
}
}

View File

@ -22,6 +22,8 @@ import javax.imageio.stream.ImageOutputStream;
*/
public class ImageTransformation {
public static float compressRatio = 0.5f;
public static byte[] toByteArray(BufferedImage image) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
@ -29,13 +31,13 @@ public class ImageTransformation {
return data;
}
public static byte[] compress(BufferedImage image, float scale) throws IOException {
public static byte[] compress(BufferedImage image) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(scale);
param.setCompressionQuality(compressRatio);
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), param);

View File

@ -0,0 +1,9 @@
package br.edu.ufsj.sms.control; // ou o pacote onde preferir organizar suas interfaces
public interface MessageSubscriber {
/**
* Método chamado pelo Socket sempre que um novo pacote for recebido e deserializado.
* * @param message O objeto recebido da rede (ex: ChatMessage, ScreenCastMessage)
*/
void onMessageReceived(Object message);
}

View File

@ -0,0 +1,120 @@
package br.edu.ufsj.sms.control;
import br.edu.ufsj.sms.net.Socket;
import br.edu.ufsj.sms.GUI.MainWindow;
import br.edu.ufsj.sms.control.AlarmClock;
import br.edu.ufsj.sms.control.ImageTransformation;
import br.edu.ufsj.sms.GUI.ChatWindow;
import br.edu.ufsj.sms.GUI.MenuBar;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.util.Timer;
import javax.swing.JMenuBar;
//import java.io.IOException;
public class ShareMySheetCtr implements MessageSubscriber {
private MainWindow window;
private Socket socket;
private String name;
private int port;
private Timer timer;
private AlarmClock alarmClock;
public final static int TIME = 1000;
public ShareMySheetCtr (String name, int port, Socket socket, MainWindow window){
this.name = name;
this.port = port;
this.socket = socket;
this.window = window;
ChatWindow chatWindow = window.getChatWindow();
MenuBar menuBar = window.getSMSMenuBar();
socket.subscribe(this);
chatWindow.getButton().addActionListener(e -> {
String msg = chatWindow.getText();
this.sendChatMessage(msg);
});
chatWindow.getJTextArea().addKeyListener(new java.awt.event.KeyAdapter() {
@Override
public void keyTyped(java.awt.event.KeyEvent evt) {
if (evt.getKeyChar() == '\n') {
String msg = chatWindow.getText();
sendChatMessage(msg);
evt.consume();
}
}
});
menuBar.getShowChatWindowMenu().addActionListener(e -> {
window.showChatWindow();
});
javax.swing.JCheckBoxMenuItem shareMySheetMenu;
shareMySheetMenu.addActionListener(e -> {
shareMySheet(shareMySheetMenu.isSelected());
if (shareMySheetMenu.isSelected()) {
shareMySheetMenu.setText("Stop sharing it!");
} else {
shareMySheetMenu.setText("Share My Sheer!");
}
});
}
public void onMessageReceived(Object message){
if (message instanceof ScreenCastMessage) {
this.window.receiveScreenCastMessage((ScreenCastMessage) message);
}
if (message instanceof ChatMessage) {
this.window.receiveChatMessage((ChatMessage) message);
}
}
public void sendChatMessage(String text) {
ChatMessage message = new ChatMessage(this.name, text);
try {
this.socket.send(message.toByteArray());
} catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void sendScreenCastMessage() {
byte[] msg = null;
try {
BufferedImage image = ScreenShot.takeAShot();
BufferedImage rescaled = ImageTransformation.resize(image, 1024, 768);
byte[] byteImage = ImageTransformation.compress(rescaled);
ScreenCastMessage message = new ScreenCastMessage(this.name, byteImage);
msg = message.toByteArray();
} catch (AWTException | IOException ex) {
Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex);
}
if (msg != null) {
try {
this.socket.send(msg);
} catch (IOException ex) {
Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex);
ImageTransformation.compressRatio -= 0.05f;
}
}
}
public void shareMySheet(boolean status) {
if (status) {
this.timer = new Timer();
this.alarmClock = new AlarmClock(this);
this.timer.scheduleAtFixedRate(this.alarmClock, 0, ShareMySheetCtr.TIME);
} else {
this.timer.cancel();
}
}
}

View File

@ -6,6 +6,7 @@
package br.edu.ufsj.sms.net;
import br.edu.ufsj.sms.GUI.MainWindow;
import br.edu.ufsj.sms.control.MessageSubscriber;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInput;
@ -16,6 +17,8 @@ import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.List;
import java.util.ArrayList;
/**
*
@ -27,9 +30,9 @@ public class Socket extends Thread {
private InetAddress address;
private MainWindow main;
public final static String INET_ADDR = "224.0.0.3";
private final List<MessageSubscriber> subscribers = new ArrayList<>();
public Socket(MainWindow main) throws UnknownHostException {
this.main = main;
public Socket() throws UnknownHostException {
this.address = InetAddress.getByName(Socket.INET_ADDR);
try {
multicastSocket = new MulticastSocket(this.main.getPort());
@ -59,15 +62,16 @@ public class Socket extends Thread {
ObjectInput in = new ObjectInputStream(bis);
Object message;
message = in.readObject();
if (message instanceof ScreenCastMessage) {
this.main.receiveScreenCastMessage((ScreenCastMessage) message);
}
if (message instanceof ChatMessage) {
this.main.receiveChatMessage((ChatMessage) message);
for (MessageSubscriber sub : subscribers) {
sub.onMessageReceived(message);
}
}
}
public void subscribe(MessageSubscriber sub) {
this.subscribers.add(sub);
}
@Override
public void run() {
try {