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

All works so far

parent c4ed3c3f
No related branches found
No related tags found
No related merge requests found
......@@ -36,6 +36,7 @@ public class ApplicationMain extends JPanel {
content.add(paintGUI.getGlobal(), BorderLayout.WEST);
content.add(chatScreen.panel2, BorderLayout.EAST);
SwingUtilities.getRootPane(chatScreen.getSendButton()).setDefaultButton(chatScreen.getSendButton());
frame.addWindowListener(new WindowAdapter()
{
@Override
......
......@@ -16,9 +16,10 @@ import java.awt.geom.RectangularShape;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.RemoteException;
public class DrawingArea extends JPanel implements MouseMotionListener, MouseListener {
public class DrawingArea extends JPanel implements MouseMotionListener, MouseListener, Serializable {
......@@ -42,12 +43,14 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
private Color shapeColor;// = new Color(0, 0, 0);
private Mode currentMode;// = Mode.FREEHAND;
/// Create a empty canvas ///
private BufferedImage image;// = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
/// Drawing tool
private Graphics2D g2;// = (Graphics2D) image.getGraphics();
private Graphics2D g2;// = (Graphics2D) image.getGraphics();
public DrawingArea(Client client) {
this.client = client;
......@@ -64,7 +67,6 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
addMouseMotionListener(this);
}
public Shape getDrawing() {
return drawing;
}
......@@ -73,6 +75,16 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
this.drawing = drawing;
}
public Graphics2D getG2() { return g2; }
public void setG2(Graphics2D g2) { this.g2 = g2; }
public BufferedImage getImage() { return image; }
public void setImage(BufferedImage image) { this.image = image; }
@Override
public Dimension getPreferredSize()
......@@ -108,12 +120,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
Color borderColor = currentMode != Mode.ERASE ? shapeColor : Color.WHITE;
g2.setColor(borderColor);
g2.draw(drawing);
IDrawingController drawingController = client.getDrawingController();
try {
drawingController.broadcastDrawing(client.getUserName(), drawing);
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
}
......@@ -269,6 +276,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
@Override
public void mouseReleased(MouseEvent e) {
IDrawingController drawingController = client.getDrawingController();
switch (currentMode) {
case OVAL:
case RECTANGLE:
......@@ -295,6 +303,12 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
/// This repaint is needed if we want to fill the drawing shape with color
repaint();
try {
drawingController.broadcastDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor);
} catch (RemoteException ex) {
ex.printStackTrace();
}
drawing = null;
}
......@@ -310,8 +324,9 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
@Override
public void mouseDragged(MouseEvent e) {
currentPoint = e.getPoint();
IDrawingController drawingController = client.getDrawingController();
currentPoint = e.getPoint();
int x = Math.min(startPoint.x, e.getX());
int y = Math.min(startPoint.y, e.getY());
int width = Math.abs(startPoint.x - e.getX());
......@@ -365,6 +380,11 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
}
repaint();
try {
drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor);
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
@Override
......
......@@ -217,16 +217,16 @@ public class PaintGUI extends JPanel {
public void showGUI() {
frame = new JFrame("Shared Whiteboard System");
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setContentPane(global);
frame.setSize(800, 600);
frame.setLocationRelativeTo( null );
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// public void showGUI() {
// frame = new JFrame("Shared Whiteboard System");
// JFrame.setDefaultLookAndFeelDecorated(true);
// frame.setContentPane(global);
//
// 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
......@@ -85,8 +85,7 @@ public class Client
{
Client client = new Client(args[0]);
client.connect();
// client.getChatScreen().showGUI();
// client.getPaintGUI().showGUI();
// client.getApplicationMain().getPaintGUI().getDrawingArea().setImage(client.drawingController.getCurrentImage());
client.getApplicationMain().createAndShowGUI();
}
catch (Exception e)
......
......@@ -4,6 +4,10 @@ import GUI.DrawingArea;
import remote.IDrawingUpdate;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
......@@ -18,12 +22,45 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate
}
@Override
public boolean notifyDrawing(String fromClient, Shape drawing) throws RemoteException {
client.getApplicationMain().getPaintGUI().getDrawingArea().setDrawing(drawing);
public boolean notifyDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException {
Graphics2D g2 = client.getApplicationMain().getPaintGUI().getDrawingArea().getG2();
switch (mode) {
case "OVAL":
case "RECTANGLE":
case "CIRCLE":
case "FREEHAND":
case "LINE":
g2.setColor(color);
g2.draw(drawing);
break;
default:
System.out.println("Erased");
}
g2 = (Graphics2D) client.getApplicationMain().getPaintGUI().getDrawingArea().getImage().getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(color);
client.getApplicationMain().getPaintGUI().getDrawingArea().repaint();
return true;
}
public boolean notifyDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException {
Graphics2D g2 = client.getApplicationMain().getPaintGUI().getDrawingArea().getG2();
switch (mode) {
case "FREEHAND":
g2.setColor(color);
g2.draw(drawing);
break;
case "ERASE":
g2.setColor(Color.WHITE);
g2.fill(drawing);
g2.draw(drawing);
break;
default:
break;
}
client.getApplicationMain().getPaintGUI().getDrawingArea().repaint();
// DrawingArea drawingArea = (DrawingArea) client.getChatScreen().getDrawingPanel().getComponent(1);
// drawingArea.setDrawing(drawing);
// drawingArea.repaint();
return false;
return true;
}
}
package remote;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.rmi.Remote;
import java.rmi.RemoteException;
......@@ -15,7 +16,8 @@ import java.rmi.RemoteException;
*/
public interface IDrawingController extends Remote {
boolean broadcastDrawing(String fromClient, Shape drawing) throws RemoteException;
boolean broadcastDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException;
boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException;
// BufferedImage getCurrentImage() throws RemoteException;
}
......@@ -6,5 +6,6 @@ import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IDrawingUpdate extends Remote, Serializable {
boolean notifyDrawing(String fromClient, Shape drawing) throws RemoteException;
boolean notifyDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException;
boolean notifyDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException;
}
......@@ -6,14 +6,17 @@ import remote.IChatUpdate;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class ChatController extends UnicastRemoteObject implements IChatController, Serializable
{
private Server server;
private ArrayList<String> messages;
public ChatController(Server server) throws RemoteException
{
this.server = server;
this.messages = new ArrayList<>();
}
@Override
......
......@@ -3,32 +3,108 @@ package server;
import remote.IDrawingController;
import remote.IDrawingUpdate;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class DrawingController extends UnicastRemoteObject implements IDrawingController {
private final static int AREA_WIDTH = 600;
private final static int AREA_HEIGHT = 620;
private Server server;
private BufferedImage bufferedImage;
// private ImageCanvas image;
private Graphics2D g2;
protected DrawingController(Server server) throws RemoteException {
this.server = server;
this.bufferedImage = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
g2 = (Graphics2D) bufferedImage.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
@Override
public boolean broadcastDrawing(String fromClient, Shape drawing) throws RemoteException {
public boolean broadcastDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException {
System.out.print("Broadcasting drawing to everyone...");
// 1. Draw on server
g2.setColor(color);
g2.draw(drawing);
g2 = (Graphics2D) bufferedImage.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(color);
g2.drawImage(bufferedImage, 0,0, null);
// 2. Broadcast to other clients
IDrawingUpdate client;
for( User u : server.users )
{
if (!u.getUserName().equals(fromClient)) {
client = u.getIDrawingUpdate();
client.notifyDrawing(fromClient, drawing);
client.notifyDrawing(fromClient, drawing, mode, color);
}
}
System.out.print("...DONE\n");
return true;
}
public boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException {
System.out.print("Broadcasting dragging drawing to everyone...");
// 1. Draw on server
// g2.setColor(color);
// g2.draw(drawing);
// 2. Broadcast to other clients
IDrawingUpdate client;
for( User u : server.users )
{
if (!u.getUserName().equals(fromClient)) {
client = u.getIDrawingUpdate();
client.notifyDraggingDrawing(fromClient, drawing, mode, color);
}
}
System.out.print("...DONE\n");
return true;
}
// public ImageCanvas getCurrentImage() {
// return image;
// }
}
class ImageCanvas implements Serializable {
transient ArrayList<BufferedImage> images;
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(images.size()); // how many images are serialized?
for (BufferedImage eachImage : images) {
ImageIO.write(eachImage, "png", out); // png is lossless
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
final int imageCount = in.readInt();
images = new ArrayList<BufferedImage>(imageCount);
for (int i=0; i<imageCount; i++) {
images.add(ImageIO.read(in));
}
}
}
\ 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