-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
120 lines (111 loc) · 5.02 KB
/
Copy pathMessage.java
File metadata and controls
120 lines (111 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
import java.util.Arrays;
/**
* Message class
* PS-6
* @author godwin kangor
* @author Ahmed Elmi
* Dartmouth CS 10, Winter 2024
* determine what action to perform and update sketch based on messages from users.
*/
public class Message {
private Shape curr = null; //current shape
String[] message; //message broken out into array
Editor e; //editor
Sketch sketch; //sketch
String text; //text
/**
* Constructor
* @param e editor
* @param text text
* @param sketch sketch
*/
public void message(Editor e, String text, Sketch sketch){
this.e = e;
this.text = text;
this.sketch = sketch;
}
/**
* Updates the shapes
* @param msg message input
* @param sketch sketch
* @return sketch id
*/
public int update(String msg, Sketch sketch){
Sketch temp;
message = msg.split(" ");
switch (message[0]) {
case "draw" -> {
if (message[1].equals("polyline")) {
String[] points = message[2].split(",");
//System.out.println(Arrays.toString(points));
curr = new Polyline(Integer.parseInt(points[0]), Integer.parseInt(points[1]), new Color(Integer.parseInt(message[3])));
for (int i = 2; i < points.length-2; i = i + 2) {
((Polyline) curr).addNext(Integer.parseInt(points[i]),Integer.parseInt(points[i + 1]));
}
}
if (message[1].equals("ellipse")) {
curr = new Ellipse(Integer.parseInt(message[2]), Integer.parseInt(message[3]), Integer.parseInt(message[4]),
Integer.parseInt(message[5]), new Color(Integer.parseInt(message[6])));
}
if (message[1].equals("rectangle")) {
curr = new Rectangle(Integer.parseInt(message[2]), Integer.parseInt(message[3]), Integer.parseInt(message[4]),
Integer.parseInt(message[5]), new Color(Integer.parseInt(message[6])));
}
if (message[1].equals("segment")) {
curr = new Segment(Integer.parseInt(message[2]), Integer.parseInt(message[3]), Integer.parseInt(message[4]),
Integer.parseInt(message[5]), new Color(Integer.parseInt(message[6])));
}
sketch.addSketch(curr);
}
case "move" -> {
int id = Integer.parseInt(message[1]);
curr = sketch.getShape(id);
if (curr != null) {
curr.moveBy(Integer.parseInt(message[4]), Integer.parseInt(message[5]));
}
}
case "delete" -> {
int id = Integer.parseInt(message[1]);
curr = sketch.getShape(id);
sketch.remove(id);
}
case "recolor" -> {
int id = Integer.parseInt(message[1]);
curr = sketch.getShape(id);
curr.setColor(new Color(Integer.parseInt(message[2])));
}
case "update" -> {
if (message[2].equals("polyline")) {
String[] points = message[3].split(",");
//System.out.println(points);
curr = new Polyline(Integer.parseInt(points[0]), Integer.parseInt(points[1]), new Color(Integer.parseInt(message[4])));
for (int i = 2; i < points.length-2; i = i + 2) {
((Polyline) curr).addNext(Integer.parseInt(points[i]),Integer.parseInt(points[i + 1]));
}
sketch.addSketch(Integer.parseInt(message[5]), curr);
}
if (message[2].equals("ellipse")) {
curr = new Ellipse(Integer.parseInt(message[3]), Integer.parseInt(message[4]), Integer.parseInt(message[5]),
Integer.parseInt(message[6]), new Color(Integer.parseInt(message[7])));
sketch.addSketch(Integer.parseInt(message[8]), curr);
}
if (message[2].equals("rectangle")) {
curr = new Rectangle(Integer.parseInt(message[3]), Integer.parseInt(message[4]), Integer.parseInt(message[5]),
Integer.parseInt(message[6]), new Color(Integer.parseInt(message[7])));
sketch.addSketch(Integer.parseInt(message[8]), curr);
}
if (message[2].equals("segment")) {
curr = new Segment(Integer.parseInt(message[3]), Integer.parseInt(message[4]), Integer.parseInt(message[5]),
Integer.parseInt(message[6]), new Color(Integer.parseInt(message[7])));
sketch.addSketch(Integer.parseInt(message[8]), curr);
}
}
case "start" -> {
temp = new Sketch();
sketch = temp;
}
}
return sketch.getId(curr); //return shape ID
}
}