-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawing.java
More file actions
179 lines (165 loc) · 4.02 KB
/
Drawing.java
File metadata and controls
179 lines (165 loc) · 4.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
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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
/**
* Class which represents a drawing of different shapes.
* Shapes in the drawing have different depth levels.
* @author Jasper Bingham
*/
public class Drawing {
private Color myColor = null;
private Shape currShape = null;
private ArrayList<Shape> myShapes = new ArrayList<Shape>(); //list to store shapes in drawing
/**
* Constructor for objects of type Drawing.
* @param initialColor the color the drawing starts as
*/
public Drawing(Color initialColor) {
myColor = initialColor;
}
/**
* Draws the entire drawing on a given page.
* @param page the page to draw on
*/
public void draw(Graphics page) {
for(int i = myShapes.size() - 1; i >= 0; i--) //draws starting with back shape
{
Shape s = myShapes.get(i);
page.setColor(s.getColor()); //set drawing color to shape color
s.drawShape(page); //draw shape on page
}
}
/**
* Returns the frontmost shape in the drawing under a given point.
* @param p the given point
* @return the frontmost shape in the drawing under the point
*/
public Shape getFrontmostContainer(Point p) {
Shape front = null;
for(int i = myShapes.size() - 1; i >= 0; i--) //starts at back of drawing
{
Shape s = myShapes.get(i);
if(s.containsPoint(p)) //is the shape under the click point?
{
front = s; //set new front
}
}
return front; //return shape closest to front
}
/**
* Sets the drawing color to a new color.
* @param newColor the new color
*/
public void setColor(Color newColor)
{
myColor = newColor;
}
/**
* Returns the current drawing color.
* @return the current drawing color
*/
public Color getColor()
{
return myColor;
}
/**
* Sets the current shape to a new shape.
* @param newShape the new shape
*/
public void setShape(Shape newShape)
{
currShape = newShape;
}
/**
* Stores the current shape in the drawing.
*/
public void addCurrent()
{
myShapes.add(0, currShape); //adds current shape to front of drawing
}
/**
* Returns the current shape in the drawing.
* @return the current shape in the drawing
*/
public Shape getCurrent()
{
return currShape;
}
/**
* Sets the current shape to be a new shape of the same type
* and color as the current shape if the current shape is stored.
*/
public void resetCurrent()
{
//Check type of current shape, then create new shape of same type/color.
if(currShape instanceof Rect)
{
currShape = new Rect(getColor());
}
else if(currShape instanceof Ellipse)
{
currShape = new Ellipse(getColor());
}
else if(currShape instanceof Segment)
{
currShape = new Segment(getColor());
}
}
/**
* Returns the shape at the front of the drawing.
* @return the shape at the front of the drawing
*/
public Shape getFront()
{
return myShapes.get(0); //get front shape
}
/**
* Remove a given shape from the drawing.
* @param s the shape to remove
*/
public void removeShape(Shape s)
{
for(int i = 0; i < myShapes.size(); i++) //search shapes
{
if(myShapes.get(i).equals(s))
{
myShapes.remove(myShapes.get(i)); //remove shape
}
}
}
/**
* Moves a given shape to the front of the drawing.
* @param s the shape to move to the front
*/
public void moveToFront(Shape s)
{
for(int i = 0; i < myShapes.size(); i++)
{
if(myShapes.get(i).equals(s)) //search shapes
{
Shape temp = myShapes.get(i); //save shape to move
myShapes.remove(i);
myShapes.add(0, temp); //put shape at front
}
}
}
/**
* Moves a given shape to the back of the drawing.
* @param s the shape to move to the back
*/
public void moveToBack(Shape s)
{
for(int i = 0; i < myShapes.size(); i++) //search shapes
{
if(myShapes.get(i).equals(s))
{
Shape curr = myShapes.get(i); //shape to move
Shape switchTarget = myShapes.get(myShapes.size() - 1); //back shape
//switch shape to move and back shape
myShapes.set(myShapes.size() - 1, curr);
myShapes.set(i, switchTarget);
}
}
}
}