-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
120 lines (97 loc) · 2.31 KB
/
Model.java
File metadata and controls
120 lines (97 loc) · 2.31 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
package a2;
import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;
interface IView {
public void updateView();
}
@SuppressWarnings("serial")
public class Model implements Serializable {
private ArrayList<Polyline> strokes = new ArrayList<Polyline>();
private ArrayList<IView> views = new ArrayList<IView>();
private int sliderPos = 0;
private Color selectedColor = Color.BLACK;
private Color pickerColor = Color.WHITE;
private float selectedStroke = 2.0f;
private String filepath;
// set the view observer
public void addView(IView view) {
views.add(view);
view.updateView();
}
// notify the IView observer
private void notifyObservers() {
for (IView view : this.views) {
view.updateView();
}
}
public String getPath() {
return filepath;
}
public void setPath(String path) {
filepath = path;
}
public Color getColor() {
return selectedColor;
}
public void setColor(Color color) {
selectedColor = color;
notifyObservers();
}
public Color getPicker() {
return pickerColor;
}
public void setPicker(Color color) {
pickerColor = color;
notifyObservers();
}
public float getSelectedStroke() {
return selectedStroke;
}
public void setStroke(float thickness) {
selectedStroke = thickness;
notifyObservers();
}
public int getSliderPos() {
return sliderPos;
}
public void setSliderPos(int value) {
sliderPos = value;
notifyObservers();
}
public void incrementSlider() {
sliderPos++;
notifyObservers();
}
public void decrementSlider() {
sliderPos--;
notifyObservers();
}
public ArrayList<Polyline> getStrokes() {
return strokes;
}
public Polyline getCurrentStroke() {
return strokes.get(strokes.size() - 1);
}
public void addPoint(double x, double y) {
getCurrentStroke().addPoint(x, y);
notifyObservers();
}
public void deleteLastStroke() {
strokes.remove(strokes.size() - 1);
}
public void addStroke() {
if (strokes.size() != sliderPos) {
ArrayList<Polyline> updateStrokes = new ArrayList<Polyline>();
for (int i = 0; i < sliderPos; i++) {
updateStrokes.add(strokes.get(i));
}
strokes = updateStrokes;
}
Polyline stroke = new Polyline();
stroke.setColour(selectedColor);
stroke.setStrokeThickness(selectedStroke);
strokes.add(stroke);
notifyObservers();
}
}