diff --git a/src/GUI/ChatScreen.form b/src/GUI/ChatScreen.form index fc19ecab3953bc99aa5416516e99b31508b22226..8628672c8b03ba53bb54ae10847a29458c98b80e 100644 --- a/src/GUI/ChatScreen.form +++ b/src/GUI/ChatScreen.form @@ -23,7 +23,9 @@ <grid id="bad73" binding="myAreaPanel" layout-manager="GridLayoutManager" row-count="11" 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="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="1" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="1" anchor="0" fill="3" indent="0" use-parent-layout="false"> + <preferred-size width="200" height="-1"/> + </grid> </constraints> <properties/> <border type="none" title="My Area"/> @@ -124,7 +126,9 @@ <grid id="5d886" binding="chatPanel" layout-manager="GridLayoutManager" row-count="6" 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="2" col-span="1" vsize-policy="7" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + <grid row="0" column="0" row-span="2" col-span="1" vsize-policy="7" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false"> + <preferred-size width="300" height="-1"/> + </grid> </constraints> <properties/> <border type="none" title="Chat Area"/> diff --git a/src/GUI/DrawingArea.java b/src/GUI/DrawingArea.java index a5dfa87448243d06b8516b17802b3b51af883a64..481c938471b92a3d62209282dd34a3560bf87ede 100644 --- a/src/GUI/DrawingArea.java +++ b/src/GUI/DrawingArea.java @@ -61,7 +61,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis lineStroke = new BasicStroke(strokeSize); eraserSize = 10; textSize = 60; - textString = "Enter text..."; + textString = "Text here."; drawing = null; addMouseListener(this); addMouseMotionListener(this); @@ -218,70 +218,6 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis eraserSize = size; } -// public void setColorAqua() { -// shapeColor = new Color(0,255, 255); -// } -// -// public void setColorBlack() { -// shapeColor = new Color(0, 0, 0); -// } -// -// public void setColorBlue() { -// shapeColor = new Color(0, 0, 255); -// } -// -// public void setColorFuchsia() { -// shapeColor = new Color(255, 0, 255); -// } -// -// public void setColorGray() { -// shapeColor = new Color(128, 128, 128); -// } -// -// public void setColorGreen() { -// shapeColor = new Color(0, 128, 0); -// } -// -// public void setColorLime() { -// shapeColor = new Color(0, 255, 0); -// } -// -// public void setColorMaroon() { -// shapeColor = new Color(128,0, 0); -// } -// -// public void setColorNavy() { -// shapeColor = new Color(0, 0, 128); -// } -// -// public void setColorOlive() { -// shapeColor = new Color(128, 128, 0); -// } -// -// public void setColorPurple() { -// shapeColor = new Color(128, 0, 128); -// } -// -// public void setColorRed() { -// shapeColor = new Color(255, 0, 0); -// } -// -// public void setColorSilver() { -// shapeColor = new Color(192, 192, 192); -// } -// -// public void setColorTeal() { -// shapeColor = new Color(0, 128, 128); -// } -// -// public void setColorWhite() { -// shapeColor = new Color(255, 255, 255); -// } -// -// public void setColorYellow() { -// shapeColor = new Color(255, 255, 0); -// } - @Override public void mouseClicked(MouseEvent e) { startPoint = e.getPoint(); @@ -298,6 +234,9 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis } catch (RemoteException ex) { ex.printStackTrace(); } + repaint(); + break; + default: break; } } @@ -327,8 +266,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis drawing = new Rectangle2D.Double(); break; -// case TEXT: -// + case TEXT: + // g2.setFont(new Font("TimesRoman", Font.PLAIN, textSize)); // g2.drawString(textString, startPoint.x, startPoint.y); // try { @@ -336,6 +275,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis // } catch (RemoteException ex) { // ex.printStackTrace(); // } +// repaint(); // break; } } @@ -347,20 +287,17 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis case OVAL: case RECTANGLE: case CIRCLE: - - /// Abort drawing if 2D has no width or height /// - if (((RectangularShape) drawing).getWidth() == 0 || ((RectangularShape) drawing).getHeight() == 0) { - break; - } case FREEHAND: case LINE: - // Graphics2D g2 = (Graphics2D) image.getGraphics(); g2.setColor(shapeColor); - - /// Uncomment the line below to fill the shapes with color /// - // g2.fill(drawing); + // g2.fill(drawing); /// Uncomment the line to fill the shapes with color /// g2.setStroke(lineStroke); g2.draw(drawing); + break; + case TEXT: + case ERASE: + default: + break; } g2 = (Graphics2D) image.getGraphics(); diff --git a/src/GUI/PaintGUI.java b/src/GUI/PaintGUI.java index 0621a492813df01f10cc8e58bb1fbd89455cddcd..1b87a668692f5c400ca04d65c24daad095848d3e 100644 --- a/src/GUI/PaintGUI.java +++ b/src/GUI/PaintGUI.java @@ -13,6 +13,7 @@ import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.image.BufferedImage; import java.io.File; +import java.rmi.RemoteException; public class PaintGUI extends JPanel { @@ -165,7 +166,24 @@ public class PaintGUI extends JPanel { /// Clear button /// if (e.getSource() == clearBtn) { - drawingArea.clear(); + int reply = JOptionPane.showConfirmDialog(null, + "Do you want to save the canvas before clearing?", + "Canvas clearance", JOptionPane.YES_NO_OPTION); + + if( reply == JOptionPane.YES_OPTION ) + { + try + { + client.getDrawingController().broadcastClearCanvas(client.getUserName()); + } + catch (RemoteException err) + { + JOptionPane.showMessageDialog(null, + "Error in clearing the canvas", "Error", JOptionPane.ERROR_MESSAGE); + } + + drawingArea.clear(); + } /// Create new canvas /// } else if (e.getSource() == newBtn) { diff --git a/src/GUI/SerializableImage.java b/src/GUI/SerializableImage.java new file mode 100644 index 0000000000000000000000000000000000000000..159436e9213b1b666e2e4448a16afa3c7536808d --- /dev/null +++ b/src/GUI/SerializableImage.java @@ -0,0 +1,23 @@ +package GUI; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +public class SerializableImage implements Serializable { + + transient BufferedImage image; + + public void writeObject(ObjectOutputStream output) throws IOException { + output.defaultWriteObject(); + ImageIO.write(image, "png", output); + } + + public void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { + input.defaultReadObject(); + image = ImageIO.read(input); + } +} diff --git a/src/client/DrawingUpdate.java b/src/client/DrawingUpdate.java index 59f39d947853d9edde1add1c7c901959875ebe14..bccf0339c196411c022f16f408709239a90cc8d3 100644 --- a/src/client/DrawingUpdate.java +++ b/src/client/DrawingUpdate.java @@ -70,4 +70,9 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate client.getApplicationMain().getPaintGUI().getDrawingArea().repaint(); return true; } + + public boolean notifyCanvasClearance(String fromClient) throws RemoteException { + client.getApplicationMain().getPaintGUI().getDrawingArea().clear(); + return true; + } } diff --git a/src/remote/IDrawingController.java b/src/remote/IDrawingController.java index e39d8ecfa95ea62e865a94a6e3c3cf8edac78347..e30900af1ff1193799bb14110c0231985a9d2837 100644 --- a/src/remote/IDrawingController.java +++ b/src/remote/IDrawingController.java @@ -20,6 +20,8 @@ public interface IDrawingController extends Remote { 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; + boolean broadcastClearCanvas(String fromClient) throws RemoteException; + ArrayList<Shape> getShapeList() throws RemoteException; ArrayList<Color> getColorList() throws RemoteException; ArrayList<Integer> getStrokeSizeList() throws RemoteException; diff --git a/src/remote/IDrawingUpdate.java b/src/remote/IDrawingUpdate.java index 27f73ccfb04917f7a20189f40b6e6762bbc9465f..91e26578ba95c104afab0a9b5affde0349a919fe 100644 --- a/src/remote/IDrawingUpdate.java +++ b/src/remote/IDrawingUpdate.java @@ -9,4 +9,5 @@ public interface IDrawingUpdate extends Remote, Serializable { 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; + boolean notifyCanvasClearance(String fromClient) throws RemoteException; } diff --git a/src/server/DrawingController.java b/src/server/DrawingController.java index da53d58f1248a09b7a6d712843c79aa809a3f712..dd89137a80de993953a9355dd991794b20a43aed 100644 --- a/src/server/DrawingController.java +++ b/src/server/DrawingController.java @@ -1,5 +1,6 @@ package server; +import GUI.SerializableImage; import remote.IDrawingController; import remote.IDrawingUpdate; @@ -21,6 +22,7 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo private final static int AREA_HEIGHT = 620; private Server server; + private SerializableImage serializableImage; private BufferedImage bufferedImage; private Graphics2D g2; @@ -35,9 +37,15 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo 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.serializableImage = new SerializableImage(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>(); @@ -118,6 +126,32 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo return true; } + public boolean broadcastClearCanvas(String fromClient) throws RemoteException { + System.out.print("Broadcasting canvas clearance to everyone..."); + + IDrawingUpdate client; + + this.shapeList.clear(); + this.colorList.clear(); + this.strokeSizeList.clear(); + + this.textList.clear(); + this.fontList.clear(); + this.textStartPointList.clear(); + + for( User u : server.users ) + { + if (!u.getUserName().equals(fromClient)) { + client = u.getIDrawingUpdate(); + client.notifyCanvasClearance(fromClient); + } + } + + System.out.print("...DONE\n"); + + return true; + } + // public ImageCanvas getCurrentImage() { // return image; // } diff --git a/src/server/Server.java b/src/server/Server.java index 65842248bec9de49f36f79a9e29c9e2eface21a3..02172a2b8dcee73a78134e6651f1debc376203e8 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -49,13 +49,17 @@ public class Server String chatControllerName = "ChatController"; String drawingControllerName = "DrawingController"; - IClientController clientController = new ClientController(this); - IChatController chatController = new ChatController(this); - IDrawingController drawingController = new DrawingController(this); +// IClientController clientController = new ClientController(this); +// IChatController chatController = new ChatController(this); +// IDrawingController drawingController = new DrawingController(this); - registry.rebind(clientControllerName, clientController); - registry.rebind(chatControllerName, chatController); - registry.rebind(drawingControllerName, drawingController); + IClientController iclientController = this.clientController; + IChatController ichatController = this.chatController; + IDrawingController idrawingController = this.drawingController; + + registry.rebind(clientControllerName, iclientController); + registry.rebind(chatControllerName, ichatController); + registry.rebind(drawingControllerName, idrawingController); System.out.println("Server is ready"); }