-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.java
More file actions
128 lines (113 loc) · 3.07 KB
/
Text.java
File metadata and controls
128 lines (113 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//HIDE
import javax.swing.*;
import java.awt.*;
public class Text implements Shape
{
private Color color = Color.BLACK;
private JLabel label = new JLabel();
private double x;
private double y;
private double xGrow;
private double yGrow;
/**
* Constructs a text at a given location.
* @param x the leftmost x-position of the shape
* @param y the topmost y-position of the shape
* @param message the text string
*/
public Text(double x, double y, String message)
{
this.x = x;
this.y = y;
label.setText(message);
}
/**
* Gets the leftmost x-position of the bounding box.
* @return the leftmost x-position
*/
public int getX()
{
return (int) Math.round(x - xGrow) ;
}
/**
* Gets the topmost y-position of the bounding box.
* @return the topmost y-position
*/
public int getY()
{
return (int) Math.round(y - yGrow);
}
/**
* Gets the width of the bounding box.
* @return the width
*/
public int getWidth()
{
return (int) Math.round(label.getPreferredSize().getWidth() + 2 * xGrow);
}
/**
* Gets the height of the bounding box.
* @return the height
*/
public int getHeight()
{
return (int) Math.round(label.getPreferredSize().getHeight() + 2 * yGrow);
}
/**
* Moves this text by a given amount.
* @param dx the amount by which to move in x-direction
* @param dy the amount by which to move in y-direction
*/
public void translate(double dx, double dy)
{
x += dx;
y += dy;
Canvas.getInstance().repaint();
}
/**
* Resizes this text both horizontally and vertically.
* @param dw the amount by which to resize the width on each side
* @param dw the amount by which to resize the height on each side
*/
public void grow(double dw, double dh)
{
xGrow += dw;
yGrow += dh;
Canvas.getInstance().repaint();
}
/**
* Sets the color for drawing this text.
* @param newColor the new color
*/
public void setColor(Color newColor)
{
color = newColor;
Canvas.getInstance().repaint();
}
/**
* Shows this text on the canvas.
*/
public void draw()
{
Canvas.getInstance().show(this);
}
public String toString()
{
return "Text[x=" + getX() + ",y=" + getY() + ",message=" + label.getText() + "]";
}
public void paintShape(Graphics2D g2)
{
if (color != null)
{
label.setForeground(new Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue()));
Dimension dim = label.getPreferredSize();
if (dim.width > 0 && dim.height > 0)
{
label.setBounds(0, 0, dim.width, dim.height);
g2.translate(getX(), getY());
g2.scale(getWidth() / dim.width, getHeight() / dim.height);
label.paint(g2);
}
}
}
}