Skip to content
Snippets Groups Projects
Commit 6788b93f authored by Ho Dac Hai's avatar Ho Dac Hai
Browse files

Merge branch 'hai' into 'master'

Load previous GUI (without text)

See merge request 1050369/comp90015-dsass2-infinitymonkeys-remaster!31
parents 9ef192b7 5f3c35d7
Branches
Tags all-code-merged-by-hai-v1
1 merge request!31Load previous GUI (without text)
...@@ -7,6 +7,7 @@ import java.awt.*; ...@@ -7,6 +7,7 @@ import java.awt.*;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.ArrayList;
public class ApplicationMain extends JPanel { public class ApplicationMain extends JPanel {
private Client client; private Client client;
...@@ -38,6 +39,36 @@ public class ApplicationMain extends JPanel { ...@@ -38,6 +39,36 @@ public class ApplicationMain extends JPanel {
content.add(chatScreen.panel2, BorderLayout.EAST); content.add(chatScreen.panel2, BorderLayout.EAST);
chatScreen.setUserName(client.getUserName()); 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()); SwingUtilities.getRootPane(chatScreen.getSendButton()).setDefaultButton(chatScreen.getSendButton());
frame.addWindowListener(new WindowAdapter() frame.addWindowListener(new WindowAdapter()
{ {
......
...@@ -37,6 +37,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis ...@@ -37,6 +37,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
private Mode currentMode; private Mode currentMode;
private Color shapeColor; private Color shapeColor;
private Stroke lineStroke; private Stroke lineStroke;
private int strokeSize;
private int eraserSize; private int eraserSize;
private int textSize; private int textSize;
private String textString; private String textString;
...@@ -56,7 +57,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis ...@@ -56,7 +57,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
shapeColor = new Color(0, 0, 0); shapeColor = new Color(0, 0, 0);
currentMode = Mode.FREEHAND; currentMode = Mode.FREEHAND;
lineStroke = new BasicStroke(3); strokeSize = 3;
lineStroke = new BasicStroke(strokeSize);
eraserSize = 10; eraserSize = 10;
textSize = 60; textSize = 60;
textString = "Enter text..."; textString = "Enter text...";
...@@ -208,7 +210,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis ...@@ -208,7 +210,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
/// Drawing stroke setter /// /// Drawing stroke setter ///
public void setStroke(int size) { public void setStroke(int size) {
lineStroke = new BasicStroke(size); strokeSize = size;
lineStroke = new BasicStroke(strokeSize);
} }
public void setEraserSize(int size) { public void setEraserSize(int size) {
...@@ -281,7 +284,22 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis ...@@ -281,7 +284,22 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
@Override @Override
public void mouseClicked(MouseEvent e) { 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 @Override
...@@ -309,11 +327,16 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis ...@@ -309,11 +327,16 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
drawing = new Rectangle2D.Double(); drawing = new Rectangle2D.Double();
break; break;
case TEXT: // case TEXT:
//
g2.setFont(new Font("TimesRoman", Font.PLAIN, textSize)); // g2.setFont(new Font("TimesRoman", Font.PLAIN, textSize));
g2.drawString(textString, startPoint.x, startPoint.y); // g2.drawString(textString, startPoint.x, startPoint.y);
break; // 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 ...@@ -336,6 +359,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
/// Uncomment the line below to fill the shapes with color /// /// Uncomment the line below to fill the shapes with color ///
// g2.fill(drawing); // g2.fill(drawing);
g2.setStroke(lineStroke);
g2.draw(drawing); g2.draw(drawing);
} }
...@@ -347,7 +371,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis ...@@ -347,7 +371,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
repaint(); repaint();
try { try {
drawingController.broadcastDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor); drawingController.broadcastDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize);
} catch (RemoteException ex) { } catch (RemoteException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
...@@ -428,7 +452,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis ...@@ -428,7 +452,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis
repaint(); repaint();
try { try {
drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor); drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize);
} catch (RemoteException ex) { } catch (RemoteException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
......
...@@ -18,7 +18,16 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate ...@@ -18,7 +18,16 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate
} }
@Override @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(); Graphics2D g2 = client.getApplicationMain().getPaintGUI().getDrawingArea().getG2();
switch (mode) { switch (mode) {
case "OVAL": case "OVAL":
...@@ -27,6 +36,7 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate ...@@ -27,6 +36,7 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate
case "FREEHAND": case "FREEHAND":
case "LINE": case "LINE":
g2.setColor(color); g2.setColor(color);
g2.setStroke(new BasicStroke(strokeSize));
g2.draw(drawing); g2.draw(drawing);
break; break;
default: default:
...@@ -39,11 +49,12 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate ...@@ -39,11 +49,12 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate
return true; 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(); Graphics2D g2 = client.getApplicationMain().getPaintGUI().getDrawingArea().getG2();
switch (mode) { switch (mode) {
case "FREEHAND": case "FREEHAND":
g2.setColor(color); g2.setColor(color);
g2.setStroke(new BasicStroke(strokeSize));
g2.draw(drawing); g2.draw(drawing);
break; break;
......
...@@ -4,6 +4,7 @@ import java.awt.*; ...@@ -4,6 +4,7 @@ import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.rmi.Remote; import java.rmi.Remote;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.ArrayList;
/** /**
...@@ -16,8 +17,16 @@ import java.rmi.RemoteException; ...@@ -16,8 +17,16 @@ import java.rmi.RemoteException;
*/ */
public interface IDrawingController extends Remote { public interface IDrawingController extends Remote {
boolean broadcastDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException; boolean broadcastText(String fromClient, String text, Font font, Point startPoint) throws RemoteException;
boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) 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; // BufferedImage getCurrentImage() throws RemoteException;
} }
...@@ -6,6 +6,7 @@ import java.rmi.Remote; ...@@ -6,6 +6,7 @@ import java.rmi.Remote;
import java.rmi.RemoteException; import java.rmi.RemoteException;
public interface IDrawingUpdate extends Remote, Serializable { public interface IDrawingUpdate extends Remote, Serializable {
boolean notifyDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException; boolean notifyTextDrawing(String fromClient, String text, Font font, Point startPoint) throws RemoteException;
boolean notifyDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) 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;
} }
...@@ -22,36 +22,70 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo ...@@ -22,36 +22,70 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo
private Server server; private Server server;
private BufferedImage bufferedImage; private BufferedImage bufferedImage;
// private ImageCanvas image;
private Graphics2D g2; 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 { protected DrawingController(Server server) throws RemoteException {
this.server = server; this.server = server;
this.bufferedImage = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB); this.bufferedImage = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
g2 = (Graphics2D) bufferedImage.getGraphics(); g2 = (Graphics2D) bufferedImage.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 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 @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..."); System.out.print("Broadcasting drawing to everyone...");
// 1. Draw on server textList.add(text);
g2.setColor(color); fontList.add(font);
g2.draw(drawing); textStartPointList.add(startPoint);
g2 = (Graphics2D) bufferedImage.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); IDrawingUpdate client;
g2.setColor(color);
g2.drawImage(bufferedImage, 0,0, null); for( User u : server.users )
{
if (!u.getUserName().equals(fromClient)) {
client = u.getIDrawingUpdate();
client.notifyTextDrawing(fromClient, text, font, startPoint);
}
}
System.out.print("...DONE\n");
return true;
}
@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);
// 2. Broadcast to other clients
IDrawingUpdate client; IDrawingUpdate client;
for( User u : server.users ) for( User u : server.users )
{ {
if (!u.getUserName().equals(fromClient)) { if (!u.getUserName().equals(fromClient)) {
client = u.getIDrawingUpdate(); client = u.getIDrawingUpdate();
client.notifyDrawing(fromClient, drawing, mode, color); client.notifyDrawing(fromClient, drawing, mode, color, strokeSize);
} }
} }
...@@ -60,21 +94,22 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo ...@@ -60,21 +94,22 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo
return true; return true;
} }
public boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color) throws RemoteException { public boolean broadcastDraggingDrawing(String fromClient, Shape drawing, String mode, Color color, int strokeSize) throws RemoteException {
System.out.print("Broadcasting dragging drawing to everyone..."); System.out.print("Broadcasting dragging drawing to everyone...");
// 1. Draw on server if (mode.equals("FREEHAND")) {
// g2.setColor(color); shapeList.add(drawing);
// g2.draw(drawing); colorList.add(color);
strokeSizeList.add(strokeSize);
}
// 2. Broadcast to other clients
IDrawingUpdate client; IDrawingUpdate client;
for( User u : server.users ) for( User u : server.users )
{ {
if (!u.getUserName().equals(fromClient)) { if (!u.getUserName().equals(fromClient)) {
client = u.getIDrawingUpdate(); 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 ...@@ -86,25 +121,28 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo
// public ImageCanvas getCurrentImage() { // public ImageCanvas getCurrentImage() {
// return image; // return image;
// } // }
public ArrayList<Shape> getShapeList() {
return shapeList;
} }
class ImageCanvas implements Serializable { public ArrayList<Color> getColorList() {
transient ArrayList<BufferedImage> images; return colorList;
}
private void writeObject(ObjectOutputStream out) throws IOException { public ArrayList<Integer> getStrokeSizeList() {
out.defaultWriteObject(); return strokeSizeList;
out.writeInt(images.size()); // how many images are serialized?
for (BufferedImage eachImage : images) {
ImageIO.write(eachImage, "png", out); // png is lossless
} }
public ArrayList<String> getTextList() {
return textList;
} }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { public ArrayList<Font> getFontList() {
in.defaultReadObject(); return fontList;
final int imageCount = in.readInt();
images = new ArrayList<BufferedImage>(imageCount);
for (int i=0; i<imageCount; i++) {
images.add(ImageIO.read(in));
} }
public ArrayList<Point> getTextStartPointList() {
return textStartPointList;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment