- Main methods moved to MainWindow class (still not good but much better than before)

- Local variable socket IP address moved to socket class
- AlarmClock now is just a scheduler
- Still working on message too long error
This commit is contained in:
Flávio Luiz Schiavoni 2017-01-23 16:00:12 -02:00
parent 5d5d87de77
commit 42ba2744cb
7 changed files with 68 additions and 139 deletions

View File

@ -35,14 +35,12 @@ public final class Login extends JDialog {
JButton cancelButton = new JButton("Cancel"); JButton cancelButton = new JButton("Cancel");
public Login() { public Login() {
setupUI(); initComponents();
setUpListeners();
this.setSize(400, 150); this.setSize(400, 150);
this.setModal(true); this.setModal(true);
} }
public void setupUI() { public void initComponents() {
this.setTitle("Login"); this.setTitle("Login");
portField.setModel(new javax.swing.SpinnerNumberModel(8000, 8000, 9000, 1)); portField.setModel(new javax.swing.SpinnerNumberModel(8000, 8000, 9000, 1));
@ -83,9 +81,7 @@ public final class Login extends JDialog {
this.add(topPanel); this.add(topPanel);
this.add(buttonPanel, BorderLayout.SOUTH); this.add(buttonPanel, BorderLayout.SOUTH);
}
private void setUpListeners() {
okButton.addActionListener((ActionEvent e) -> { okButton.addActionListener((ActionEvent e) -> {
Login.this.setVisible(false); Login.this.setVisible(false);
}); });

View File

@ -6,9 +6,12 @@
package br.edu.ufsj.sms.GUI; package br.edu.ufsj.sms.GUI;
import br.edu.ufsj.sms.control.AlarmClock; import br.edu.ufsj.sms.control.AlarmClock;
import br.edu.ufsj.sms.control.ImageTransformation;
import br.edu.ufsj.sms.control.ScreenShot;
import br.edu.ufsj.sms.net.ChatMessage; import br.edu.ufsj.sms.net.ChatMessage;
import br.edu.ufsj.sms.net.ScreenCastMessage; import br.edu.ufsj.sms.net.ScreenCastMessage;
import br.edu.ufsj.sms.net.Socket; import br.edu.ufsj.sms.net.Socket;
import java.awt.AWTException;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
@ -28,29 +31,30 @@ import javax.swing.JLabel;
*/ */
public class MainWindow extends javax.swing.JFrame { public class MainWindow extends javax.swing.JFrame {
/** private javax.swing.JScrollPane jScrollPane1;
* Creates new form MainWindow private javax.swing.JLabel status;
*/ private javax.swing.JPanel statusPanel;
private javax.swing.JTabbedPane tabbedPane;
private ChatWindow chatWindow;
private Socket socket; private Socket socket;
private AlarmClock alarmClock; private AlarmClock alarmClock;
private Timer timer; private Timer timer;
public final static String INET_ADDR = "224.0.0.3";
public final static int TIME = 1000; public final static int TIME = 1000;
private String name; private String name;
private int port; private int port;
private ChatWindow chatWindow; private float compressRatio;
public MainWindow(String name, int port) throws UnknownHostException { public MainWindow(String name, int port) throws UnknownHostException {
this.name = name; this.name = name;
this.port = port; this.port = port;
this.compressRatio = 0.5f;
this.initComponents(); this.initComponents();
} }
private void initComponents() throws UnknownHostException { private void initComponents() throws UnknownHostException {
this.setSize(new java.awt.Dimension(1024, 768));
this.setJMenuBar(new MenuBar(this)); this.setJMenuBar(new MenuBar(this));
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setTitle("Share My Sheet"); this.setTitle("Share My Sheet");
@ -75,7 +79,7 @@ public class MainWindow extends javax.swing.JFrame {
this.chatWindow = new ChatWindow(this); this.chatWindow = new ChatWindow(this);
this.socket.start(); this.socket.start();
this.setSize(new java.awt.Dimension(1024, 768));
} }
public void showChatWindow() { public void showChatWindow() {
@ -89,7 +93,7 @@ public class MainWindow extends javax.swing.JFrame {
public void shareMySheet(boolean status) { public void shareMySheet(boolean status) {
if (status) { if (status) {
this.timer = new Timer(); this.timer = new Timer();
this.alarmClock = new AlarmClock(this.name, this.socket); this.alarmClock = new AlarmClock(this);
this.timer.scheduleAtFixedRate(this.alarmClock, 0, MainWindow.TIME); this.timer.scheduleAtFixedRate(this.alarmClock, 0, MainWindow.TIME);
} else { } else {
this.timer.cancel(); this.timer.cancel();
@ -101,6 +105,7 @@ public class MainWindow extends javax.swing.JFrame {
} }
public void exit() { public void exit() {
this.chatWindow.setVisible(true);
this.setVisible(false); this.setVisible(false);
this.dispose(); this.dispose();
System.exit(0); System.exit(0);
@ -115,22 +120,44 @@ public class MainWindow extends javax.swing.JFrame {
} }
} }
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) { public void receiveScreenCastMessage(ScreenCastMessage message) {
// Verificar se a aba existe, se existir, atualizo.
ByteArrayInputStream bais = new ByteArrayInputStream(message.getImageByte()); ByteArrayInputStream bais = new ByteArrayInputStream(message.getImageByte());
BufferedImage image; BufferedImage image;
try { try {
image = ImageIO.read(bais);
ImageIcon imageIcon = new ImageIcon(image);
JLabel myShoot = new JLabel(imageIcon);
for (int i = 0; i < tabbedPane.getTabCount(); i++) { for (int i = 0; i < tabbedPane.getTabCount(); i++) {
String title = tabbedPane.getTitleAt(i); String title = tabbedPane.getTitleAt(i);
if (title.equals(message.getName())) { if (title.equals(message.getName())) {
image = ImageIO.read(bais);
JLabel myShoot = new JLabel(new ImageIcon(image));
tabbedPane.setComponentAt(i, myShoot); tabbedPane.setComponentAt(i, myShoot);
return; return;
} }
} }
image = ImageIO.read(bais);
JLabel myShoot = new JLabel(new ImageIcon(image));
tabbedPane.addTab(message.getName(), myShoot); tabbedPane.addTab(message.getName(), myShoot);
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
@ -145,27 +172,6 @@ public class MainWindow extends javax.swing.JFrame {
+ message.getText() + "<br>"); + message.getText() + "<br>");
} }
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel status;
private javax.swing.JPanel statusPanel;
private javax.swing.JTabbedPane tabbedPane;
// End of variables declaration//GEN-END:variables
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/** /**
* @return the port * @return the port
*/ */
@ -173,11 +179,4 @@ public class MainWindow extends javax.swing.JFrame {
return port; return port;
} }
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
}
} }

View File

@ -24,11 +24,14 @@ public class MenuBar extends JMenuBar {
private javax.swing.JMenuItem showChatWindowMenu; private javax.swing.JMenuItem showChatWindowMenu;
private javax.swing.JMenu viewMenu; private javax.swing.JMenu viewMenu;
private MainWindow mainWindow; private final MainWindow mainWindow;
public MenuBar(MainWindow mainWindow) { public MenuBar(MainWindow mainWindow) {
this.mainWindow = mainWindow; this.mainWindow = mainWindow;
this.initComponents();
}
private void initComponents() {
fileMenu = new javax.swing.JMenu(); fileMenu = new javax.swing.JMenu();
closeTabMenu = new javax.swing.JMenuItem(); closeTabMenu = new javax.swing.JMenuItem();
exitMenu = new javax.swing.JMenuItem(); exitMenu = new javax.swing.JMenuItem();
@ -40,64 +43,33 @@ public class MenuBar extends JMenuBar {
aboutMenu = new javax.swing.JMenuItem(); aboutMenu = new javax.swing.JMenuItem();
fileMenu.setText("File"); fileMenu.setText("File");
fileMenu.setToolTipText("");
closeTabMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK)); closeTabMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
closeTabMenu.setText("Close Current Tab"); closeTabMenu.setText("Close Current Tab");
closeTabMenu.addActionListener(new java.awt.event.ActionListener() { closeTabMenu.addActionListener(this::closeTabMenuActionPerformed);
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeTabMenuActionPerformed(evt);
}
});
fileMenu.add(closeTabMenu);
exitMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK)); exitMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
exitMenu.setText("Exit"); exitMenu.setText("Exit");
exitMenu.addActionListener(new java.awt.event.ActionListener() { exitMenu.addActionListener(this::exitMenuActionPerformed);
public void actionPerformed(java.awt.event.ActionEvent evt) { fileMenu.add(closeTabMenu);
exitMenuActionPerformed(evt);
}
});
fileMenu.add(exitMenu); fileMenu.add(exitMenu);
this.add(fileMenu); this.add(fileMenu);
editMenu.setText("Edit"); editMenu.setText("Edit");
shareMySheetMenu.setText("Share My Sheet!"); shareMySheetMenu.setText("Share My Sheet!");
shareMySheetMenu.addActionListener(new java.awt.event.ActionListener() { shareMySheetMenu.addActionListener(this::shareMySheetMenuActionPerformed);
public void actionPerformed(java.awt.event.ActionEvent evt) {
shareMySheetMenuActionPerformed(evt);
}
});
editMenu.add(shareMySheetMenu); editMenu.add(shareMySheetMenu);
this.add(editMenu); this.add(editMenu);
viewMenu.setText("View"); viewMenu.setText("View");
showChatWindowMenu.setText("Chat Window"); showChatWindowMenu.setText("Chat Window");
showChatWindowMenu.addActionListener(new java.awt.event.ActionListener() { showChatWindowMenu.addActionListener(this::showChatWindowMenuActionPerformed);
public void actionPerformed(java.awt.event.ActionEvent evt) {
showChatWindowMenuActionPerformed(evt);
}
});
viewMenu.add(showChatWindowMenu); viewMenu.add(showChatWindowMenu);
this.add(viewMenu); this.add(viewMenu);
helpMenu.setText("Help"); helpMenu.setText("Help");
aboutMenu.setText("About"); aboutMenu.setText("About");
aboutMenu.addActionListener(new java.awt.event.ActionListener() { aboutMenu.addActionListener(this::aboutMenuActionPerformed);
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutMenuActionPerformed(evt);
}
});
helpMenu.add(aboutMenu); helpMenu.add(aboutMenu);
this.add(helpMenu); this.add(helpMenu);
} }
private void showChatWindowMenuActionPerformed(java.awt.event.ActionEvent evt) { private void showChatWindowMenuActionPerformed(java.awt.event.ActionEvent evt) {
@ -109,13 +81,12 @@ public class MenuBar extends JMenuBar {
} }
private void shareMySheetMenuActionPerformed(java.awt.event.ActionEvent evt) { private void shareMySheetMenuActionPerformed(java.awt.event.ActionEvent evt) {
mainWindow.shareMySheet(shareMySheetMenu.isSelected());
if (shareMySheetMenu.isSelected()) { if (shareMySheetMenu.isSelected()) {
shareMySheetMenu.setText("Stop sharing it!"); shareMySheetMenu.setText("Stop sharing it!");
} else { } else {
shareMySheetMenu.setText("Share My Sheer!"); shareMySheetMenu.setText("Share My Sheer!");
} }
mainWindow.shareMySheet(shareMySheetMenu.isSelected());
} }
private void closeTabMenuActionPerformed(java.awt.event.ActionEvent evt) { private void closeTabMenuActionPerformed(java.awt.event.ActionEvent evt) {

View File

@ -5,14 +5,8 @@
*/ */
package br.edu.ufsj.sms.control; package br.edu.ufsj.sms.control;
import br.edu.ufsj.sms.net.ScreenCastMessage; import br.edu.ufsj.sms.GUI.MainWindow;
import br.edu.ufsj.sms.net.Socket;
import java.awt.AWTException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* *
@ -20,45 +14,14 @@ import java.util.logging.Logger;
*/ */
public class AlarmClock extends TimerTask { public class AlarmClock extends TimerTask {
private final ScreenShot ss; private final MainWindow mainWindow;
private final String name;
private final Socket socket;
private float compressRatio;
public AlarmClock(String name, Socket socket) { public AlarmClock(MainWindow mainWindow) {
ss = new ScreenShot(); this.mainWindow = mainWindow;
this.name = name;
this.socket = socket;
this.compressRatio = 0.5f;
} }
@Override @Override
public void run() { public void run() {
byte[] msg = null; this.mainWindow.sendScreenCastMessage();
try {
BufferedImage image = ss.takeAShot();
BufferedImage rescaled = ImageTransformation.resize(image, 1024, 768);
int size = ImageTransformation.toByteArray(rescaled).length / 65000;
size++;
int number_of_cuts = rescaled.getWidth() / size;
byte[] byteImage = ImageTransformation.compress(rescaled, compressRatio);
for (int i = 0 ; i < size ; i++){
}
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;
}
}
} }
} }

View File

@ -18,7 +18,7 @@ import java.io.IOException;
*/ */
public class ScreenShot { public class ScreenShot {
public BufferedImage takeAShot() throws AWTException, IOException { public static BufferedImage takeAShot() throws AWTException, IOException {
Robot robot = new Robot(); Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage output = robot.createScreenCapture(screenRect); BufferedImage output = robot.createScreenCapture(screenRect);

View File

@ -12,7 +12,6 @@ package br.edu.ufsj.sms.net;
public class ScreenCastMessage extends Message { public class ScreenCastMessage extends Message {
private String name; private String name;
private int y;
private byte[] imageByte; private byte[] imageByte;
public ScreenCastMessage() { public ScreenCastMessage() {

View File

@ -26,10 +26,11 @@ public class Socket extends Thread {
private MulticastSocket multicastSocket; private MulticastSocket multicastSocket;
private InetAddress address; private InetAddress address;
private MainWindow main; private MainWindow main;
public final static String INET_ADDR = "224.0.0.3";
public Socket(MainWindow main) throws UnknownHostException { public Socket(MainWindow main) throws UnknownHostException {
this.main = main; this.main = main;
this.address = InetAddress.getByName(MainWindow.INET_ADDR); this.address = InetAddress.getByName(Socket.INET_ADDR);
try { try {
multicastSocket = new MulticastSocket(this.main.getPort()); multicastSocket = new MulticastSocket(this.main.getPort());
multicastSocket.setSendBufferSize(256000); multicastSocket.setSendBufferSize(256000);
@ -42,6 +43,12 @@ public class Socket extends Thread {
} }
} }
public void send(byte[] msg) throws IOException {
DatagramPacket msgPacket;
msgPacket = new DatagramPacket(msg, msg.length, this.address, this.main.getPort());
multicastSocket.send(msgPacket);
}
public void receive() throws UnknownHostException, IOException, ClassNotFoundException { public void receive() throws UnknownHostException, IOException, ClassNotFoundException {
byte[] buf = new byte[256000]; byte[] buf = new byte[256000];
while (true) { while (true) {
@ -61,12 +68,6 @@ public class Socket extends Thread {
} }
} }
public void send(byte[] msg) throws IOException {
DatagramPacket msgPacket;
msgPacket = new DatagramPacket(msg, msg.length, this.address, this.main.getPort());
multicastSocket.send(msgPacket);
}
@Override @Override
public void run() { public void run() {
try { try {