1 | package org.expeditee.items;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics2D;
|
---|
5 | import java.awt.Polygon;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.expeditee.gui.Frame;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Represents a point on the screen. All Point objects are stored as x,y pairs
|
---|
15 | * with an Item ID. Note: Lines and all combinates of lines (rectangles, etc)
|
---|
16 | * are represented only as Points that share line IDs.
|
---|
17 | *
|
---|
18 | * @author jdm18
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class Dot extends Item {
|
---|
22 |
|
---|
23 | // Standard Item variables
|
---|
24 |
|
---|
25 | private static final int _MINIMUM_DOT_SIZE = 6;
|
---|
26 |
|
---|
27 | // the polygon surrounding this point, used for 'gravity'
|
---|
28 | private Polygon _poly = null;
|
---|
29 |
|
---|
30 | private int _pointType = Item.POINTTYPE_SQUARE;
|
---|
31 |
|
---|
32 | private boolean _filled = true;
|
---|
33 |
|
---|
34 | protected boolean _filledHighlight = false;
|
---|
35 |
|
---|
36 | public Dot(int id) {
|
---|
37 | super();
|
---|
38 | setID(id);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Constructs a new Point with the given x,y coordinates and Item ID.
|
---|
43 | *
|
---|
44 | * @param x
|
---|
45 | * The x coordinate of this point
|
---|
46 | * @param y
|
---|
47 | * The y coordinate of this point
|
---|
48 | * @param id
|
---|
49 | * The Item ID of this Point
|
---|
50 | */
|
---|
51 | public Dot(int x, int y, int id) {
|
---|
52 | super();
|
---|
53 | setID(id);
|
---|
54 | setPosition(x, y);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void setPointType(int type) {
|
---|
58 | _pointType = type;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void useFilledPoints(boolean val) {
|
---|
62 | _filled = val;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | protected boolean hasVisibleArrow() {
|
---|
67 | return getArrowheadRatio() != 0 && getArrowheadLength() != 0;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Sets the 'thickness' of this Dot, that is, how many pixels this Dot
|
---|
72 | * should be.
|
---|
73 | *
|
---|
74 | * @param thick
|
---|
75 | * The number of pixels to use when displaying this Dot The Dot
|
---|
76 | * is displayed as a thick x thick square.
|
---|
77 | */
|
---|
78 | @Override
|
---|
79 | public void setThickness(float thick) {
|
---|
80 | super.setThickness(thick);
|
---|
81 |
|
---|
82 | updatePolygon();
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public void setColor(Color c) {
|
---|
87 | super.setColor(c);
|
---|
88 |
|
---|
89 | // update the colour of any lines
|
---|
90 | for (Line line : getLines())
|
---|
91 | line.setColor(c);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void paintFill(Graphics2D g) {
|
---|
95 | if (getFillColor() != null && getEnclosingDots() != null) {
|
---|
96 | g.setColor(getFillColor());
|
---|
97 | g.fillPolygon(getEnclosedShape());
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public void paint(Graphics2D g) {
|
---|
103 | if (isHighlighted() /* && !Frame.FreeItems.contains(this) */) {
|
---|
104 | g.setColor(getHighlightColor());
|
---|
105 | // Draw the highlighting rectangle surrounding the dot
|
---|
106 | // this is drawn even if its part of a rectangle
|
---|
107 | Rectangle rect = getPolygon().getBounds();
|
---|
108 | if (_mode == SelectedMode.Enclosed ||
|
---|
109 | // Make sure single dots are highlighted filled
|
---|
110 | this.getConnected().size() <= 1)
|
---|
111 | g.fillRect(rect.x, rect.y, rect.width, rect.height);
|
---|
112 | else if (_mode == SelectedMode.Connected)
|
---|
113 | g.drawRect(rect.x, rect.y, rect.width, rect.height);
|
---|
114 | else if (_mode == SelectedMode.Normal) {
|
---|
115 | g.fillOval(rect.x, rect.y, rect.width, rect.height);
|
---|
116 | }
|
---|
117 | //System.out.println(_mode.toString());
|
---|
118 | }
|
---|
119 |
|
---|
120 | // dots on lines are hidden
|
---|
121 | if (getLines().size() > 0)
|
---|
122 | return;
|
---|
123 |
|
---|
124 | g.setColor(getPaintColor());
|
---|
125 |
|
---|
126 | int thick = (int) getThickness();
|
---|
127 | if (thick < 2)
|
---|
128 | thick = 2;
|
---|
129 |
|
---|
130 | int width = thick / 2;
|
---|
131 |
|
---|
132 | if (_pointType == Item.POINTTYPE_CIRCLE) {
|
---|
133 | int points = 40;
|
---|
134 |
|
---|
135 | double radians = 0.0;
|
---|
136 | int xPoints[] = new int[points];
|
---|
137 | int yPoints[] = new int[xPoints.length];
|
---|
138 |
|
---|
139 | for (int i = 0; i < xPoints.length; i++) {
|
---|
140 | xPoints[i] = getX() + (int) (width * Math.cos(radians));
|
---|
141 | yPoints[i] = getY() + (int) (width * Math.sin(radians));
|
---|
142 | radians += (2.0 * Math.PI) / xPoints.length;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (_filled)
|
---|
146 | g.fillPolygon(new Polygon(xPoints, yPoints, xPoints.length));
|
---|
147 | else
|
---|
148 | g.drawPolygon(new Polygon(xPoints, yPoints, xPoints.length));
|
---|
149 | } else {
|
---|
150 | if (_filled)
|
---|
151 | g.fillRect(getX() - width, getY() - width, thick, thick);
|
---|
152 | else
|
---|
153 | g.drawRect(getX() - width, getY() - width, thick, thick);
|
---|
154 | }
|
---|
155 |
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Updates the points of the polygon surrounding this Dot
|
---|
160 | */
|
---|
161 | protected void updatePolygon() {
|
---|
162 | int thick = (int) Math.ceil(getThickness());
|
---|
163 | // Sets a minimum size for the dot
|
---|
164 | thick = Math.max(thick, _MINIMUM_DOT_SIZE);
|
---|
165 |
|
---|
166 | int x = getX() - thick / 2;
|
---|
167 | int y = getY() - thick / 2;
|
---|
168 |
|
---|
169 | _poly = new Polygon();
|
---|
170 | _poly.addPoint(x - getGravity(), y - getGravity());
|
---|
171 | _poly.addPoint(x + thick + getGravity(), y - getGravity());
|
---|
172 | _poly.addPoint(x + thick + getGravity(), y + thick + getGravity());
|
---|
173 | _poly.addPoint(x - getGravity(), y + thick + getGravity());
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public Polygon getPolygon() {
|
---|
178 | if (_poly == null)
|
---|
179 | updatePolygon();
|
---|
180 |
|
---|
181 | Polygon external = new Polygon(_poly.xpoints, _poly.ypoints,
|
---|
182 | _poly.npoints);
|
---|
183 |
|
---|
184 | return external;
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public Dot copy() {
|
---|
189 | Dot copy = new Dot(getX(), getY(), getID());
|
---|
190 |
|
---|
191 | Item.DuplicateItem(this, copy);
|
---|
192 |
|
---|
193 | copy.setThickness(getThickness());
|
---|
194 | copy.setPointType(_pointType);
|
---|
195 | copy.useFilledPoints(_filled);
|
---|
196 |
|
---|
197 | return copy;
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public int setSelectionColor() {
|
---|
202 | super.setSelectionColor();
|
---|
203 |
|
---|
204 | return Item.DEFAULT_CURSOR;
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override
|
---|
208 | public void setAnnotation(boolean val) {
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public boolean isAnnotation() {
|
---|
213 | return false;
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | public Item merge(Item merger, int mouseX, int mouseY) {
|
---|
218 | // if the item being merged with is another Dot
|
---|
219 | if (merger instanceof Dot) {
|
---|
220 | Dot dot = (Dot) merger;
|
---|
221 |
|
---|
222 | // prevent concurrency issues if removing lines
|
---|
223 | List<Line> lines = new ArrayList<Line>();
|
---|
224 | lines.addAll(dot.getLines());
|
---|
225 |
|
---|
226 | for (Line line : lines) {
|
---|
227 | // remove lines that are in common
|
---|
228 | if (getLines().contains(line)) {
|
---|
229 | dot.removeLine(line);
|
---|
230 | removeLine(line);
|
---|
231 | } else {
|
---|
232 | // check for duplicated lines as a result of merging
|
---|
233 | Dot check = (Dot) line.getOppositeEnd(dot);
|
---|
234 | boolean dup = false;
|
---|
235 |
|
---|
236 | for (Line l : getLines()) {
|
---|
237 | Item opposite = l.getOppositeEnd(this);
|
---|
238 |
|
---|
239 | if (check == opposite) {
|
---|
240 | line.getStartItem().removeLine(line);
|
---|
241 | line.getEndItem().removeLine(line);
|
---|
242 | dup = true;
|
---|
243 | break;
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (!dup)
|
---|
248 | line.replaceEnd(dot, this);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | setThickness(dot.getThickness());
|
---|
253 | setColor(dot.getColor());
|
---|
254 | setFillColor(dot.getFillColor());
|
---|
255 |
|
---|
256 | return null;
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (merger instanceof Text) {
|
---|
260 | merger.setPosition(this.getPosition());
|
---|
261 | List<Line> lines = new LinkedList<Line>();
|
---|
262 | lines.addAll(getLines());
|
---|
263 | for (Line line : lines)
|
---|
264 | line.replaceEnd(this, merger);
|
---|
265 | this.delete();
|
---|
266 | return merger;
|
---|
267 | }
|
---|
268 |
|
---|
269 | // if the item being merged with is a Line
|
---|
270 | if (merger instanceof Line) {
|
---|
271 | Line line = (Line) merger;
|
---|
272 | // if this dot is part of the line then keep it, otherwise it
|
---|
273 | // can be removed
|
---|
274 | if (line.getOppositeEnd(this) != null && getLines().contains(line))
|
---|
275 | return merger;
|
---|
276 | else
|
---|
277 | return null;
|
---|
278 | }
|
---|
279 |
|
---|
280 | return merger;
|
---|
281 | }
|
---|
282 |
|
---|
283 | @Override
|
---|
284 | public String getTypeAndID() {
|
---|
285 | return "P " + getID();
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public void setFilledHighlight(boolean value) {
|
---|
290 | _filledHighlight = value;
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | public void delete() {
|
---|
295 | super.delete();
|
---|
296 |
|
---|
297 | for (Line l : this.getLines())
|
---|
298 | l.delete();
|
---|
299 | }
|
---|
300 |
|
---|
301 | @Override
|
---|
302 | public void anchor() {
|
---|
303 | super.anchor();
|
---|
304 | Frame current = getCurrentFrame();
|
---|
305 | for (Line line : getLines()) {
|
---|
306 | if (line.getID() < 0 && !current.getItems().contains(line)) {
|
---|
307 | line.setID(current.getNextItemID());
|
---|
308 | line.setSelectionColor();
|
---|
309 | // Mike: Why was this line here?
|
---|
310 | // anchor(line);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Override
|
---|
316 | public void addLine(Line line) {
|
---|
317 | super.addLine(line);
|
---|
318 | line.setColor(getColor());
|
---|
319 | line.setThickness(getThickness());
|
---|
320 | line.setLinePattern(getLinePattern());
|
---|
321 | }
|
---|
322 |
|
---|
323 | @Override
|
---|
324 | public void lineColorChanged(Color c) {
|
---|
325 | if (getColor() != c) {
|
---|
326 | setColor(c);
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|