diff --git a/src/GUI/ChatScreen.form b/src/GUI/ChatScreen.form index 62e6e34eb874be74427ea0edf4f88a6c4a99be32..05bc77bc7c92c0705b176c7bbc1ab31ab2bf8e08 100644 --- a/src/GUI/ChatScreen.form +++ b/src/GUI/ChatScreen.form @@ -35,7 +35,7 @@ </hspacer> </children> </grid> - <grid id="b37e3" binding="x" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="b37e3" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> @@ -107,10 +107,10 @@ </component> </children> </grid> - <grid id="be22c" binding="ryanPanel" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="c0bac" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> - <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + <grid row="0" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> </constraints> <properties/> <border type="none"/> diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java index 9449615087d77f8d09f7bf7527073f37aa8248eb..1deb3f47c46a0b8727edbd500d988532358b56ae 100644 --- a/src/GUI/ChatScreen.java +++ b/src/GUI/ChatScreen.java @@ -1,12 +1,25 @@ package GUI; +import client.Client; +import remote.IChatController; +import remote.IClientController; + import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.rmi.RemoteException; public class ChatScreen { public JPanel panel2; private JButton quitButton; private JButton send; + + public JTextArea getChatOutputArea() + { + return chatOutputArea; + } + private JTextArea chatOutputArea; private JTextArea chatInputArea; private JTextArea userListArea; @@ -14,16 +27,65 @@ public class ChatScreen { private JPanel x; private JFrame frame; - public ChatScreen(){ + private Client client; + + public void setChatOutput(String message) + { + this.chatOutputArea.setText(message); + } + + ActionListener actionListener = new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + if (e.getSource() == send) + { + String message = chatInputArea.getText(); + chatInputArea.setText(""); + IChatController chatController = client.getChatController(); + try + { + System.out.println("Send button pressed"); + chatController.broadcastMessage(client.getUserName(), message); + } + catch (RemoteException ex) + { + ex.printStackTrace(); + } + } + else if (e.getSource() == quitButton) + { + IClientController clientController = client.getClientController(); + try + { + System.out.println("Quit button pressed"); + clientController.quit(client.getUserName()); + System.exit(0); + } + catch (RemoteException ex) + { + ex.printStackTrace(); + } + } + } + + }; + + public ChatScreen(Client client) + { + this.client = client; + quitButton.addActionListener(actionListener); + send.addActionListener(actionListener); frame = new JFrame("App"); frame.setContentPane(panel2); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); createUIComponents(); - } + + private void createUIComponents() { ryanPanel = new PaintGUI().getGlobal(); diff --git a/src/GUI/PaintGUI.java b/src/GUI/PaintGUI.java index f63f737bb61ae12d6cd36dd5ec5b50f551e04b3f..74d68ce593ed415444844dbbf67269dc2b951cd1 100644 --- a/src/GUI/PaintGUI.java +++ b/src/GUI/PaintGUI.java @@ -1,5 +1,7 @@ package GUI; +import client.DrawingArea; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; diff --git a/src/GUI/StartScreen.java b/src/GUI/StartScreen.java index 8c64799bbb8150d20f6d9e5cf24dd8b362554c28..6fdb1bdad02e8540b44c4cafefbd81f36830a104 100644 --- a/src/GUI/StartScreen.java +++ b/src/GUI/StartScreen.java @@ -40,14 +40,13 @@ public class StartScreen { { frame.setVisible(false); frame.dispose(); - client.run(); + //client.doSomething(); + new ChatScreen(client); } else { showErrorMessage("Could not connect to server..."); } - - //new ChatScreen(); } } diff --git a/src/client/ChatUpdate.java b/src/client/ChatUpdate.java index 93cce44f87ee930cab1fab6d47f2e560d65d03d8..72682a9249b2ceae23b1a156d620252df2fdd778 100644 --- a/src/client/ChatUpdate.java +++ b/src/client/ChatUpdate.java @@ -8,14 +8,21 @@ import java.rmi.server.UnicastRemoteObject; public class ChatUpdate extends UnicastRemoteObject implements IChatUpdate, Serializable { - public ChatUpdate() throws RemoteException + private Client client; + + public ChatUpdate(Client client) throws RemoteException { super(); + this.client = client; } @Override public boolean notifyChat(String fromClient, String message) throws RemoteException { +// client.getChatScreen().setChatOutput(message); + client.getChatScreen().getChatOutputArea().setText(message); + + //client.setReceivedMessage(message); System.out.println(fromClient + ": " + message); return true; } diff --git a/src/client/Client.java b/src/client/Client.java index 4601865c0c8d078ecb6bd7a3acb8d17f160a3fe3..6a1845d0631815a7f46c925bda139d1f841f03a8 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -1,5 +1,6 @@ package client; +import GUI.ChatScreen; import remote.IChatController; import remote.IClientController; import client.ChatUpdate; @@ -25,11 +26,37 @@ public class Client private StartScreen startScreen; + public ChatScreen getChatScreen() + { + return chatScreen; + } + + private ChatScreen chatScreen; + + + + private String receivedMessage; + + public IChatController getChatController() + { + return chatController; + } + + public IClientController getClientController() + { + return clientController; + } + public void setUsername(String userName) { this.userName = userName; } + public String getUserName() + { + return this.userName; + } + public void setServerAddress(String serverAddress) { this.serverAddress = serverAddress; @@ -38,7 +65,7 @@ public class Client public Client() throws RemoteException { this.userName = "DefaultUser"; - this.chatUpdate = new ChatUpdate(); + this.chatUpdate = new ChatUpdate(this); this.startScreen = new StartScreen(this); this.drawingUpdate = new DrawingUpdate(); } @@ -56,12 +83,13 @@ public class Client } } - public void run() + public void doSomething() { - //connect(); +// new ChatScreen(); try { + new ChatScreen(this); System.out.println("Sleeping..."); TimeUnit.MINUTES.sleep(5); } @@ -95,7 +123,13 @@ public class Client return false; } + public String getReceivedMessage() + { + return receivedMessage; + } - - + public void setReceivedMessage(String receivedMessage) + { + this.receivedMessage = receivedMessage; + } } \ No newline at end of file diff --git a/src/client/Client1.java b/src/client/Client1.java deleted file mode 100644 index 18d3b752c54b0006fae74cd71db0722b8018cc0f..0000000000000000000000000000000000000000 --- a/src/client/Client1.java +++ /dev/null @@ -1,73 +0,0 @@ -package client; - -import remote.IChatController; -import remote.IClientController; - -import java.rmi.NotBoundException; -import java.rmi.RemoteException; -import java.rmi.registry.LocateRegistry; -import java.rmi.registry.Registry; -import java.util.concurrent.TimeUnit; - -public class Client1 -{ - private String userName; - private Registry registryServer; - private IChatController chatController; - private IClientController clientController; - private ChatUpdate chatUpdate; - - public Client1(String userName) throws RemoteException - { - this.userName = userName; - this.chatUpdate = new ChatUpdate(); - } - - public static void main(String[] args) - { - try - { - Client1 client1 = new Client1("Hai"); - client1.run(); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - public void run() throws RemoteException, NotBoundException - { - connect(); - - try - { - TimeUnit.MINUTES.sleep(5); - } - catch(Exception e) - { - e.printStackTrace(); - } - } - - public boolean connect() throws RemoteException, NotBoundException - { - registryServer = LocateRegistry.getRegistry("localhost"); - - chatController = (IChatController) registryServer.lookup("ChatController"); - clientController = (IClientController) registryServer.lookup("ClientController"); - - System.out.println(userName + " fetched all controller from RMI registry"); - - if ( clientController.join(userName, this.chatUpdate ) ) - { - System.out.println(userName + " registered at server"); - - return true; - } - - return false; - } - - -} \ No newline at end of file diff --git a/src/client/Client2.java b/src/client/Client2.java deleted file mode 100644 index 3eda56bbcb3ba7adc889775fffdd352da89fe084..0000000000000000000000000000000000000000 --- a/src/client/Client2.java +++ /dev/null @@ -1,76 +0,0 @@ -package client; - -import remote.IChatController; -import remote.IClientController; -import client.ChatUpdate; - -import java.rmi.NotBoundException; -import java.rmi.RemoteException; -import java.rmi.registry.LocateRegistry; -import java.rmi.registry.Registry; -import java.util.concurrent.TimeUnit; - -public class Client2 -{ - private String userName; - private Registry registryServer; - private IChatController chatController; - private IClientController clientController; - private ChatUpdate chatUpdate; - - public Client2(String userName) throws RemoteException - { - this.userName = userName; - this.chatUpdate = new ChatUpdate(); - } - - public static void main(String[] args) - { - try - { - Client2 client1 = new Client2("Max"); - client1.run(); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - public void run() throws RemoteException, NotBoundException - { - connect(); - - chatController.broadcast(userName, "Hello"); - - try - { - TimeUnit.MINUTES.sleep(5); - } - catch(Exception e) - { - e.printStackTrace(); - } - } - - public boolean connect() throws RemoteException, NotBoundException - { - registryServer = LocateRegistry.getRegistry("localhost"); - - chatController = (IChatController) registryServer.lookup("ChatController"); - clientController = (IClientController) registryServer.lookup("ClientController"); - - System.out.println(userName + " fetched all controller from RMI registry"); - - if ( clientController.join(userName, this.chatUpdate ) ) - { - System.out.println(userName + " registered at server"); - - return true; - } - - return false; - } - - -} \ No newline at end of file diff --git a/src/client/PaintGUI.java b/src/client/PaintGUI.java deleted file mode 100644 index 02ea1bce4399175d6903809aea3653751cd2046e..0000000000000000000000000000000000000000 --- a/src/client/PaintGUI.java +++ /dev/null @@ -1,219 +0,0 @@ -package client; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.File; - -public class PaintGUI { - - - String[] shapes = {"Freehand", "Line", "Circle", "Rectangle", "Oval", "Eraser"}; - String[] colors = {"Aqua", "Black", "Blue", "Fuchsia", "Gray", "Green", "Lime", "Maroon", "Navy", "Olive", "Purple", "Red", "Silver", "Teal", "White", "Yellow"}; - JFrame frame; - JButton clearBtn, newBtn, openBtn, saveBtn, saveAsBtn, closeBtn; - JComboBox colorOptions; - JComboBox shapeOptions; - - DrawingArea drawingArea; - - JFileChooser fileChooser= new JFileChooser(); - JPanel toolbox = new JPanel(); - JPanel fileControl = new JPanel(); - ActionListener actionListener = new ActionListener() { - - public void actionPerformed(ActionEvent e) { - -/// Clear button /// - if (e.getSource() == clearBtn) { - drawingArea.clear(); - -/// Create new canvas /// - } else if (e.getSource() == newBtn) { - int returnVal = JOptionPane.showConfirmDialog(new JFrame(), "Save your current whiteboard?", "Save or Discard", JOptionPane.YES_NO_CANCEL_OPTION); - if (returnVal == JOptionPane.YES_OPTION) { - - drawingArea.saveFile(); - drawingArea.clear(); - - } else if (returnVal == JOptionPane.NO_OPTION) { - - drawingArea.clear(); - - } - -// Open file (PNG only) /// - } else if (e.getSource() == openBtn) { - int returnVal = fileChooser.showOpenDialog(frame); - if (returnVal == JFileChooser.APPROVE_OPTION) { - File file = fileChooser.getSelectedFile(); - drawingArea.openFile(file); - } - -/// Save under project directory without filename (PNG file with default filename) /// - } else if (e.getSource() == saveBtn) { - drawingArea.saveFile(); - -/// Save with other filename (PNG only) /// - } else if (e.getSource() == saveAsBtn) { - fileChooser = new JFileChooser(); - int returnVal = fileChooser.showSaveDialog(frame); - if (returnVal == JFileChooser.APPROVE_OPTION) { - File file = fileChooser.getSelectedFile(); - drawingArea.saveAsFile(file); - } - -/// Close application /// - } else if (e.getSource() == closeBtn) { - System.exit(0); - -/// Choose drawing color /// - } else if (e.getSource() == colorOptions) { - - String colorChosen = (String) colorOptions.getSelectedItem(); - - switch (colorChosen){ - case "Aqua": - drawingArea.setColorAqua(); - break; - case "Black": - drawingArea.setColorBlack(); - break; - case "Blue": - drawingArea.setColorBlue(); - break; - case "Fuchsia": - drawingArea.setColorFuchsia(); - break; - case "Gray": - drawingArea.setColorGray(); - break; - case "Green": - drawingArea.setColorGreen(); - break; - case "Lime": - drawingArea.setColorLime(); - break; - case "Maroon": - drawingArea.setColorMaroon(); - break; - case "Navy": - drawingArea.setColorNavy(); - break; - case "Olive": - drawingArea.setColorOlive(); - break; - case "Purple": - drawingArea.setColorPurple(); - break; - case "Red": - drawingArea.setColorRed(); - break; - case "Silver": - drawingArea.setColorSilver(); - break; - case "Teal": - drawingArea.setColorTeal(); - break; - case "White": - drawingArea.setColorWhite(); - break; - case "Yellow": - drawingArea.setColorYellow(); - break; - } - -/// Choose drawing tool /// - } else if (e.getSource() == shapeOptions) { - - String shapeChosen = (String) shapeOptions.getSelectedItem(); - - switch (shapeChosen){ - case "Freehand": - drawingArea.setModeFreehand(); - break; - case "Line": - drawingArea.setModeLine(); - break; - case "Circle": - drawingArea.setModeCircle(); - break; - case "Rectangle": - drawingArea.setModeRectangle(); - break; - case "Oval": - drawingArea.setModeOval(); - break; - case "Eraser": - drawingArea.setModeErase(); - break; - } - } - } - }; - -/// Main program /// - public static void main(String[] args) { - new PaintGUI().createAndShowGUI(); - } - - -/// GUI setup /// - private void createAndShowGUI() { - -/// Main drawing area /// - drawingArea = new DrawingArea(); - -/// Set up main frame and container /// - frame = new JFrame("Shared Whiteboard System"); - JFrame.setDefaultLookAndFeelDecorated(true); - Container content = frame.getContentPane(); - content.setLayout(new BorderLayout()); - -/// Set up elements /// - shapeOptions = new JComboBox(shapes); - shapeOptions.addActionListener(actionListener); - colorOptions = new JComboBox(colors); - colorOptions.setSelectedItem("Black"); - colorOptions.addActionListener(actionListener); - clearBtn = new JButton("Clear"); - clearBtn.addActionListener(actionListener); - newBtn = new JButton("New"); - newBtn.addActionListener(actionListener); - openBtn = new JButton("Open"); - openBtn.addActionListener(actionListener); - saveBtn = new JButton("Save"); - saveBtn.addActionListener(actionListener); - saveAsBtn = new JButton("Save As"); - saveAsBtn.addActionListener(actionListener); - closeBtn = new JButton("Close"); - closeBtn.addActionListener(actionListener); - -/// Toolbox panel /// - toolbox.add(colorOptions); - toolbox.add(shapeOptions); - toolbox.add(clearBtn); - -/// File control panel /// - fileControl.add(newBtn); - fileControl.add(openBtn); - fileControl.add(saveBtn); - fileControl.add(saveAsBtn); - fileControl.add(closeBtn); - -/// Layout /// - content.add(fileControl, BorderLayout.NORTH); - content.add(drawingArea); - content.add(toolbox, BorderLayout.SOUTH); - -/// Miscellaneous /// - frame.setSize(800, 600); - frame.setLocationRelativeTo( null ); - frame.setResizable(false); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setVisible(true); - - } - -} \ No newline at end of file