Skip to content

Commit 02b4ffd

Browse files
committed
Add Rectangle Class, Modify DataInputComponent Class, Add Defaults for Rectangle
1 parent dbb4f88 commit 02b4ffd

File tree

3 files changed

+117
-8
lines changed

3 files changed

+117
-8
lines changed

src/paintcomponents/DataInputTextfieldPaintComponent.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,50 @@
1010
public class DataInputTextfieldPaintComponent extends TextPaintComponent {
1111

1212
private static final int HORIZONTAL_OFFSET = 10;
13-
DataFromPoint<String> fromPoint;
14-
Color boundColor;
15-
Color selectedColor;
13+
private DataFromPoint<String> fromPoint;
14+
private RectanglePaintComponent rect;
15+
private Color defaultColor;
16+
private Color selectedColor;
1617

1718

1819
public DataInputTextfieldPaintComponent(String displayingText, int x,
1920
int y) {
2021
super(displayingText, x, y);
2122
fromPoint = new DataFromPoint<>(x - 50, y);
22-
boundColor = Defaults.sharedDefaults().defaultColorForDataInputTextfield();
23+
defaultColor = Defaults.sharedDefaults().defaultColorForDataInputTextfield();
2324
selectedColor = Defaults.sharedDefaults().defaultColorForSelectedDataInputTextfield();
2425
}
2526

2627

2728

2829
@Override
2930
protected void paintNotSelected(Graphics g) {
30-
g.setColor(boundColor);
31+
g.setColor(defaultColor);
3132
((Graphics2D)g).setStroke(new BasicStroke(1));
3233
super.paintNotSelected(g);
3334
updateFromPointPosition();
34-
g.drawRect(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
35+
updateAndPaintBoudingRectangle(g);
3536
fromPoint.paintNotSelected(g);
3637

3738
}
3839

3940

4041
@Override
4142
protected void paintSelected(Graphics g) {
42-
((Graphics2D)g).setStroke(new BasicStroke(1));
4343
g.setColor(selectedColor);
44+
((Graphics2D)g).setStroke(new BasicStroke(1));
4445
super.paintSelected(g);;
4546
updateFromPointPosition();
46-
g.drawRect(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
47+
updateAndPaintBoudingRectangle(g);
4748
fromPoint.paintSelected(g);
4849
}
50+
51+
private void updateAndPaintBoudingRectangle(Graphics g){
52+
rect = new RectanglePaintComponent(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
53+
//select rectangle according to current select status
54+
if(isSelected())rect.select(); else rect.deselect();
55+
rect.paint(g);
56+
}
4957

5058
/**
5159
* This method will use the protected bounds, which will be updated in super.paint[Not]Selected.
@@ -72,4 +80,5 @@ public void inputData(String s) {
7280
fromPoint.offer(s);
7381
this.setDisplayingText(s);
7482
}
83+
7584
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
*
3+
*/
4+
package paintcomponents;
5+
6+
import java.awt.Color;
7+
import java.awt.Graphics;
8+
import java.awt.Rectangle;
9+
10+
import settings.Defaults;
11+
12+
/**
13+
* @author chenzb
14+
*
15+
*/
16+
public class RectanglePaintComponent extends PaintComponent {
17+
//TODO Maybe delegate this class to java.awt.Rectangle????
18+
19+
private int width;
20+
private int height;
21+
22+
private Color nonSelectColor;
23+
private Color selectColor;
24+
25+
/**
26+
* @return the width
27+
*/
28+
public int getWidth() {
29+
return width;
30+
}
31+
32+
/**
33+
* @param width the width to set
34+
*/
35+
public void setWidth(int width) {
36+
this.width = width;
37+
}
38+
39+
/**
40+
* @return the height
41+
*/
42+
public int getHeight() {
43+
return height;
44+
}
45+
46+
/**
47+
* @param height the height to set
48+
*/
49+
public void setHeight(int height) {
50+
this.height = height;
51+
}
52+
53+
/**
54+
* @param x
55+
* @param y
56+
*/
57+
public RectanglePaintComponent(int x, int y, int width, int height) {
58+
super(x, y);
59+
this.width = width;
60+
this.height = height;
61+
this.nonSelectColor = Defaults.sharedDefaults().defaultColorForRectanglePaintComponent();
62+
this.selectColor = Defaults.sharedDefaults().defaultColorForSelectedRectanglePaintComponent();
63+
}
64+
65+
/* (non-Javadoc)
66+
* @see paintcomponents.PaintComponent#paintNotSelected(java.awt.Graphics)
67+
*/
68+
@Override
69+
protected void paintNotSelected(Graphics g) {
70+
g.setColor(nonSelectColor);
71+
g.drawRect(getX(), getY(), width, height);
72+
73+
}
74+
75+
/* (non-Javadoc)
76+
* @see paintcomponents.PaintComponent#paintSelected(java.awt.Graphics)
77+
*/
78+
@Override
79+
protected void paintSelected(Graphics g) {
80+
g.setColor(selectColor);
81+
g.drawRect(getX(), getY(), width, height);
82+
83+
}
84+
85+
/* (non-Javadoc)
86+
* @see paintcomponents.PaintComponent#contains(int, int)
87+
*/
88+
@Override
89+
public boolean contains(int x2, int y2) {
90+
return new Rectangle(getX(), getY(), getWidth(), getHeight()).contains(x2, y2);
91+
}
92+
93+
}

src/settings/Defaults.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ public Color defaultColorForDataInputTextfield() {
6262
public Color defaultColorForSelectedDataInputTextfield(){
6363
return DATA_INPUT_TEXTFIELD_SELECTED_COLOR;
6464
}
65+
public Color defaultColorForRectanglePaintComponent() {
66+
return DATA_INPUT_TEXTFIELD_COLOR;
67+
}
68+
69+
public Color defaultColorForSelectedRectanglePaintComponent(){
70+
return DATA_INPUT_TEXTFIELD_SELECTED_COLOR;
71+
}
6572
}

0 commit comments

Comments
 (0)