Skip to content

Commit dbb4f88

Browse files
committed
Add comment for Paint Objects, Add Data Input Box
1 parent e5082ea commit dbb4f88

6 files changed

Lines changed: 104 additions & 3 deletions

File tree

bin/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/actions/
2+
/icons/
3+
/paintcomponents/
4+
/painttools/
5+
/settings/
6+
/ui/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package actions;
2+
3+
import java.util.ArrayList;
4+
5+
import javax.swing.JOptionPane;
6+
7+
import paintcomponents.DataInputTextfieldPaintComponent;
8+
import paintcomponents.PaintComponent;
9+
import ui.PaintPanel;
10+
11+
public class InputDataForDataInputBoxAction extends PaintAction {
12+
13+
public InputDataForDataInputBoxAction(PaintPanel panel) {
14+
super(panel);
15+
}
16+
17+
@Override
18+
public boolean canPerformAction() {
19+
ArrayList<PaintComponent> comps = panel.getSelectTool().getSelectedComponents();
20+
if(comps.size()!= 1) return false;
21+
if(comps.get(0) instanceof DataInputTextfieldPaintComponent){
22+
return true;
23+
}
24+
return false;
25+
}
26+
27+
@Override
28+
public void performAction() {
29+
DataInputTextfieldPaintComponent inputComp = (DataInputTextfieldPaintComponent) panel.getSelectTool().getSelectedComponents().get(0);
30+
String s = JOptionPane.showInputDialog("Please specify the message to push to the data input");
31+
inputComp.inputData(s);
32+
panel.repaint();
33+
}
34+
35+
@Override
36+
public String locationString() {
37+
return "Input/Input into Data Panel";
38+
}
39+
40+
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import actions.AddTextBoxAction;
1212
import actions.ConstructLineSegmentAction;
1313
import actions.GeneratePolygonSourceJava;
14+
import actions.InputDataForDataInputBoxAction;
1415
import actions.PaintAction;
1516
import painttools.tools.SelectionToolListener;
1617
import ui.PaintPanel;
@@ -22,6 +23,7 @@ public ActionsMenuBar(PaintPanel panel){
2223
addAction(new ConstructLineSegmentAction(panel));
2324
addAction(new AddTextBoxAction(panel));
2425
addAction(new AddDataInputBoxAction(panel));
26+
addAction(new InputDataForDataInputBoxAction(panel));
2527

2628
}
2729

src/paintcomponents/DataInputTextfieldPaintComponent.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package paintcomponents;
22

3+
import java.awt.BasicStroke;
34
import java.awt.Color;
45
import java.awt.Graphics;
6+
import java.awt.Graphics2D;
57

68
import settings.Defaults;
79

810
public class DataInputTextfieldPaintComponent extends TextPaintComponent {
911

12+
private static final int HORIZONTAL_OFFSET = 10;
1013
DataFromPoint<String> fromPoint;
1114
Color boundColor;
1215
Color selectedColor;
@@ -24,21 +27,35 @@ public DataInputTextfieldPaintComponent(String displayingText, int x,
2427

2528
@Override
2629
protected void paintNotSelected(Graphics g) {
27-
super.paintNotSelected(g);
2830
g.setColor(boundColor);
31+
((Graphics2D)g).setStroke(new BasicStroke(1));
32+
super.paintNotSelected(g);
33+
updateFromPointPosition();
2934
g.drawRect(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
3035
fromPoint.paintNotSelected(g);
3136

3237
}
38+
3339

3440
@Override
3541
protected void paintSelected(Graphics g) {
36-
super.paintSelected(g);;
42+
((Graphics2D)g).setStroke(new BasicStroke(1));
3743
g.setColor(selectedColor);
44+
super.paintSelected(g);;
45+
updateFromPointPosition();
3846
g.drawRect(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
3947
fromPoint.paintSelected(g);
4048
}
4149

50+
/**
51+
* This method will use the protected bounds, which will be updated in super.paint[Not]Selected.
52+
* Make sure you've already invoked super's paintNotSelectedMethod before invoking this one.
53+
*/
54+
private void updateFromPointPosition(){
55+
this.fromPoint.setX((int) (getX() + this.bounds.getWidth() + HORIZONTAL_OFFSET));
56+
this.fromPoint.setY((int) (getY() + this.bounds.getHeight() / 2));
57+
}
58+
4259
@Override
4360
public void translate(int i, int j) {
4461
super.translate(i, j);
@@ -50,4 +67,9 @@ public boolean contains(int x, int y) {
5067

5168
return fromPoint.contains(x, y) || super.contains(x, y);
5269
}
70+
71+
public void inputData(String s) {
72+
fromPoint.offer(s);
73+
this.setDisplayingText(s);
74+
}
5375
}

src/paintcomponents/PaintComponent.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public PaintComponent(int x, int y) {
4242
this.y = y;
4343
}
4444

45+
/**
46+
* Paints this component using a graphics object
47+
* @param g
48+
*/
4549
public void paint(Graphics g){
4650
if(selected){
4751
paintSelected(g);
@@ -51,23 +55,45 @@ public void paint(Graphics g){
5155

5256
}
5357

58+
/**
59+
* Paint the non-select version of this paint object
60+
* @param g
61+
*/
5462
protected abstract void paintNotSelected(Graphics g) ;
5563

5664

65+
/**
66+
* Paints the selected version of this paint object
67+
* @param g
68+
*/
5769
protected abstract void paintSelected(Graphics g) ;
5870

5971

72+
/**
73+
* Set the state of this object to be selected state
74+
*/
6075
public void select(){
6176
selected = true;
6277
}
78+
79+
/**
80+
* Set the state of this object to be unselected
81+
*/
6382
public void deselect(){
6483
selected = false;
6584
}
6685

86+
/**
87+
* toggle the selection status of this paintable object
88+
*/
6789
public void toggleSelect() {
6890
selected = !selected;
6991
}
7092

93+
/**
94+
* Check if this object is selected
95+
* @return
96+
*/
7197
public boolean isSelected(){
7298
return selected;
7399
}

src/paintcomponents/TextPaintComponent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ protected void paintNotSelected(Graphics g) {
4242
//draw string starts from bottom left corner, shift to top left
4343
g.drawString(displayingText, getX(), (int) (getY() + bounds.getHeight()));
4444
}
45+
//IMPORTANT :: THIS COMPONENT IS SHIFTED DOWN, WHICH CAUSES PROBLEMS. WHEN FIXING COORDINATES,
46+
//WE SHOULD COME UP WITH A COMMON SOLUTION
4547

4648
@Override
4749
protected void paintSelected(Graphics g) {
48-
paintNotSelected(g);
50+
bounds = ((Graphics2D)g).getFontMetrics().getStringBounds(displayingText,g);
51+
52+
//draw string starts from bottom left corner, shift to top left
53+
g.drawString(displayingText, getX(), (int) (getY() + bounds.getHeight()));
4954

5055
}
5156

0 commit comments

Comments
 (0)