Sample program to draw text in J2ME

By: Abinaya Emailed: 1588 times Printed: 2038 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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);
}
...
}


J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

How to draw a thick circle in midlet?

View Tutorial          By: masi at 2011-12-01 11:47:00

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Abinaya
How to compile a Java program - javac
The clone() Method in Java
Data Types in Java
JSP Example to connect to MS SQL database and retrieve records
faces-config.xml to DirectTraffic in the JSF Application
Enabling Expression Language Evaluation in JSP
Using malloc() Function in C
ActionErrors and ActionError in Struts
Open, Creat, Close, Unlink system calls sample program in C
Structures and Functions in C
Introduction to JSP expression language
Multi-dimensional Arrays in C (Explained using date conversion program)
A C program similar to grep command in UNIX
Type Conversions in C (String to Integer, isdigit() etc)
Basics of C

More Tutorials in J2ME
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
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
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
POST UTF-8 encoded data to the server in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using List to create a Menu and Menu items in J2ME
Using alerts and tickers in J2ME
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
List of GPRS Access points for all countries

More Latest News
Most Viewed Articles (in J2ME )
Sample Java program shows how to Read a file over a network using J2ME midlet
A sample J2ME Auto-resume program.
Y.S. Sun Green Building Research Center
A sample J2ME midlet that shows an example of using a Timer and a TimerTask.
DateField sample program in J2ME
Internationalization Support in J2ME MIDP
List of GPRS Access points for all countries
J2ME Canvas sample to show games programming in J2ME
Sample J2ME code that shows various functionality of RMS.
Your first J2ME program and a midlet lifecycle explained.
java.lang.SecurityException: MIDP lifecycle does not support system exit.
How to get the CLDC and MIDP version from a J2ME program
Image and ImageItem sample program in J2ME
Sample program to draw text in J2ME
RMS Basics in J2ME
Most Emailed Articles (in J2ME)
How to load J2ME applications to the IDEN handsets
'LINK.EXE' is not recognized as an internal or ext
Sample Java program shows how to Read a file over a network using J2ME midlet
Y.S. Sun Green Building Research Center
Ticker sample program in J2ME
Image and ImageItem sample program in J2ME
RMS Basics in J2ME
Internationalization Support in J2ME MIDP
Using List to create a Menu and Menu items in J2ME
Download a file over a network in J2ME midlet
Sample J2ME code that shows various functionality of RMS.
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
DateField sample program in J2ME