Skip to content

Commit 7c26f77

Browse files
committed
Add line Width, modified contians function: Individual components can override contains method
1 parent 30d6eb5 commit 7c26f77

File tree

6 files changed

+54
-13
lines changed

6 files changed

+54
-13
lines changed

src/actions/ConstructLineSegmentAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public boolean canPerformAction() {
2525
return false;
2626
}
2727
}
28+
//TODO If line segment already exists, do not add again!!!
29+
//TODO Do not allow adding two line segments connecting the same point
2830
return true;
2931

3032
}

src/icons/LeftArrow.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ protected void paintNotSelected(Graphics g) {
4242
g.fillPolygon(poly);
4343
}
4444

45+
46+
4547
@Override
46-
public Rectangle getBounds() {
47-
return poly.getBounds();
48+
public boolean contains(int x2, int y2) {
49+
return poly.getBounds().contains(x2, y2);
4850
}
4951
};
5052

src/paintcomponents/LineSegment.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class LineSegment extends PaintComponent {
1818
private Color defaultColor;
1919
private Color selectColor;
2020
private Stroke stroke;
21+
private double strokeWidth;
2122

2223
/**
2324
* @return the toPoint
@@ -57,6 +58,7 @@ public LineSegment(SimplePoint fromPoint, SimplePoint toPoint,
5758
this.defaultColor = defaultColor;
5859
this.selectColor = selectColor;
5960
this.stroke = new BasicStroke(strokeWidth);
61+
this.strokeWidth = strokeWidth;
6062
}
6163

6264
public LineSegment(SimplePoint fromPoint, SimplePoint toPoint) {
@@ -70,7 +72,7 @@ public LineSegment(SimplePoint fromPoint, SimplePoint toPoint) {
7072
@Override
7173
protected void paintNotSelected(Graphics g) {
7274
g.setColor(defaultColor);
73-
((Graphics2D)g).setStroke(stroke);
75+
((Graphics2D) g).setStroke(stroke);
7476
g.drawLine(fromPoint.getX(), fromPoint.getY(), toPoint.getX(),
7577
toPoint.getY());
7678

@@ -79,16 +81,50 @@ protected void paintNotSelected(Graphics g) {
7981
@Override
8082
protected void paintSelected(Graphics g) {
8183
g.setColor(selectColor);
82-
((Graphics2D)g).setStroke(stroke);
84+
((Graphics2D) g).setStroke(stroke);
8385
g.drawLine(fromPoint.getX(), fromPoint.getY(), toPoint.getX(),
8486
toPoint.getY());
8587
}
8688

8789
@Override
88-
public Rectangle getBounds() {
89-
return new Rectangle(fromPoint.getX(), fromPoint.getY(),
90-
toPoint.getX() - fromPoint.getX(),
91-
toPoint.getY() - fromPoint.getY());
90+
public boolean contains(int x, int y) {
91+
// if either end points contains, I do not contain
92+
if (fromPoint.contains(x, y) || toPoint.contains(x, y)) {
93+
return false;
94+
}
95+
// else return the D(curPoint , fromPoint) + D(curPoint, toPoint) ==
96+
// D(fromPoint, toPoint)
97+
double distanceBetweenXYandFrom = Math
98+
.sqrt(Math.pow(fromPoint.getX() - x, 2)
99+
+ Math.pow(fromPoint.getY() - y, 2));
100+
double distanceBetweenXYandTo = Math
101+
.sqrt(Math.pow(toPoint.getX() - x, 2)
102+
+ Math.pow(toPoint.getY() - y, 2));
103+
double distanceBetweenFromAndTo = Math
104+
.sqrt(Math.pow(toPoint.getX() - fromPoint.getX(), 2)
105+
+ Math.pow(toPoint.getY() - fromPoint.getY(), 2));
106+
107+
// checking delta distance
108+
// Note: this calculation is only an approximation
109+
if (Math.abs(distanceBetweenFromAndTo - distanceBetweenXYandFrom
110+
- distanceBetweenXYandTo) < Math.sqrt(strokeWidth)) {
111+
return true;
112+
113+
}
114+
return false;
115+
}
116+
117+
@Override
118+
public void translate(int i, int j) {
119+
super.translate(i, j);
120+
// if from and to points are not selected, translate them as well
121+
if (this.fromPoint.isSelected() == false){
122+
this.fromPoint.translate(i, j);
123+
}
124+
if (this.toPoint.isSelected() == false){
125+
this.toPoint.translate(i, j);
126+
}
127+
92128
}
93129

94130
}

src/paintcomponents/PaintComponent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public void toggleSelect() {
7171
public boolean isSelected(){
7272
return selected;
7373
}
74-
public abstract Rectangle getBounds();
7574

7675
public void translate(int i, int j) {
7776
this.x+=i;
@@ -80,4 +79,7 @@ public void translate(int i, int j) {
8079
}
8180

8281

82+
public abstract boolean contains(int x2, int y2);
83+
84+
8385
}

src/paintcomponents/SimplePoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ protected void paintSelected(Graphics g) {
4848
}
4949

5050
@Override
51-
public Rectangle getBounds() {
52-
return new Rectangle(this.getX()- this.radius/2, this.getY() - this.radius/2, radius, radius);
51+
public boolean contains(int x, int y){
52+
return new Rectangle(this.getX()- this.radius/2, this.getY() - this.radius/2, radius, radius).contains(x, y);
5353
}
5454

5555
}

src/ui/PaintPanel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ public void addPaintComponent(PaintComponent comp) {
186186
* @return null if there is no component under current point
187187
*/
188188
public PaintComponent componentUnderPoint(int x, int y) {
189-
System.out.println("Nuber of Paint Component: " +components.size() + " " + components.toString());
190189
for (PaintComponent paintComponent : components) {
191-
if (paintComponent.getBounds().contains(x, y)) {
190+
if (paintComponent.contains(x, y)) {
192191
return paintComponent;
193192
}
194193
}

0 commit comments

Comments
 (0)