-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextSprite.java
More file actions
39 lines (33 loc) · 1 KB
/
TextSprite.java
File metadata and controls
39 lines (33 loc) · 1 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
import java.awt.*;
public class TextSprite extends Sprite
{
private String m_text;
private int textHeight;
TextSprite(Canvas p, String s)
{
super(p);
m_text = s;
String[] outputs = m_text.split("\n");
textHeight = p.getFontMetrics(new Font("monospaced", Font.PLAIN, 12)).getHeight();
m_size.height = outputs.length * textHeight;
m_size.width = p.getFontMetrics(new Font("monospaced", Font.PLAIN, 12)).stringWidth(outputs[0]);
}
protected void paint(Graphics2D g2d)
{
Point p = getLocation();
if (p != null) {
g2d.setColor(getColor());
String[] outputs = m_text.split("\n");
for(int i=0; i<outputs.length; i++)
{
g2d.drawString(outputs[i], p.x, p.y + i*textHeight);
}
}
}
public Rectangle getBounds()
{
return new Rectangle(m_location.x,m_location.y - textHeight,
m_size.width,
m_size.height);
}
}