Skip to content

Commit eb48045

Browse files
committed
Merge branch 'develop' of https://github.com/UCSDOalads/JavaSketchPad into updateMenuBarbyKent
2 parents 8dc27a8 + 46edfd8 commit eb48045

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/actions/ConstructLineSegmentAction.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package actions;
22

3+
import java.awt.Component;
34
import java.util.ArrayList;
45

56
import paintcomponents.LineSegment;
@@ -20,12 +21,32 @@ public boolean canPerformAction() {
2021
//only two points can be selected
2122
if(items.size() != 2) return false;
2223
//selected component must be of type point
24+
2325
for (PaintComponent paintComponent : items) {
2426
if(!(paintComponent instanceof SimplePoint)){
2527
return false;
2628
}
2729
}
28-
//TODO If line segment already exists, do not add again!!!
30+
31+
//check if line segment already exist
32+
// if exists, then it will not form a new line segment
33+
ArrayList<PaintComponent> components = panel.getPaintComponents();
34+
LineSegment line = null;
35+
36+
//get all paintComponents
37+
for(PaintComponent paintComponent : components) {
38+
if( paintComponent instanceof LineSegment ) {
39+
line = (LineSegment) paintComponent;
40+
//check front point and to point similarities
41+
if(items.get(0) == line.getFromPoint() &&
42+
items.get(1) == line.getToPoint())
43+
return false;
44+
if(items.get(1) == line.getFromPoint() &&
45+
items.get(0) == line.getToPoint())
46+
return false;
47+
}
48+
}
49+
2950
//TODO Do not allow adding two line segments connecting the same point
3051
return true;
3152

src/ui/PaintPanel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ public void addPaintComponent(PaintComponent comp) {
177177
components.add(comp);
178178

179179
}
180+
181+
public ArrayList<PaintComponent> getPaintComponents() {
182+
return components;
183+
}
180184

181185
/**
182186
* Returns the topmost component under a given point

0 commit comments

Comments
 (0)