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(); } }
相关文章推荐
- J2ME简单灵活实现手机中自动换行显示文本
- j2me在Canvas上绘制大量文本自动换行的实现
- CSS控制文本自动换行
- HTML5 canvas 文本自动换行
- gridview大文本自动换行显示
- 自定义view,实现文本自动换行
- 在指定区域内输出具备自动换行的文本
- BGRABitmap图像操作12:文本自动换行
- span 文本内容超过宽度自动换行
- CSS样式控制超长文本在div,p等块级容器中的自动换行
- C# WinForm 中控件(Label,Button等) 文本自动换行 解决方法
- grid行中单元格文本自动处理换行
- javascript 一个自定义长度的文本自动换行的函数
- 文本自动换行问题
- CSS控制文本自动换行
- 微信小程序 view的文本不能自动换行问题
- iOS-textView文本换行高度自动适应
- HTML有趣的文本不自动换行问题
- css:设置文本不自动换行
- 在IFrame中实现文本的自动换行