Saturday, May 2, 2009

Custom Font in J2ME

Drawing strings in low level J2ME Application/Game is somewhat complicated. Though there is a easiest way to do that. That is called Custom Font.

Using this methodology you include any kind of PC Fonts in your application.

Steps to be followed:-

1. Create a Bitmap font Image based on your Charset. Your Charset may be in any Language.
Example:-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_/?"

2. Get each Character X_Offset & Width values & make an integer array.
Example:-

public int CHAR_X[] = {
0,7,13,19,26,33,39,45,53,57,61,69,72,82,89,96,103,110,115,122,127,134,140,149,156,162, 168,177,183,191,200,206,213,222,231,235,242,248,255,265,273,282,289,298,306,312,321,328,336,348,355,362,
368,376,383,391,396,405,412,418,426,432,
440,444,454,462,468,477,483,491,498,503,507,515,522
} ;

public int CHAR_W[] = {
5,5,6,6,6,4,6,6,2,2,6,2,9,6,6,6,6,4,5,4,5,5,9,6,6,6,
8,5,7,7,4,5,8,8,2,5,6,5,10,7,8,6,8,8,6,8,7,8,12,8,7,6,
7,5,7,5,7,6,5,7,5,6,2,
10,7,5,8,6,8,6,3,3,7,4,4
} ;

3. Integrate the above steps in the source code.


/////////////////////////////////////////////////////////////////////
Example Canvas class
/////////////////////////////////////////////////////////////////////
public class FontCanvas extends Canvas{

private int mWidth;
private int mHeight;
private Font mFont;

/** Creates a new instance of FontCanvas */
public FontCanvas() {
this.setFullScreenMode(true);
mFont = new Font();
this.mWidth = getWidth();
this.mHeight = getHeight();
}

protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, mWidth, mHeight);

//mFont.drawString(g, "Have a look at this Custom Font", 10, 10, 0);

mFont.setColor(0xFF0000);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 10, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 30, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 50, 0);

mFont.setColor(0x00AA00);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 80, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 100, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 120, 0);

mFont.setColor(0x0000FF);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 150, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 170, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 190, 0);

mFont.setColor(0x0);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 230, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 250, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 270, 0);

mFont.setColor(0xFF00FF);
mFont.drawString(g, "by Albert", mWidth-70, mHeight-14, 0);
}
}

/////////////////////////////////////////////////////////////////////
Example Font Class:-
/////////////////////////////////////////////////////////////////////
class Font
{
Image fontImage, forColorImage;
int fontHeight;

public Font()
{
try{
fontImage = Image.createImage("/font.png");
forColorImage = fontImage;
fontHeight = fontImage.getHeight();
}catch(Exception e){}
}

public String CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_/?";

public int CHAR_X[] = {
0,7,13,19,26,33,39,45,53,57,61,69,72,82,89,96,103,110,115,122,127,134,140,149,156,162,
168,177,183,191,200,206,213,222,231,235,242,248,255,265,273,282,289,298,306,312,321,328,336,348,355,362,
368,376,383,391,396,405,412,418,426,432,440,444,454,462,468,477,483,
491,498,503,507,515,522
} ;

public int CHAR_W[] = {
5,5,6,6,6,4,6,6,2,2,6,2,9,6,6,6,6,4,5,4,5,5,9,6,6,6,
8,5,7,7,4,5,8,8,2,5,6,5,10,7,8,6,8,8,6,8,7,8,12,8,7,6,
7,5,7,5,7,6,5,7,5,6,2,
10,7,5,8,6,8,6,3,3,7,4,4
} ;

public int convert_RGB_to_ARGB(int rgb) {
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb >> 0) & 0xFF;
return 0xff000000 | (r <<>
}

public void setColor(int c)
{
int w = fontImage.getWidth();
int h = fontImage.getHeight();
int len = w*h;
int temp[] = new int[len];
forColorImage.getRGB(temp, 0, w, 0, 0, w, h);
int color = convert_RGB_to_ARGB(c);
//System.out.println("COLOR: " + Integer.toHexString(color));
for(int i=0; i
if(temp[i] == -16711936)
temp[i] = color;
this.fontImage = fontImage.createRGBImage(temp, w, h, false);
temp=null;
}

public void drawString(Graphics g, String str, int x, int y, int anchor)
{
int cx = g.getClipX();
int cy = g.getClipY();
int cw = g.getClipWidth();
int ch = g.getClipHeight();

char chr[] = str.toCharArray();
int len = chr.length;
int c;
for(int i=0; i
{
if(chr[i] != ' ')
{
c = CHAR_SET.indexOf(""+chr[i]);
//System.out.println(i+":"+c + "fontHeight: " + fontHeight);

g.clipRect(x, y, CHAR_W[c], 14);
g.drawImage(fontImage, x-CHAR_X[c], y, Graphics.TOP | Graphics.LEFT);
g.setClip(cx, cy, cw, ch);

x += CHAR_W[c]+1;
}
else
x += 3;
}
g.setClip(cx, cy, cw, ch);
}
}

/////////////////////////////////////////////////////////////////////
Example Screen Shot
/////////////////////////////////////////////////////////////////////


Thursday, April 30, 2009

J2ME Packages

J2ME Configurations

JSR Specification

Acronym

Description

JSR 30

CLDC 1.0

Connected, Limited Device Configuration

JSR 139

CLDC 1.1

Connected, Limited Device Configuration 1.1

JSR 36

CDC

Connected Device Configuration

JSR 218

CDC 1.1

Connected Device Configuration 1.1


J2ME Profiles

JSR Specification

Acronym

Description

JSR 37

MIDP 1.0

Mobile Information Device Profile

JSR 118

MIDP 2.0

Mobile Information Device Profile 2.0

JSR 75

PDAP

PDA Profile

JSR 46

FP

Foundation Profile

JSR 219

FP 1.1

Foundation Profile 1.1

JSR 129

PBP

Personal Basis Profile

JSR 217

PBP 1.1

Personal Basis Profile 1.1

JSR 62

PP

Personal Profile

JSR 215

PP 1.1

Personal Profile 1.1

JSR 195

IMP

Information Module Profile

JSR 228

IMP-NG

Information Module Profile - Next Generation

J2ME Optional Packages

JSR Specification

Acronym

Description

JSR 75

PIM

PDA Optional Packages for the J2ME Platform(It supports File Connection & PIM-Personal Information Management.)

JSR 82

BTAPI

Java APIs for Bluetooth(It supports Bluetooth & Obex)

JSR 120

WMA

Wireless Messaging API(It supports Text & Binary message)

JSR 205

WMA 2.0

Wireless Messaging API 2.0(It also supports Multimedia message)

JSR 135

MMAPI

Mobile Media API(It supports Audio, Video, Volume control)

JSR 164

JAIN SIMPLE Presence

JSR 165

JAIN SIMPLE Instant Messaging

JSR 172

J2ME Web Services

JSR 177

SATSA

Security and Trust Services API for J2ME

JSR 179

Location API for J2ME

JSR 180

SIP

SIP API for J2ME

JSR 184

3D

Mobile 3D Graphics API for J2ME

JSR 186

JAIN Presence

JSR 187

JAIN Instant Messaging

JSR 190

Event Tracking API for J2ME

JSR 209

Advanced Graphics and User Interface Optional Package for J2ME Platform

JSR 211

CHAPI

Content Handling API

JSR 213

Micro WSCI Framework for J2ME

JSR 214

Micro BPSS for J2ME Devices

JSR 226

Scalable 2D Vector Graphics API

JSR 229

Payment API

JSR 230

Data Sync API

JSR 232

Mobile Operational Management

JSR 234

AMMS

Advanced Multimedia Supplements

JSR 238

Mobile Internationalization API

JSR 239

Java Bindings for OpenGL ES

JSR 246

Device Management API

JSR 253

MTA

Mobile Telephony API

RGB and ARGB in J2ME

RGB - It is a color combination value of Red Green Blue

Syntax: 0xRRGGBB

Example: -

0xFF0000 (or) 255, 0, 0 (Red color)

0x00FF00 (or) 0, 255, 0 (Green color)

0x0000FF (or) 0, 0, 255 (Blue color)

Red - the red component of the color being set in range 0-255

Green - the green component of the color being set in range 0-255

Blue - the blue component of the color being set in range 0-255


ARGB - It is a color combination value of Red Green Blue with Alpha channel

Syntax: 0xAARRGGBB

Example: -

0xFFFF0000 - Red color with white color background

0x00FF0000 - Red color with transparent background

Alpha – it specifies the opacity (transparency) of the pixel

[0x00 – Fully transparent and 0xFF – white background]

Red - the red component of the color being set in range 0-255

Green - the green component of the color being set in range 0-255

Blue - the blue component of the color being set in range 0-255


Convert RGB to ARGB

public int convert_RGB_to_ARGB(int rgb) {

int r = (rgb >> 16) & 0xFF;

int g = (rgb >> 8) & 0xFF;

int b = (rgb >> 0) & 0xFF;

return 0xff000000 | (r <<>

}

Convert ARGB to RGB

public int convert_ARGB_to_RGB(int argb) {

int a = (argb >> 24) & 0xFF;

int r = (argb >> 16) & 0xFF;

int g = (argb >> 8) & 0xFF;

int b = (argb >> 0) & 0xFF;

return (r <<>

}