source: trunk/src/org/expeditee/items/Dot.java@ 108

Last change on this file since 108 was 108, checked in by ra33, 16 years ago

Heaps of changes!!!!
Added circles...
Better drawing of lines etc etc

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