-
Notifications
You must be signed in to change notification settings - Fork 1
/
Text.java
73 lines (63 loc) · 2.22 KB
/
Text.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.font.FontRenderContext;
import java.awt.Graphics2D;
/**
* This class provides objects that show text on the screen; in the current
* demo, this is really only used to display the number of "sun points" that
* the player has.
*
* NOTE: This was adapted from the Text class as shown
* in the book-examples/ch10/marbles scenario
*
* @author: bcanada
* @version: 2014.01.17
*
*/
public class Text extends Actor
{
/**
* One-arg constructor to specify the length of the field,
* with text added later
*
* @param length the length of the text field (number of characters)
*/
public Text(int length)
{
setImage(new GreenfootImage(length * 12, 16));
} // end one-arg overloaded Text constructor (with int parameter)
/**
* One-arg constructor that calls the ABOVE constructor and
* then immediately sets the text to the specified String paramter
*
* @param text a String containing the text to be displayed in the object
*/
public Text(String text)
{
// call the other constructor
this (text.length());
setText(text);
} // end one-arg overloaded Text constructor (with String parameter)
/**
* Set the text of an existing Text object to the specified
* String parameter
*
* @param text a String containing the text to be displayed in the object
*/
public void setText(String text)
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text, 2, 12);
/* calculate width of text in pixels */
// FontRenderContext frc = ((Graphics2D)getImage().getAwtImage().getGraphics()).getFontRenderContext();
// int textWidth = (int)image.getFont().getStringBounds(text, frc).getWidth();
} // end method setText
/**
* Adapt location to make placement left-justified. Uses the setLocation
* method of the Actor superclass.
*/
public void setLocation(int x, int y)
{
super.setLocation(x + getImage().getWidth() / 2, y);
} // end method setLocation
} // end class Text