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