Sample program to draw text in J2ME

By: Abinaya Printer Friendly Format    


To draw text, specify the anchor point and the weighting of the anchor point. You can also specify the font of the text to be drawn.

This demo chooses to space the text strings "Default," "Large," "Medium," and "Small" by positioning the baselines of the bounding rectangles. The text is also "left-justified." Notice that the logical OR of the horizontal and vertical anchor policies (LEFT | BOTTOM) specify the anchor position.

The two strings "BOLD" and "VERTICAL" are drawn vertically simply by positioning individual characters using the drawChar() method. They are offset from the right edge of the display. Using the RIGHT anchor policy, the code calculates the position of the right edge of the bounding rectangles by subtracting some number of pixels from the display's rightmost pixel coordinate.

The Graphics API also defines another constant, VCENTER, that is valid only for specifying the vertical anchor policy for positioning images. It is invalid for text. VCENTER stipulates that the vertical center of the image should be placed at the (x, y) coordinate point. 

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
Displays some text "drawn" on a Canvas. Demonstrates the use of
the Graphics text drawing routines.
@see javax.microedition.lcdui.Graphics
*/
public class TextDemo extends Canvas
implements CommandListener
{
...
public void paint(Graphics g)
{
paintClipRect(g);
int width = getWidth();
int height = getHeight();
g.setFont(Font.getDefaultFont());
g.drawString("Default", 5, 30,
Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_PLAIN,
Font.SIZE_LARGE));
g.drawString("Large", 5, 53,
Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_ITALIC,
Font.SIZE_MEDIUM));
g.drawString("Medium", 5, 71,
Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_UNDERLINED,
Font.SIZE_SMALL));
g.drawString("Small", 5, 90,
Graphics.LEFT | Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_BOLD,
Font.SIZE_MEDIUM));
g.drawString("V", width - 10, 20,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("E", width - 10, 32,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("R", width - 10, 44,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("T", width - 10, 56,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("I", width - 10, 68,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("C", width - 10, 80,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("A", width - 10, 92,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawString("L", width - 10, 104,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('B', width - 25, 20,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('O', width - 25, 32,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('L', width - 25, 44,
Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('D', width - 25, 56,
Graphics.RIGHT | Graphics.BOTTOM);
}
...
}



Ask a Question



Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)

Comment on this tutorial