Skip to content

Commit c8c5297

Browse files
committed
Add Select Tool
1 parent 42d7364 commit c8c5297

File tree

8 files changed

+146
-45
lines changed

8 files changed

+146
-45
lines changed

src/paintcomponents/PaintComponent.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public void deselect(){
6464
selected = false;
6565
}
6666

67+
public void toggleSelect() {
68+
selected = !selected;
69+
}
70+
71+
public boolean isSelected(){
72+
return selected;
73+
}
6774
public abstract Rectangle getBounds();
6875

76+
6977
}

src/paintcomponents/SimplePoint.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
public class SimplePoint extends PaintComponent {
99

1010

11-
private static final int DEFUALT_RADIUS = 10;
1211
private int radius;
1312
private Color color;
1413
private Color selectedColor;

src/painttools/toolbar/ToolBar.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,70 @@
11
package painttools.toolbar;
2+
23
import java.awt.Button;
34
import java.awt.Component;
45
import java.awt.Graphics;
56
import java.awt.event.ActionEvent;
67
import java.awt.event.ActionListener;
78
import java.util.ArrayList;
89

10+
import javax.swing.BoxLayout;
911
import javax.swing.Icon;
1012
import javax.swing.JButton;
1113
import javax.swing.JPanel;
1214
import javax.tools.Tool;
1315

1416
import painttools.tools.DotTool;
1517
import painttools.tools.PaintTool;
16-
18+
import painttools.tools.SelectTool;
1719

1820
public class ToolBar extends JPanel {
19-
20-
21+
2122
public ArrayList<ToolBarListener> listeners;
22-
23-
public ToolBar(){
23+
24+
/**
25+
* Creates a default toolbar and add necessary tools
26+
*/
27+
public ToolBar() {
2428
listeners = new ArrayList<>();
29+
30+
//sets the box layout
31+
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
32+
2533
addTool(new DotTool());
34+
addTool(new SelectTool());
2635
}
2736

37+
/**
38+
* Adds a tool to the toolbar. This method will add specific tool to the
39+
* tool bar, and an action listener associated with it
40+
*
41+
* @param tool
42+
*/
2843
private void addTool(PaintTool tool) {
2944
JButton button = tool.getButton();
3045
button.addActionListener(new ActionListener() {
31-
46+
3247
@Override
3348
public void actionPerformed(ActionEvent e) {
3449
select(tool);
3550
}
3651
});
3752
add(button);
3853
}
39-
40-
public void addToolBarListener(ToolBarListener listener){
54+
55+
/**
56+
* Adds a ToolBarListener to this toolbar
57+
* @param listener
58+
*/
59+
public void addToolBarListener(ToolBarListener listener) {
4160
listeners.add(listener);
4261
}
43-
44-
private void select(PaintTool tool){
62+
63+
/**
64+
* this method should be invoked by the actionlistener when an tool button is selected
65+
* @param tool
66+
*/
67+
private void select(PaintTool tool) {
4568
for (ToolBarListener toolBarListener : listeners) {
4669
toolBarListener.toolSelected(tool);
4770
}

src/painttools/tools/DotTool.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ public class DotTool extends PaintTool<SimplePoint> {
1919
public void start(PaintPanel panel) {
2020
this.panel = panel;
2121

22+
panel.hideCursor();
2223
p = new SimplePoint(0, 0);
2324
panel.setTempComponent(p);
2425

2526
}
2627

2728

28-
@Override
29-
public SimplePoint paintedComponent() {
30-
return p;
31-
}
32-
29+
3330
@Override
3431
public JButton getButton() {
3532
JButton button = super.getButton();

src/painttools/tools/PaintTool.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,11 @@
1313
public abstract class PaintTool<T extends PaintComponent> implements MouseListener, MouseMotionListener {
1414

1515

16-
//if this tool has finished
17-
private boolean done;
18-
1916
/**
2017
* Set the start condition of this paint tool
2118
*/
2219
public abstract void start(PaintPanel panel);
2320

24-
25-
/**
26-
* the completed component
27-
* @return
28-
*/
29-
public abstract T paintedComponent();
30-
31-
/**
32-
* @return whether this component has done painting
33-
*/
34-
public boolean isDone() {
35-
return done;
36-
}
37-
38-
/**
39-
* sets the done condition
40-
* @param
41-
*/
42-
public void setDone(boolean done) {
43-
this.done = done;
44-
}
4521

4622
public JButton getButton() {
4723
return new JButton(this.getClass().getName());
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package painttools.tools;
2+
3+
import java.awt.event.MouseEvent;
4+
import java.util.ArrayList;
5+
6+
import paintcomponents.PaintComponent;
7+
import ui.PaintPanel;
8+
9+
public class SelectTool extends PaintTool {
10+
11+
private PaintPanel panel;
12+
13+
private ArrayList<PaintComponent> selectedComponents;
14+
15+
public SelectTool() {
16+
selectedComponents = new ArrayList<>();
17+
}
18+
19+
@Override
20+
public void mouseClicked(MouseEvent e) {
21+
22+
PaintComponent comp = panel.componentUnderPoint(e.getX(), e.getY());
23+
if(comp != null){
24+
if(comp.isSelected()){
25+
comp.deselect();
26+
selectedComponents.remove(comp);
27+
} else {
28+
comp.select();
29+
selectedComponents.add(comp);
30+
}
31+
panel.repaint();
32+
33+
}
34+
35+
}
36+
37+
@Override
38+
public void mousePressed(MouseEvent e) {
39+
// TODO Auto-generated method stub
40+
41+
}
42+
43+
@Override
44+
public void mouseReleased(MouseEvent e) {
45+
// TODO Auto-generated method stub
46+
47+
}
48+
49+
@Override
50+
public void mouseEntered(MouseEvent e) {
51+
// TODO Auto-generated method stub
52+
53+
}
54+
55+
@Override
56+
public void mouseExited(MouseEvent e) {
57+
// TODO Auto-generated method stub
58+
59+
}
60+
61+
@Override
62+
public void mouseDragged(MouseEvent e) {
63+
// TODO Auto-generated method stub
64+
65+
}
66+
67+
@Override
68+
public void mouseMoved(MouseEvent e) {
69+
// TODO Auto-generated method stub
70+
71+
}
72+
73+
@Override
74+
public void start(PaintPanel panel) {
75+
this.panel = panel;
76+
77+
}
78+
79+
}

src/settings/Defaults.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ public class Defaults {
66

77
public static Defaults defaults = new Defaults();
88

9+
private static Color SIMPLE_POINT_COLOR = new Color(200, 41, 47);
10+
private static Color SIMPLE_POINT_SELECTED_COLOR = new Color(0, 0, 132);
11+
private static int SIMPLE_POINT_DEFAULT_SIZE = 10;
12+
913
private Defaults(){
1014

1115
}
@@ -16,13 +20,13 @@ public static Defaults sharedDefaults(){
1620
}
1721

1822
public int defaultSimplePointSize(){
19-
return 10;
23+
return SIMPLE_POINT_DEFAULT_SIZE;
2024
}
2125

2226
public Color defaultSimplePointColor(){
23-
return new Color(200, 41, 47);
27+
return SIMPLE_POINT_COLOR;
2428
}
2529
public Color defaultSimplePointSelectedColor(){
26-
return new Color(136, 0, 32);
30+
return SIMPLE_POINT_SELECTED_COLOR;
2731
}
2832
}

src/ui/PaintPanel.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ private void setTool(PaintTool tool) {
4848
}
4949
this.tool = tool;
5050
this.tool.start(this);
51-
hideCursor();
5251
this.state = State.TOOLS;
5352
this.addMouseListener(this.tool);
5453
this.addMouseMotionListener(this.tool);
@@ -144,7 +143,8 @@ protected void paintComponent(Graphics g) {
144143
paintComponent.paint(g);
145144
}
146145
if (state == State.TOOLS) {
147-
tempComponent.paint(g);
146+
if(tempComponent != null)
147+
tempComponent.paint(g);
148148
}
149149
}
150150

@@ -167,4 +167,19 @@ public void addPaintComponent(PaintComponent comp) {
167167
components.add(comp);
168168

169169
}
170+
171+
/**
172+
* Returns the topmost component under a given point
173+
* @param x
174+
* @param y
175+
* @return null if there is no component under current point
176+
*/
177+
public PaintComponent componentUnderPoint(int x, int y) {
178+
for (PaintComponent paintComponent : components) {
179+
if(paintComponent.getBounds().contains(x, y)){
180+
return paintComponent;
181+
}
182+
}
183+
return null;
184+
}
170185
}

0 commit comments

Comments
 (0)