Skip to content
Snippets Groups Projects
Commit eddb94d3 authored by Maxim Priymak's avatar Maxim Priymak
Browse files

Merge branch 'mpriymak' into 'master'

parents 712a37e3 9ad8f535
Branches
Tags
No related merge requests found
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
</hspacer> </hspacer>
</children> </children>
</grid> </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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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"/> <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 @@ ...@@ -107,10 +107,10 @@
</component> </component>
</children> </children>
</grid> </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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
......
package GUI; package GUI;
import client.Client;
import remote.IChatController;
import remote.IClientController;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
public class ChatScreen { public class ChatScreen {
public JPanel panel2; public JPanel panel2;
private JButton quitButton; private JButton quitButton;
private JButton send; private JButton send;
public JTextArea getChatOutputArea()
{
return chatOutputArea;
}
private JTextArea chatOutputArea; private JTextArea chatOutputArea;
private JTextArea chatInputArea; private JTextArea chatInputArea;
private JTextArea userListArea; private JTextArea userListArea;
...@@ -14,16 +27,65 @@ public class ChatScreen { ...@@ -14,16 +27,65 @@ public class ChatScreen {
private JPanel x; private JPanel x;
private JFrame frame; 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 = new JFrame("App");
frame.setContentPane(panel2); frame.setContentPane(panel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); frame.pack();
frame.setVisible(true); frame.setVisible(true);
createUIComponents(); createUIComponents();
} }
private void createUIComponents() { private void createUIComponents() {
ryanPanel = new PaintGUI().getGlobal(); ryanPanel = new PaintGUI().getGlobal();
......
package GUI; package GUI;
import client.DrawingArea;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
......
...@@ -40,14 +40,13 @@ public class StartScreen { ...@@ -40,14 +40,13 @@ public class StartScreen {
{ {
frame.setVisible(false); frame.setVisible(false);
frame.dispose(); frame.dispose();
client.run(); //client.doSomething();
new ChatScreen(client);
} }
else else
{ {
showErrorMessage("Could not connect to server..."); showErrorMessage("Could not connect to server...");
} }
//new ChatScreen();
} }
} }
......
...@@ -8,14 +8,21 @@ import java.rmi.server.UnicastRemoteObject; ...@@ -8,14 +8,21 @@ import java.rmi.server.UnicastRemoteObject;
public class ChatUpdate extends UnicastRemoteObject implements IChatUpdate, Serializable public class ChatUpdate extends UnicastRemoteObject implements IChatUpdate, Serializable
{ {
public ChatUpdate() throws RemoteException private Client client;
public ChatUpdate(Client client) throws RemoteException
{ {
super(); super();
this.client = client;
} }
@Override @Override
public boolean notifyChat(String fromClient, String message) throws RemoteException 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); System.out.println(fromClient + ": " + message);
return true; return true;
} }
......
package client; package client;
import GUI.ChatScreen;
import remote.IChatController; import remote.IChatController;
import remote.IClientController; import remote.IClientController;
import client.ChatUpdate; import client.ChatUpdate;
...@@ -25,11 +26,37 @@ public class Client ...@@ -25,11 +26,37 @@ public class Client
private StartScreen startScreen; 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) public void setUsername(String userName)
{ {
this.userName = userName; this.userName = userName;
} }
public String getUserName()
{
return this.userName;
}
public void setServerAddress(String serverAddress) public void setServerAddress(String serverAddress)
{ {
this.serverAddress = serverAddress; this.serverAddress = serverAddress;
...@@ -38,7 +65,7 @@ public class Client ...@@ -38,7 +65,7 @@ public class Client
public Client() throws RemoteException public Client() throws RemoteException
{ {
this.userName = "DefaultUser"; this.userName = "DefaultUser";
this.chatUpdate = new ChatUpdate(); this.chatUpdate = new ChatUpdate(this);
this.startScreen = new StartScreen(this); this.startScreen = new StartScreen(this);
this.drawingUpdate = new DrawingUpdate(); this.drawingUpdate = new DrawingUpdate();
} }
...@@ -56,12 +83,13 @@ public class Client ...@@ -56,12 +83,13 @@ public class Client
} }
} }
public void run() public void doSomething()
{ {
//connect(); // new ChatScreen();
try try
{ {
new ChatScreen(this);
System.out.println("Sleeping..."); System.out.println("Sleeping...");
TimeUnit.MINUTES.sleep(5); TimeUnit.MINUTES.sleep(5);
} }
...@@ -95,7 +123,13 @@ public class Client ...@@ -95,7 +123,13 @@ public class Client
return false; return false;
} }
public String getReceivedMessage()
{
return receivedMessage;
}
public void setReceivedMessage(String receivedMessage)
{
this.receivedMessage = receivedMessage;
}
} }
\ No newline at end of file
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
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment