Skip to content

Commit 655f8c6

Browse files
committed
Add Text Component
1 parent ea762e3 commit 655f8c6

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

src/actions/AddTextBoxAction.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package actions;
2+
3+
import javax.swing.JOptionPane;
4+
5+
import paintcomponents.TextPaintComponent;
6+
import ui.PaintPanel;
7+
8+
public class AddTextBoxAction extends PaintAction {
9+
10+
public AddTextBoxAction(PaintPanel panel) {
11+
super(panel);
12+
}
13+
14+
@Override
15+
public boolean canPerformAction() {
16+
return true;
17+
}
18+
19+
@Override
20+
public void performAction() {
21+
String s = JOptionPane.showInputDialog("Please enter the text to display");
22+
panel.addPaintComponent(new TextPaintComponent(s, panel.getWidth() / 2, panel.getHeight()/2));
23+
panel.repaint();
24+
}
25+
26+
@Override
27+
public String locationString() {
28+
return "Add/Text Box...";
29+
}
30+
31+
}

src/actions/PaintAction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ public PaintAction(PaintPanel panel){
1111
}
1212

1313
public abstract boolean canPerformAction();
14+
/**
15+
* Performs this action
16+
* Subclassess must invoke panel.repaint if the action changes the panel
17+
*/
1418
public abstract void performAction();
19+
1520
public abstract String locationString();
1621

1722
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.swing.JMenuBar;
88
import javax.swing.JMenuItem;
99

10+
import actions.AddTextBoxAction;
1011
import actions.ConstructLineSegmentAction;
1112
import actions.GeneratePolygonSourceJava;
1213
import actions.PaintAction;
@@ -18,6 +19,7 @@ public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
1819
public ActionsMenuBar(PaintPanel panel){
1920
addAction(new GeneratePolygonSourceJava(panel));
2021
addAction(new ConstructLineSegmentAction(panel));
22+
addAction(new AddTextBoxAction(panel));
2123

2224
}
2325

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package paintcomponents;
2+
3+
import java.awt.Graphics;
4+
import java.awt.Graphics2D;
5+
import java.awt.Rectangle;
6+
import java.awt.geom.Rectangle2D;
7+
8+
9+
public class TextPaintComponent extends PaintComponent{
10+
11+
public String displayingText;
12+
private Rectangle2D bounds;
13+
14+
/**
15+
* @return the displayingText
16+
*/
17+
public String getDisplayingText() {
18+
return displayingText;
19+
}
20+
21+
/**
22+
* @param displayingText the displayingText to set
23+
*/
24+
public void setDisplayingText(String displayingText) {
25+
this.displayingText = displayingText;
26+
}
27+
28+
public TextPaintComponent(String displayingText, int x, int y) {
29+
super(x, y);
30+
this.displayingText = displayingText;
31+
}
32+
33+
34+
@Override
35+
protected void paintNotSelected(Graphics g) {
36+
bounds = ((Graphics2D)g).getFontMetrics().getStringBounds(displayingText,g);
37+
38+
//draw string starts from bottom left corner, shift to top left
39+
g.drawString(displayingText, getX(), (int) (getY() + bounds.getHeight()));
40+
}
41+
42+
@Override
43+
protected void paintSelected(Graphics g) {
44+
paintNotSelected(g);
45+
46+
}
47+
48+
@Override
49+
public boolean contains(int x2, int y2) {
50+
51+
return new Rectangle(getX(), getY(),(int) bounds.getWidth(), (int)bounds.getHeight()).contains(x2, y2);
52+
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)