Java Reference
In-Depth Information
You could draw a box around a string, for example:
import javax.microedition.lcdui.*;
public class BoxTextCanvas
extends Canvas {
private Font mFont;
public BoxTextCanvas() {
mFont = Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_PLAIN, Font.SIZE_LARGE);
}
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
g.setColor(0x000000);
String s = "dolce";
int stringWidth = mFont.stringWidth(s);
int stringHeight = mFont.getHeight();
int x = (w - stringWidth) / 2;
int y = h / 2;
g.setFont(mFont);
g.drawString(s, x, y, Graphics.TOP | Graphics.LEFT);
g.drawRect(x, y, stringWidth, stringHeight);
}
}
Drawing Images
The Graphics class contains a single method for drawing an image:
public void drawImage(Image img, int x, int y, int anchor)
The drawImage() method uses an anchor point, just like the anchor point in the text drawing
methods. The available anchor points are slightly different. BASELINE is no longer an option for
the vertical anchor point of an image, as the concept of baseline is specific to text. Instead,
VCENTER is an additional option for the vertical anchor point. Figure 13-7 shows the available
combinations of anchor points.
Search WWH ::




Custom Search