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

Load previous GUI (without text)

If drawingArea has text -> load error
parent 769db637
No related branches found
No related tags found
1 merge request!31Load previous GUI (without text)
......@@ -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()
{
......
......@@ -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();
}
......
......@@ -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;
......
......@@ -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;
}
......@@ -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;
}
......@@ -22,36 +22,70 @@ 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);
IDrawingUpdate client;
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;
for( User u : server.users )
{
if (!u.getUserName().equals(fromClient)) {
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
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...");
// 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;
// }
public ArrayList<Shape> getShapeList() {
return shapeList;
}
class ImageCanvas implements Serializable {
transient ArrayList<BufferedImage> images;
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;
}
public ArrayList<String> getTextList() {
return textList;
}
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<Font> getFontList() {
return fontList;
}
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