您的位置:首页 > 其它

j2me文本自动换行

2010-12-14 19:46 211 查看


j2me文本自动换行

一、Midlet框架

import javax.microedition.lcdui.Display;

import javax.microedition.midlet.MIDlet;

import javax.microedition.midlet.MIDletStateChangeException;

import demo.MainCanvas;

public class AutoAjustMidlet extends MIDlet {

public MainCanvas m_MainCanvas;

public AutoAjustMidlet() {

super();

}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

m_MainCanvas.Stop();

}

protected void pauseApp() {

m_MainCanvas.Stop();

}

protected void startApp() throws MIDletStateChangeException {

m_MainCanvas = new MainCanvas();

Display.getDisplay(this).setCurrent(m_MainCanvas);

}

}

二、辅助类

package demo;

import javax.microedition.lcdui.Font;

import javax.microedition.lcdui.Graphics;

public class TextAjust {

//功能: 		计算需要换行的位置

//str:		需要显示的文字

//font:		文字的字体

//linewd:	每行的宽度限制

static public int ChangLine(String str, Font font, int linewd )

{

int wd = 0;

char ch;

for (int i = 0; i < str.length(); i++)

{

ch = str.charAt(i);

if (ch == '/n')

return i + 1;

wd += font.charWidth(ch);

if (wd > linewd)

return i;

}

return 0;

}

//功能: 		分行显示字符串

//g:		当前显示的缓冲

//strText:	显示的字符串

//linewd:	每行的宽度限制

//x,y:		字符串左上角显示的位置

//yDis:     显示文字时每行间隔的距离

static public void AjustDrawString( Graphics g, String strText,

int linewd, int x, int y, int yDis )

{

String subStr;

int nPos; //需要换行的位置

while (true)

{

nPos = ChangLine(strText, g.getFont(), linewd );

if (nPos == 0)

{

g.drawString( strText, x, y, 0);

break;

}

else

{

if (strText.charAt(nPos - 1) == '/n' )

subStr = strText.substring(0, nPos - 1);

else

subStr = strText.substring(0, nPos);

g.drawString( subStr, x, y, 0);

strText = strText.substring(nPos, strText.length());

y = y + yDis;

}

}

}

}

三、GameCanvas框架:

package demo;

import javax.microedition.lcdui.Font;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.game.GameCanvas;

public class MainCanvas extends GameCanvas implements Runnable{

private boolean	m_bRunning;				//控制项目运行

public int	m_nWidth = 50;					//显示宽度

public Font m_Font;							//存储字体

public MainCanvas(){

super(true);

//创建字体,如果创建失败,则采用系统默认字体

try{

m_Font 	= Font.getFont(Font.FACE_SYSTEM,

Font.STYLE_PLAIN, Font.SIZE_LARGE );

}catch(Exception e){

m_Font  = Font.getDefaultFont();

}

Start();								//启动线程

}

public void Start(){

m_bRunning = true;

Thread thread = new Thread(this);		//分配新线程

thread.start();							//线程启动

}

public void run() {						//新线程自动调用此方法

//获得系统当前时间,并将时间换算成毫秒

long T1 = System.currentTimeMillis();

long T2 = T1;

while(m_bRunning){

T2 = System.currentTimeMillis();

if( T2 - T1 > 100 ){			//间隔100毫秒

T1 = T2;

Input();

Logic();

Paint();

}

}

}

public void Stop(){						//终止游戏

m_bRunning = false;

}

public void Input(){

int keyStates = getKeyStates();			//获取当前按键状态

//如果按下方向键的上键,则调整显示宽度

if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 )

m_nWidth = m_nWidth +10;

//如果按下方向键的下键,则调整显示宽度

if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 )

m_nWidth = m_nWidth -10;

}

public void Logic(){

}

public void Paint(){

Graphics g = getGraphics();

//用黑色清屏

g.setColor(0x00000000);

g.fillRect( 0, 0, getWidth(), getHeight() );

g.setFont(m_Font);

g.setColor(0x00FFFFFF);

TextAjust.AjustDrawString(g,

"我要自动换行我要自动换行我要自动换行我要自动换行",

m_nWidth, 20, 20, 15 );

flushGraphics();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息