Skip to content
Snippets Groups Projects
Commit 639a2044 authored by Hai HoDac's avatar Hai HoDac
Browse files

check

parent 168350fb
No related branches found
No related tags found
No related merge requests found
......@@ -28,4 +28,18 @@ public class ChatScreen {
ryanPanel = new PaintGUI().getGlobal();
}
// public static void main (String[] args) {
// ChatScreen chatScreen = new ChatScreen();
// chatScreen.go();
// }
public void go(){
//joinButton.addActionListener(actionListener);
frame = new JFrame("App");
frame.setContentPane(panel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
package GUI;
import client.DrawingArea;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
......
......@@ -22,8 +22,13 @@ public class StartScreen {
this.client = client;
}
public StartScreen() {
super();
}
// public static void main(String[] args) {
// new StartScreen().go();
// StartScreen startScreen = new StartScreen();
// startScreen.go();
// }
......@@ -39,7 +44,7 @@ public class StartScreen {
if( client.connect() )
{
frame.setVisible(false);
frame.dispose();
// frame.dispose();
client.run();
}
else
......
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