Java Reference
In-Depth Information
public void drawChar(char character, int x, int y, int anchor)
public void drawChars(char[] data, int offset, int length,
int x, int y, int anchor)
public void drawString(String str, int x, int y, int anchor)
public void drawSubstring(String str, int offset, int len,
int x, int y, int anchor)
The following example shows how to place text at various places on a
Canvas
:
import javax.microedition.lcdui.*;
public class TextCanvas
extends Canvas {
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
g.setColor(0x000000);
// First label the four corners.
g.drawString("corner", 0, 0,
Graphics.TOP | Graphics.LEFT);
g.drawString("corner", w, 0,
Graphics.TOP | Graphics.RIGHT);
g.drawString("corner", 0, h,
Graphics.BOTTOM | Graphics.LEFT);
g.drawString("corner", w, h,
Graphics.BOTTOM | Graphics.RIGHT);
// Now put something in the middle (more or less).
g.drawString("Sin Wagon", w / 2, h / 2,
Graphics.BASELINE | Graphics.HCENTER);
}
}
To see this
Canvas
, you'll have to create a MIDlet that displays it. We suggest using
Pacer
;
just edit the source file so it instantiates a
TextCanvas
instead of a
PacerCanvas
. The finished
product is shown in Figure 13-5.
Note that
Canvas
denies us some real estate at the bottom of the screen. This is to allow
space for commands.
Canvas
, like any other
Displayable
, can display commands and have a
command listener.
Search WWH ::
Custom Search