public class TextRenderer
extends java.lang.Object
You can find a complete game API sample named 'Scape' in the TotalCross examples folder.
Here is some sample code:
import totalcross.game.*;
import totalcross.util.props.*;
...
public class Ping extends GameEngine {
// 2 text renderers to quickly display level and score values
private TextRenderer levelRenderer;
private TextRenderer scoreRenderer;
//---------------------------------------------------------
// overload the API's game init event.
// this function is called when the game is launched.
//---------------------------------------------------------
public void onGameInit() {
// create two text renderers, one for the 'level' and one for the 'score'
// we use the current font, the text should be black on the game background
// level display has 2 digits: max value is 99
levelRenderer=createTextRenderer(getFont(),Color.BLACK,"level:",2);
// score display has 5 digits: max value is 99999
scoreRenderer=createTextRenderer(getFont(),Color.BLACK,"score:",5);
}
//---------------------------------------------------------
// overload TotalCross' onPaint function.
//---------------------------------------------------------
public void onPaint(Graphics gfx) {
...
if (gameIsRunning) { // game engine's running state
...
// display the game level & score, both enable transparency
levelRenderer.display(15,2,level,true);
scoreRenderer.display(80,2,score,true);
...
}
...
}| Modifier and Type | Field and Description |
|---|---|
protected int |
fmH
height in pixels of the drawing (= font metrics height).
|
protected int |
textWidth
width in pixels of the text.
|
protected boolean |
zeroPadding
enable left zero padding.
|
| Modifier | Constructor and Description |
|---|---|
protected |
TextRenderer(Font font,
int foreColor,
int backColor,
java.lang.String text,
int maxDigits)
Creates a text renderer with the given parameters.
|
protected |
TextRenderer(Font font,
int foreColor,
int backColor,
java.lang.String text,
int maxDigits,
boolean zeroPadding)
Creates a text renderer with the given parameters.
|
| Modifier and Type | Method and Description |
|---|---|
void |
display(int x,
int y,
boolean transparent)
Display a text rendering.
|
void |
display(int x,
int y,
int value)
Display a text rendering.
|
int |
getHeight()
Returns the height of the current font
|
int |
getWidth()
Returns the maximum width of the text plus the number of digits
|
protected int fmH
protected int textWidth
protected boolean zeroPadding
protected TextRenderer(Font font, int foreColor, int backColor, java.lang.String text, int maxDigits, boolean zeroPadding) throws ImageException
font - to useforeColor - text foreground colorbackColor - text background colortext - to be displayed before the digits (or null if none)maxDigits - the number of digits to display. E.g.: 4 means the max value shown will be 9999.zeroPadding - pad with leading zeros.ImageExceptionprotected TextRenderer(Font font, int foreColor, int backColor, java.lang.String text, int maxDigits) throws ImageException
font - to useforeColor - text foreground colorbackColor - text background colortext - to be displayed before the digits (or null if none)maxDigits - the number of digits to display. E.g.: 4 means the max value shown will be 9999.ImageExceptionpublic void display(int x,
int y,
boolean transparent)
x - position.y - position.transparent - should the background be preserved.public void display(int x,
int y,
int value)
x - position.y - position.value - positive integer value to draw next to the text.public int getWidth()
public int getHeight()