-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
202 lines (171 loc) · 5.09 KB
/
Model.java
File metadata and controls
202 lines (171 loc) · 5.09 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// The base for the MVC was taken from the mvc3 example from the lecture example code
import java.awt.*;
import java.awt.geom.*;
import java.io.Serializable;
import java.util.ArrayList;
// View interface
interface IView {
public void updateView();
}
enum tool {
SELECT, ERASE, LINE, CIRCLE, RECTANGLE, FILL
}
class MyShape implements Serializable{
Shape shape;
Color strokeColor = Color.BLACK;
Color fillColor = null;
Integer thickness = 1;
MyShape(Shape newShape, Color sColor, Integer newThickness) {
shape = newShape;
strokeColor = sColor;
thickness = newThickness;
};
}
public class Model {
private tool currentTool = tool.SELECT;
private Color currentColor = Color.BLACK;
private Integer currentThickness = 0; // Based of index of list so start at 0, canvas' paint component will handle this correctly
private MyShape drawingShape = null;
private MyShape selectedShape = null;
private ArrayList<MyShape> shapeList = new ArrayList<MyShape>();
// all views of this model
private ArrayList<IView> views = new ArrayList<IView>();
// set the view observer
public void addView(IView view) {
views.add(view);
view.updateView();
}
public ArrayList<MyShape> getShapeList() {
return shapeList;
}
public tool getCurrentTool() {
return currentTool;
}
public Color getCurrentColor() {
return currentColor;
}
public Integer getCurrentThickness() {
return currentThickness;
}
public MyShape getDrawingShape() {
return drawingShape;
}
public MyShape getSelectedShape() {
return selectedShape;
}
public void setCurrentTool(tool toolName) {
currentTool = toolName;
notifyObservers();
}
public void setCurrentColor(Color color) {
currentColor = color;
notifyObservers();
}
public void setCurrentThickness(int thickness) {
currentThickness = thickness;
notifyObservers();
}
public void setSelectedShapeColor(Color color) {
if(selectedShape != null) {
selectedShape.strokeColor = color;
}
notifyObservers();
}
public void setSelectedShapeThickness(int thickness) {
if(selectedShape != null) {
selectedShape.thickness = thickness;
}
notifyObservers();
}
public void setDrawingShape(MyShape newDrawingShape) {
drawingShape = newDrawingShape;
notifyObservers();
}
public void newDrawing() {
shapeList = new ArrayList<MyShape>();
notifyObservers();
}
public void loadShapes(ArrayList<MyShape> loadedShapeList) {
shapeList = loadedShapeList;
notifyObservers();
}
public void drawingShape(int x1, int y1, int x2, int y2) {
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
if(currentTool == tool.RECTANGLE) {
drawingShape = new MyShape(new Rectangle2D.Float(x,y,width,height), currentColor, currentThickness);
}
else if(currentTool == tool.CIRCLE) {
int size = Math.max(width, height);
drawingShape = new MyShape(new Ellipse2D.Float(x,y,size,size), currentColor, currentThickness);
}
else if(currentTool == tool.LINE) {
drawingShape = new MyShape(new Line2D.Float(x1,y1,x2,y2), currentColor, currentThickness);
}
notifyObservers();
}
public void addShape() {
shapeList.add(drawingShape);
drawingShape = null;
notifyObservers();
}
// These offsets are calculated on a mousePress event and are used to offset a shape when moving relative to where the user clicked
double offsetX;
double offsetY;
public void clearSelectedShape() {
selectedShape = null;
notifyObservers();
}
public void selectShape(int x, int y) {
for(int i = shapeList.size()-1; i >= 0; i--) {
if (shapeList.get(i).shape.contains(x, y) || shapeList.get(i).shape.intersects(x-2,y-2,4,4)) { // Check intersection with 4 pixel hitbox for lines
clearSelectedShape();
selectedShape = shapeList.get(i);
currentThickness = selectedShape.thickness;
currentColor = selectedShape.strokeColor;
break;
}
}
// Used for moving shapes relative to mousePress location
if(selectedShape != null) {
offsetX = selectedShape.shape.getBounds().getX() - x;
offsetY = selectedShape.shape.getBounds().getY() - y;
}
notifyObservers();
}
public void moveSelectedShape(int x, int y) {
if(selectedShape != null) {
double oldShapeX = selectedShape.shape.getBounds().getX();
double oldShapeY = selectedShape.shape.getBounds().getY();
AffineTransform tx = new AffineTransform();
tx.translate(x - oldShapeX + offsetX, y - oldShapeY + offsetY);
selectedShape.shape = tx.createTransformedShape(selectedShape.shape);
}
notifyObservers();
}
public void eraseShape(int x, int y) {
for(int i = shapeList.size()-1; i >= 0; i--) {
if (shapeList.get(i).shape.contains(x, y) || shapeList.get(i).shape.intersects(x-2,y-2,4,4)) { // Check intersection with 4 pixel hitbox for lines
shapeList.remove(i);
break;
}
}
notifyObservers();
}
public void fillShape(int x, int y) {
for(int i = shapeList.size()-1; i >= 0; i--) {
if (shapeList.get(i).shape.contains(x, y)) {
shapeList.get(i).fillColor = currentColor;
break;
}
}
notifyObservers();
}
private void notifyObservers() {
for (IView view : this.views) {
view.updateView();
}
}
}