Skip to content

Commit 7da9a0c

Browse files
committed
Add Line Segment component, Add dragging functionality to Select Tool
1 parent 4fa00f8 commit 7da9a0c

File tree

9 files changed

+267
-37
lines changed

9 files changed

+267
-37
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package actions;
2+
3+
import java.util.ArrayList;
4+
5+
import paintcomponents.LineSegment;
6+
import paintcomponents.PaintComponent;
7+
import paintcomponents.SimplePoint;
8+
import ui.PaintPanel;
9+
10+
public class ConstructLineSegmentAction extends PaintAction {
11+
12+
public ConstructLineSegmentAction(PaintPanel panel) {
13+
super(panel);
14+
}
15+
16+
@Override
17+
public boolean canPerformAction() {
18+
//get selected components
19+
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
20+
//only two points can be selected
21+
if(items.size() != 2) return false;
22+
//selected component must be of type point
23+
for (PaintComponent paintComponent : items) {
24+
if(!(paintComponent instanceof SimplePoint)){
25+
return false;
26+
}
27+
}
28+
return true;
29+
30+
}
31+
32+
@Override
33+
public void performAction() {
34+
//get selected components
35+
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
36+
37+
//construct line segment
38+
LineSegment lineSegment = new LineSegment((SimplePoint)(items.get(0)), (SimplePoint)(items.get(1)));
39+
//add to panel
40+
panel.addPaintComponent(lineSegment);
41+
42+
//change selection
43+
panel.getSelectTool().clearSelection();
44+
panel.getSelectTool().selectComponent(lineSegment);
45+
panel.repaint();
46+
47+
48+
}
49+
50+
@Override
51+
public String locationString() {
52+
return "Construct/Line Segment";
53+
}
54+
55+
}

src/actions/GeneratePolygonSourceJava.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.swing.JOptionPane;
1212

1313
import paintcomponents.PaintComponent;
14+
import paintcomponents.SimplePoint;
1415
import ui.PaintPanel;
1516

1617
public class GeneratePolygonSourceJava extends PaintAction {
@@ -27,7 +28,8 @@ public boolean canPerformAction() {
2728
@Override
2829
public void performAction() {
2930
try{
30-
String str = generatePolygon(panel.getSelectTool().getSelectedComponents());
31+
@SuppressWarnings("unchecked")
32+
String str = generatePolygon((ArrayList)(panel.getSelectTool().getSelectedComponents()));
3133

3234

3335
JOptionPane.showMessageDialog(panel, "Already Saved To Downloads: \n\n" + str);
@@ -39,7 +41,7 @@ public void performAction() {
3941
}
4042

4143
private String generatePolygon(
42-
ArrayList<PaintComponent> selectedComponents) {
44+
ArrayList<SimplePoint> selectedComponents) {
4345

4446

4547
//get width and height for target
@@ -55,7 +57,7 @@ private String generatePolygon(
5557

5658
//get a bounds for current polygon
5759
Polygon poly = new Polygon();
58-
for (PaintComponent paintComponent : selectedComponents) {
60+
for (SimplePoint paintComponent : selectedComponents) {
5961
poly.addPoint(paintComponent.getX(), paintComponent .getY());
6062

6163
}
@@ -64,7 +66,7 @@ private String generatePolygon(
6466
double xfactor = (double)actual.getWidth() / width;
6567
double yfactor = (double)actual.getHeight() / height;
6668

67-
for (PaintComponent paintComponent : selectedComponents) {
69+
for (SimplePoint paintComponent : selectedComponents) {
6870
int x = (int)((paintComponent.getX() - actual.getX()) / xfactor);
6971
int y = (int)((paintComponent.getY() - actual.getY()) / yfactor);
7072
builder.append("\t\tp.addPoint(" + x + ", " + y + ");\n");
@@ -92,7 +94,7 @@ private String generatePolygon(
9294

9395
@Override
9496
public String locationString() {
95-
return "Generate/Java Source File from Selection...";
97+
return "Generate/Polygon Class Java Source File from Selection...";
9698
}
9799

98100
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package actions;
1+
package actions.menu;
22

33
import java.awt.event.ActionEvent;
44
import java.awt.event.ActionListener;
@@ -7,13 +7,17 @@
77
import javax.swing.JMenuBar;
88
import javax.swing.JMenuItem;
99

10+
import actions.ConstructLineSegmentAction;
11+
import actions.GeneratePolygonSourceJava;
12+
import actions.PaintAction;
1013
import painttools.tools.SelectionToolListener;
1114
import ui.PaintPanel;
1215

1316
public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
1417

1518
public ActionsMenuBar(PaintPanel panel){
1619
addAction(new GeneratePolygonSourceJava(panel));
20+
addAction(new ConstructLineSegmentAction(panel));
1721

1822
}
1923

@@ -35,6 +39,7 @@ private void addAction(PaintAction action) {
3539
}
3640

3741
//assume 2 level depth
42+
//TODO Change here
3843
PaintActionMenuItem item = new PaintActionMenuItem(action);
3944
item.setEnabled(action.canPerformAction());
4045
item.setText(strings[1]);
@@ -56,7 +61,7 @@ public void selectionChanged() {
5661
for (int i = 0; i < getMenuCount(); i++) {
5762
JMenu menu = getMenu(i);
5863
for(int j = 0; j < menu.getItemCount(); j++){
59-
PaintActionMenuItem item = (PaintActionMenuItem) menu.getItem(i);
64+
PaintActionMenuItem item = (PaintActionMenuItem) menu.getItem(j);
6065
item.setEnabled(item.getAssociatedAction().canPerformAction());
6166
}
6267

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package actions;
1+
package actions.menu;
22

33
import javax.swing.JMenuItem;
44

5+
import actions.PaintAction;
6+
57
public class PaintActionMenuItem extends JMenuItem{
68

79

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package paintcomponents;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
import java.awt.Rectangle;
6+
7+
import settings.Defaults;
8+
9+
//TODO DEfault color, width selection
10+
public class LineSegment extends PaintComponent {
11+
12+
private SimplePoint fromPoint;
13+
private SimplePoint toPoint;
14+
15+
private Color defaultColor;
16+
private Color selectColor;
17+
18+
/**
19+
* @return the toPoint
20+
*/
21+
public SimplePoint getToPoint() {
22+
return toPoint;
23+
}
24+
25+
/**
26+
* @param toPoint
27+
* the toPoint to set
28+
*/
29+
public void setToPoint(SimplePoint toPoint) {
30+
this.toPoint = toPoint;
31+
}
32+
33+
/**
34+
* @return the fromPoint
35+
*/
36+
public SimplePoint getFromPoint() {
37+
return fromPoint;
38+
}
39+
40+
/**
41+
* @param fromPoint
42+
* the fromPoint to set
43+
*/
44+
public void setFromPoint(SimplePoint fromPoint) {
45+
this.fromPoint = fromPoint;
46+
}
47+
48+
public LineSegment(SimplePoint fromPoint, SimplePoint toPoint,
49+
Color defaultColor, Color selectColor) {
50+
super(0, 0);
51+
this.fromPoint = fromPoint;
52+
this.toPoint = toPoint;
53+
this.defaultColor = defaultColor;
54+
this.selectColor = selectColor;
55+
}
56+
57+
public LineSegment(SimplePoint fromPoint, SimplePoint toPoint) {
58+
this(fromPoint, toPoint,
59+
Defaults.sharedDefaults().defaultColorForLineSegment(),
60+
Defaults.sharedDefaults().defaultColorForSelectedLineSegment());
61+
62+
}
63+
64+
@Override
65+
protected void paintNotSelected(Graphics g) {
66+
g.setColor(defaultColor);
67+
g.drawLine(fromPoint.getX(), fromPoint.getY(), toPoint.getX(),
68+
toPoint.getY());
69+
70+
}
71+
72+
@Override
73+
protected void paintSelected(Graphics g) {
74+
g.setColor(selectColor);
75+
g.drawLine(fromPoint.getX(), fromPoint.getY(), toPoint.getX(),
76+
toPoint.getY());
77+
}
78+
79+
@Override
80+
public Rectangle getBounds() {
81+
return new Rectangle(fromPoint.getX(), fromPoint.getY(),
82+
toPoint.getX() - fromPoint.getX(),
83+
toPoint.getY() - fromPoint.getY());
84+
}
85+
86+
}

src/paintcomponents/PaintComponent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,11 @@ public boolean isSelected(){
7373
}
7474
public abstract Rectangle getBounds();
7575

76+
public void translate(int i, int j) {
77+
this.x+=i;
78+
this.y+=j;
79+
80+
}
81+
7682

7783
}

0 commit comments

Comments
 (0)