Sample program to draw text in J2ME
By: Abinaya
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);
}
...
}
Archived Comments
1. How to draw a thick circle in midlet?
View Tutorial By: masi at 2011-12-01 11:47:00
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
Client Server in J2ME (Socket Programming sample)
Datagrams in J2ME (UDP Programming sample)
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
Using List to create a Menu and Menu items in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Timer and TimerTask example in J2ME