-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawingModel.java
97 lines (85 loc) · 2.64 KB
/
DrawingModel.java
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
/*
* CS 349 Java Code Examples
*
* ShapeDemo Demo of Shape class: draw shapes using mouse.
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import javax.vecmath.*;
public class DrawingModel{
ArrayList<Shape> strokeList;
private StatusbarView sbv;
private ToolbarView tbv;
private CanvasView cv;
int totalStroke = 0;
int IDcount = 0;
public DrawingModel(){
strokeList = new ArrayList<Shape>();
}
public void setModels(StatusbarView statusbar, ToolbarView toolbar, CanvasView canvas){
sbv = statusbar;
tbv = toolbar;
cv = canvas;
}
public void notifyStatusbar(){
sbv.updateNumStrokes(totalStroke);
}
public void notifyCanvasview(){ // notify CanvasView to refresh.
cv.rp();
}
public void newStroke(Shape s){
strokeList.add(s);
}
public int getLength(){
return strokeList.size();
}
public int getTotalStroke() {return totalStroke;}
public void setTotalStroke(int i){totalStroke = i;}
public int getIDCount(){return IDcount;}
public void incrementIDCount(){IDcount = IDcount + 1;}
public void deleteStroke(){
int L = getLength();
for(int i = 0; i < L; i = i + 1){
Shape s = strokeList.get(i);
if (s.getIsSelected()){
strokeList.remove(i);
setTotalStroke(getTotalStroke()-1);
notifyCanvasview();
notifyStatusbar();
break;
}
}
}
public void hit_test(double x, double y){
int L = this.getLength();
int hitID = -1;
for(int i = 0; i < L; i = i + 1){
Shape s = strokeList.get(i);
if (s.hittest(x,y)){
hitID = s.getID();
// if a stroke is selected, update the toolbar
tbv.notifyStatusChange(true);
tbv.notifyLabelChange(1, (double)s.getScale());
tbv.notifyLabelChange(2, s.getTheta());
tbv.notifySliderChange(1, (int)s.getScale()*10);
tbv.notifySliderChange(2, (int)s.getTheta());
sbv.updateSelected(totalStroke, s.getLength(), s.getScale(), (int)s.getTheta());
}
}
for(int i = 0; i < L; i = i + 1){ // only the top stroke will be selected
Shape s = strokeList.get(i);
if (s.getID() != hitID){
s.setIsSelected(false);
}
notifyCanvasview();
}
if (hitID == -1){
tbv.notifyStatusChange(false);
notifyStatusbar();
}
}
}