diff --git a/src/GUI/ApplicationMain.java b/src/GUI/ApplicationMain.java index 21558c56fac714847d53c029627ef1d1cea24e2e..32275b92f8dece89d30856904b4cab47a776d11d 100644 --- a/src/GUI/ApplicationMain.java +++ b/src/GUI/ApplicationMain.java @@ -7,6 +7,7 @@ import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.rmi.RemoteException; +import java.util.ArrayList; public class ApplicationMain extends JPanel { private Client client; @@ -38,6 +39,36 @@ public class ApplicationMain extends JPanel { content.add(chatScreen.panel2, BorderLayout.EAST); chatScreen.setUserName(client.getUserName()); + + try { + // Update canvas + ArrayList<Shape> shapeList = client.getDrawingController().getShapeList(); + ArrayList<Color> colorList = client.getDrawingController().getColorList(); + ArrayList<Integer> strokeSizeList = client.getDrawingController().getStrokeSizeList(); + + ArrayList<String> textList = client.getDrawingController().getTextList(); + ArrayList<Font> fontList = client.getDrawingController().getFontList(); + ArrayList<Point> textStartPointList = client.getDrawingController().getTextStartPointList(); + + Graphics2D g2 = paintGUI.getDrawingArea().getG2(); +// for (int i = 0; i < textList.size(); i++) { +// g2.setFont(fontList.get(i)); +// g2.drawString(textList.get(i), textStartPointList.get(i).x, textStartPointList.get(i).y); +// client.getApplicationMain().getPaintGUI().getDrawingArea().repaint(); +// } + + for (int i = 0; i < shapeList.size(); i++) { + g2.setStroke(new BasicStroke(strokeSizeList.get(i))); + g2.setColor(colorList.get(i)); + g2.draw(shapeList.get(i)); + paintGUI.getDrawingArea().repaint(); + } + } + catch (RemoteException e) { + e.printStackTrace(); + } + + SwingUtilities.getRootPane(chatScreen.getSendButton()).setDefaultButton(chatScreen.getSendButton()); frame.addWindowListener(new WindowAdapter() { diff --git a/src/GUI/DrawingArea.java b/src/GUI/DrawingArea.java index b49cf7d6a19b2a55da9119901d39ffc542037892..a5dfa87448243d06b8516b17802b3b51af883a64 100644 --- a/src/GUI/DrawingArea.java +++ b/src/GUI/DrawingArea.java @@ -37,6 +37,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis private Mode currentMode; private Color shapeColor; private Stroke lineStroke; + private int strokeSize; private int eraserSize; private int textSize; private String textString; @@ -56,7 +57,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); shapeColor = new Color(0, 0, 0); currentMode = Mode.FREEHAND; - lineStroke = new BasicStroke(3); + strokeSize = 3; + lineStroke = new BasicStroke(strokeSize); eraserSize = 10; textSize = 60; textString = "Enter text..."; @@ -208,7 +210,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis /// Drawing stroke setter /// public void setStroke(int size) { - lineStroke = new BasicStroke(size); + strokeSize = size; + lineStroke = new BasicStroke(strokeSize); } public void setEraserSize(int size) { @@ -281,7 +284,22 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis @Override public void mouseClicked(MouseEvent e) { + startPoint = e.getPoint(); +/// Instantiate object based on current mode /// + switch (currentMode) { + + case TEXT: + + g2.setFont(new Font("TimesRoman", Font.PLAIN, textSize)); + g2.drawString(textString, startPoint.x, startPoint.y); + try { + client.getDrawingController().broadcastText(client.getUserName(), textString, g2.getFont(), startPoint); + } catch (RemoteException ex) { + ex.printStackTrace(); + } + break; + } } @Override @@ -309,11 +327,16 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis drawing = new Rectangle2D.Double(); break; - case TEXT: - - g2.setFont(new Font("TimesRoman", Font.PLAIN, textSize)); - g2.drawString(textString, startPoint.x, startPoint.y); - break; +// case TEXT: +// +// g2.setFont(new Font("TimesRoman", Font.PLAIN, textSize)); +// g2.drawString(textString, startPoint.x, startPoint.y); +// try { +// client.getDrawingController().broadcastText(client.getUserName(), textString, g2.getFont(), startPoint); +// } catch (RemoteException ex) { +// ex.printStackTrace(); +// } +// break; } } @@ -336,6 +359,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis /// Uncomment the line below to fill the shapes with color /// // g2.fill(drawing); + g2.setStroke(lineStroke); g2.draw(drawing); } @@ -347,7 +371,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis repaint(); try { - drawingController.broadcastDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor); + drawingController.broadcastDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize); } catch (RemoteException ex) { ex.printStackTrace(); } @@ -428,7 +452,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis repaint(); try { - drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor); + drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize); } catch (RemoteException ex) { ex.printStackTrace(); } diff --git a/src/client/DrawingUpdate.java b/src/client/DrawingUpdate.java index abc1fa4b035108e5e32989a3fea31bdfe600a6fd..59f39d947853d9edde1add1c7c901959875ebe14 100644 --- a/src/client/DrawingUpdate.java +++ b/src/client/DrawingUpdate.java @@ -18,7 +18,16 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate } @Override - public boolean notifyDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException { + public boolean notifyTextDrawing(String fromClient, String text, Font font, Point startPoint) throws RemoteException { + Graphics2D g2 = client.getApplicationMain().getPaintGUI().getDrawingArea().getG2(); + g2.setFont(font); + g2.drawString(text, startPoint.x, startPoint.y); + client.getApplicationMain().getPaintGUI().getDrawingArea().repaint(); + return true; + } + + @Override + public boolean notifyDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException { Graphics2D g2 = client.getApplicationMain().getPaintGUI().getDrawingArea().getG2(); switch (mode) { case "OVAL": @@ -27,6 +36,7 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate case "FREEHAND": case "LINE": g2.setColor(color); + g2.setStroke(new BasicStroke(strokeSize)); g2.draw(drawing); break; default: @@ -39,11 +49,12 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate return true; } - public boolean notifyDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException { + public boolean notifyDraggingDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException { Graphics2D g2 = client.getApplicationMain().getPaintGUI().getDrawingArea().getG2(); switch (mode) { case "FREEHAND": g2.setColor(color); + g2.setStroke(new BasicStroke(strokeSize)); g2.draw(drawing); break; diff --git a/src/remote/IDrawingController.java b/src/remote/IDrawingController.java index 1c39d4e5cf0900d1cd15c0ed9042edc4ed08b836..e39d8ecfa95ea62e865a94a6e3c3cf8edac78347 100644 --- a/src/remote/IDrawingController.java +++ b/src/remote/IDrawingController.java @@ -4,6 +4,7 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.rmi.Remote; import java.rmi.RemoteException; +import java.util.ArrayList; /** @@ -16,8 +17,16 @@ import java.rmi.RemoteException; */ public interface IDrawingController extends Remote { - boolean broadcastDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException; - boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException; + boolean broadcastText(String fromClient, String text, Font font, Point startPoint) throws RemoteException; + boolean broadcastDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException; + boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException; + ArrayList<Shape> getShapeList() throws RemoteException; + ArrayList<Color> getColorList() throws RemoteException; + ArrayList<Integer> getStrokeSizeList() throws RemoteException; + + ArrayList<String> getTextList() throws RemoteException; + ArrayList<Font> getFontList() throws RemoteException; + ArrayList<Point> getTextStartPointList() throws RemoteException; // BufferedImage getCurrentImage() throws RemoteException; } diff --git a/src/remote/IDrawingUpdate.java b/src/remote/IDrawingUpdate.java index fdfbd616a5f0da92156b1d480f31a9a628edb4b0..27f73ccfb04917f7a20189f40b6e6762bbc9465f 100644 --- a/src/remote/IDrawingUpdate.java +++ b/src/remote/IDrawingUpdate.java @@ -6,6 +6,7 @@ import java.rmi.Remote; import java.rmi.RemoteException; public interface IDrawingUpdate extends Remote, Serializable { - boolean notifyDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException; - boolean notifyDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException; + boolean notifyTextDrawing(String fromClient, String text, Font font, Point startPoint) throws RemoteException; + boolean notifyDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException; + boolean notifyDraggingDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException; } diff --git a/src/server/DrawingController.java b/src/server/DrawingController.java index 2fb582de348d07447d99c25f52fd8821907836c6..da53d58f1248a09b7a6d712843c79aa809a3f712 100644 --- a/src/server/DrawingController.java +++ b/src/server/DrawingController.java @@ -22,36 +22,46 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo private Server server; private BufferedImage bufferedImage; -// private ImageCanvas image; private Graphics2D g2; + private ArrayList<Shape> shapeList; + private ArrayList<Color> colorList; + private ArrayList<Integer> strokeSizeList; + + private ArrayList<String> textList; + private ArrayList<Font> fontList; + private ArrayList<Point> textStartPointList; + + 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); + this.shapeList = new ArrayList<Shape>(); + this.colorList = new ArrayList<Color>(); + this.strokeSizeList = new ArrayList<Integer>(); + + this.textList = new ArrayList<String>(); + this.fontList = new ArrayList<Font>(); + this.textStartPointList = new ArrayList<Point>(); } @Override - public boolean broadcastDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException { + public boolean broadcastText(String fromClient, String text, Font font, Point startPoint) 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); + textList.add(text); + fontList.add(font); + textStartPointList.add(startPoint); - // 2. Broadcast to other clients IDrawingUpdate client; for( User u : server.users ) { if (!u.getUserName().equals(fromClient)) { client = u.getIDrawingUpdate(); - client.notifyDrawing(fromClient, drawing, mode, color); + client.notifyTextDrawing(fromClient, text, font, startPoint); } } @@ -60,21 +70,46 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo return true; } - public boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException { + + @Override + public boolean broadcastDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException { + System.out.print("Broadcasting drawing to everyone..."); + + shapeList.add(drawing); + colorList.add(color); + strokeSizeList.add(strokeSize); + + IDrawingUpdate client; + + for( User u : server.users ) + { + if (!u.getUserName().equals(fromClient)) { + client = u.getIDrawingUpdate(); + client.notifyDrawing(fromClient, drawing, mode, color, strokeSize); + } + } + + System.out.print("...DONE\n"); + + return true; + } + + public boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException { System.out.print("Broadcasting dragging drawing to everyone..."); - // 1. Draw on server -// g2.setColor(color); -// g2.draw(drawing); + if (mode.equals("FREEHAND")) { + shapeList.add(drawing); + colorList.add(color); + strokeSizeList.add(strokeSize); + } - // 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); + client.notifyDraggingDrawing(fromClient, drawing, mode, color, strokeSize); } } @@ -86,25 +121,28 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo // public ImageCanvas getCurrentImage() { // return image; // } -} -class ImageCanvas implements Serializable { - transient ArrayList<BufferedImage> images; + public ArrayList<Shape> getShapeList() { + return shapeList; + } + + public ArrayList<Color> getColorList() { + return colorList; + } - 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 - } + public ArrayList<Integer> getStrokeSizeList() { + return strokeSizeList; } - 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)); - } + public ArrayList<String> getTextList() { + return textList; } -} \ No newline at end of file + + public ArrayList<Font> getFontList() { + return fontList; + } + + public ArrayList<Point> getTextStartPointList() { + return textStartPointList; + } +}