source: trunk/org/expeditee/items/Constraint.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 2.3 KB
Line 
1package org.expeditee.items;
2
3/**
4 * This class represents a constraint between two Dots on the screen. A vertical
5 * constraint means the Dot's Y values will be equal, likewise a horizontal
6 * constraint means the Dot's X values will be equal.
7 *
8 * @author jdm18
9 *
10 */
11public class Constraint {
12 public static final int VERTICAL = 2;
13
14 public static final int HORIZONTAL = 3;
15
16 // the list of points that are constrained
17 private Item _start = null;
18
19 private Item _end = null;
20
21 // id and type info
22 private int _id;
23
24 private int _type;
25
26 /**
27 * Constructs a constraint with the two given end points, id, and type. Type
28 * should be one of the constants defined in this class.
29 *
30 * @param a
31 * One of the points in this constraint.
32 * @param b
33 * The other point in this constraint.
34 * @param id
35 * The ID of this constraint item.
36 * @param type
37 * The type of this constraint (horizontal, vertical).
38 */
39 public Constraint(Item a, Item b, int id, int type) {
40 _start = a;
41 _end = b;
42
43 a.addConstraint(this);
44 b.addConstraint(this);
45
46 _id = id;
47 _type = type;
48 }
49
50 /**
51 * @return The Item ID of this Constraint.
52 */
53 public int getID() {
54 return _id;
55 }
56
57 /**
58 *
59 * @return The type of this Constraint. The Type corresponds to the
60 * constants defined in this class.
61 */
62 public int getConstraintType() {
63 return _type;
64 }
65
66 public Item getStart() {
67 return _start;
68 }
69
70 public Item getEnd() {
71 return _end;
72 }
73
74 public boolean contains(Item d) {
75 if (_start == d || _end == d)
76 return true;
77
78 return false;
79 }
80
81 /**
82 * If there are only two points in this constraint, this method returns the
83 * Point not passed to it. If there are more than two points then the first
84 * encountered is returned.
85 *
86 * @return A Point in the constraint that is not the one given (or null if
87 * none is found).
88 */
89 public Item getOppositeEnd(Item from) {
90 if (from == _start)
91 return _end;
92
93 if (from == _end)
94 return _start;
95
96 return null;
97 }
98
99 public void replacePoint(Dot toReplace, Dot with) {
100 if (_start == toReplace)
101 _start = with;
102
103 if (_end == toReplace)
104 _end = with;
105 }
106
107 public String getLineEnds() {
108 String ends = "";
109
110 if (_start != null)
111 ends += _start.getID();
112
113 if (_end != null)
114 ends += " " + _end.getID();
115
116 return ends;
117 }
118
119}
Note: See TracBrowser for help on using the repository browser.