- 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");
public Login() {
setupUI();
setUpListeners();
initComponents();
this.setSize(400, 150);
this.setModal(true);
}
public void setupUI() {
public void initComponents() {
this.setTitle("Login");
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(buttonPanel, BorderLayout.SOUTH);
}
private void setUpListeners() {
okButton.addActionListener((ActionEvent e) -> {
Login.this.setVisible(false);
});

View File

@ -6,9 +6,12 @@
package br.edu.ufsj.sms.GUI;
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.ScreenCastMessage;
import br.edu.ufsj.sms.net.Socket;
import java.awt.AWTException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -28,29 +31,30 @@ import javax.swing.JLabel;
*/
public class MainWindow extends javax.swing.JFrame {
/**
* Creates new form MainWindow
*/
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel status;
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 String INET_ADDR = "224.0.0.3";
public final static int TIME = 1000;
private String name;
private int port;
private ChatWindow chatWindow;
private float compressRatio;
public MainWindow(String name, int port) throws UnknownHostException {
this.name = name;
this.port = port;
this.compressRatio = 0.5f;
this.initComponents();
}
private void initComponents() throws UnknownHostException {
this.setSize(new java.awt.Dimension(1024, 768));
this.setJMenuBar(new MenuBar(this));
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setTitle("Share My Sheet");
@ -75,7 +79,7 @@ public class MainWindow extends javax.swing.JFrame {
this.chatWindow = new ChatWindow(this);
this.socket.start();
this.setSize(new java.awt.Dimension(1024, 768));
}
public void showChatWindow() {
@ -89,7 +93,7 @@ public class MainWindow extends javax.swing.JFrame {
public void shareMySheet(boolean status) {
if (status) {
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);
} else {
this.timer.cancel();
@ -101,6 +105,7 @@ public class MainWindow extends javax.swing.JFrame {
}
public void exit() {
this.chatWindow.setVisible(true);
this.setVisible(false);
this.dispose();
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) {
// Verificar se a aba existe, se existir, atualizo.
ByteArrayInputStream bais = new ByteArrayInputStream(message.getImageByte());
BufferedImage image;
try {
image = ImageIO.read(bais);
ImageIcon imageIcon = new ImageIcon(image);
JLabel myShoot = new JLabel(imageIcon);
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
String title = tabbedPane.getTitleAt(i);
if (title.equals(message.getName())) {
image = ImageIO.read(bais);
JLabel myShoot = new JLabel(new ImageIcon(image));
tabbedPane.setComponentAt(i, myShoot);
return;
}
}
image = ImageIO.read(bais);
JLabel myShoot = new JLabel(new ImageIcon(image));
tabbedPane.addTab(message.getName(), myShoot);
} catch (IOException 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>");
}
// 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
*/
@ -173,11 +179,4 @@ public class MainWindow extends javax.swing.JFrame {
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.JMenu viewMenu;
private MainWindow mainWindow;
private final MainWindow mainWindow;
public MenuBar(MainWindow mainWindow) {
this.mainWindow = mainWindow;
this.initComponents();
}
private void initComponents() {
fileMenu = new javax.swing.JMenu();
closeTabMenu = new javax.swing.JMenuItem();
exitMenu = new javax.swing.JMenuItem();
@ -40,64 +43,33 @@ public class MenuBar extends JMenuBar {
aboutMenu = new javax.swing.JMenuItem();
fileMenu.setText("File");
fileMenu.setToolTipText("");
closeTabMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
closeTabMenu.setText("Close Current Tab");
closeTabMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeTabMenuActionPerformed(evt);
}
});
fileMenu.add(closeTabMenu);
closeTabMenu.addActionListener(this::closeTabMenuActionPerformed);
exitMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
exitMenu.setText("Exit");
exitMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuActionPerformed(evt);
}
});
exitMenu.addActionListener(this::exitMenuActionPerformed);
fileMenu.add(closeTabMenu);
fileMenu.add(exitMenu);
this.add(fileMenu);
editMenu.setText("Edit");
shareMySheetMenu.setText("Share My Sheet!");
shareMySheetMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
shareMySheetMenuActionPerformed(evt);
}
});
shareMySheetMenu.addActionListener(this::shareMySheetMenuActionPerformed);
editMenu.add(shareMySheetMenu);
this.add(editMenu);
viewMenu.setText("View");
showChatWindowMenu.setText("Chat Window");
showChatWindowMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showChatWindowMenuActionPerformed(evt);
}
});
showChatWindowMenu.addActionListener(this::showChatWindowMenuActionPerformed);
viewMenu.add(showChatWindowMenu);
this.add(viewMenu);
helpMenu.setText("Help");
aboutMenu.setText("About");
aboutMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutMenuActionPerformed(evt);
}
});
aboutMenu.addActionListener(this::aboutMenuActionPerformed);
helpMenu.add(aboutMenu);
this.add(helpMenu);
}
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) {
mainWindow.shareMySheet(shareMySheetMenu.isSelected());
if (shareMySheetMenu.isSelected()) {
shareMySheetMenu.setText("Stop sharing it!");
} else {
shareMySheetMenu.setText("Share My Sheer!");
}
mainWindow.shareMySheet(shareMySheetMenu.isSelected());
}
private void closeTabMenuActionPerformed(java.awt.event.ActionEvent evt) {

View File

@ -5,14 +5,8 @@
*/
package br.edu.ufsj.sms.control;
import br.edu.ufsj.sms.net.ScreenCastMessage;
import br.edu.ufsj.sms.net.Socket;
import java.awt.AWTException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import br.edu.ufsj.sms.GUI.MainWindow;
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 {
private final ScreenShot ss;
private final String name;
private final Socket socket;
private float compressRatio;
private final MainWindow mainWindow;
public AlarmClock(String name, Socket socket) {
ss = new ScreenShot();
this.name = name;
this.socket = socket;
this.compressRatio = 0.5f;
public AlarmClock(MainWindow mainWindow) {
this.mainWindow = mainWindow;
}
@Override
public void run() {
byte[] msg = null;
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;
}
}
this.mainWindow.sendScreenCastMessage();
}
}

View File

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

View File

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

View File

@ -26,10 +26,11 @@ public class Socket extends Thread {
private MulticastSocket multicastSocket;
private InetAddress address;
private MainWindow main;
public final static String INET_ADDR = "224.0.0.3";
public Socket(MainWindow main) throws UnknownHostException {
this.main = main;
this.address = InetAddress.getByName(MainWindow.INET_ADDR);
this.address = InetAddress.getByName(Socket.INET_ADDR);
try {
multicastSocket = new MulticastSocket(this.main.getPort());
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 {
byte[] buf = new byte[256000];
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
public void run() {
try {