From 206b08c2cac4440e56abaa177b1f53ffac713207 Mon Sep 17 00:00:00 2001
From: Hai HoDac <hhodac@student.unimelb.edu.au>
Date: Wed, 23 Oct 2019 14:01:30 +1100
Subject: [PATCH] Fixed auto scale chat window / admin tool window when assign
other member as manager. (ChatScreen) Implemented clear canvas function.
Modified initialization of 3 controllers in server.
Issue:
_ still cannot load canvas which has text on it
_ text insert is not proper functional in the first insertion. after a shape is input, text insertion function starts to work properly.
_ added serializableImage class but don't know how it works and how to implement it.
---
src/GUI/ChatScreen.form | 8 ++-
src/GUI/DrawingArea.java | 89 +++++-------------------------
src/GUI/PaintGUI.java | 20 ++++++-
src/GUI/SerializableImage.java | 23 ++++++++
src/client/DrawingUpdate.java | 5 ++
src/remote/IDrawingController.java | 2 +
src/remote/IDrawingUpdate.java | 1 +
src/server/DrawingController.java | 34 ++++++++++++
src/server/Server.java | 16 ++++--
9 files changed, 113 insertions(+), 85 deletions(-)
create mode 100644 src/GUI/SerializableImage.java
diff --git a/src/GUI/ChatScreen.form b/src/GUI/ChatScreen.form
index fc19eca..8628672 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 a5dfa87..481c938 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 0621a49..1b87a66 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 0000000..159436e
--- /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 59f39d9..bccf033 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 e39d8ec..e30900a 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 27f73cc..91e2657 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 da53d58..dd89137 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 6584224..02172a2 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");
}
--
GitLab