您的位置:首页 > 其它

J2ME 用来处理手机游戏中长字符串显示问题,自己写的,仅作参考

2008-07-17 00:34 531 查看
我自己写的一个 类 ···用来处理文字显示,针对手机游戏对话显示,含有对话自动分页显示

/*
* TalkChar.java
*
* Created on 2007年7月24日, 上午11:25
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;

/**
*
* @author victor
*/
public class TalkChar {
Font font=Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,Font.SIZE_SMALL);
StringBuffer page;
StringBuffer[] childString;
int talkAreaWidth;
int talkAreaHeight;
int x;//文字启始点X
int rowFontNum;//每行文字个数
int TotalRow;//每页绘制多少行
char[][] chars;
char tempChars;
StringBuffer[] stringBuff;
GameCanvas gc;
Graphics g;
int fontH;//字体大小 由于是用来处理中文,中文的宽和高基本一样 故用1个参数代替宽和高
int imageX;
int imageY;
boolean change;
public static boolean isOver;
/** Creates a new instance of TalkChar */
public TalkChar(StringBuffer page,int talkAreaWidth,int talkAreaHeight,int fontH,GameCanvas gc,Graphics g) {
this.page=page;//需要表现的字符串缓冲,为了提高程序效率,代码采用drawcChar方式显示字符,在字符串过长的情况下自动分页
this.fontH=fontH;
this.gc=gc;
this.g=g;
System.out.println(this.imageX+" "+this.imageY);
this.talkAreaWidth=talkAreaWidth;//显示区域大小
this.talkAreaHeight=talkAreaHeight;
rowFontNum=talkAreaWidth/fontH-2;//每行留2个字符空间
TotalRow=2;//每页绘制多少行
System.out.println("字体请保证为:"+rowFontNum*TotalRow+"个字的整数倍 "+fontH);
stringBuff=new StringBuffer[((page.length()/(rowFontNum*TotalRow))+1)];
// System.out.println(((page.length()ength/(rowFontNum*TotalRow))+1)+" char length "+chars.length);
for(int j=0;j<stringBuff.length;j++){
stringBuff[j]=new StringBuffer();
}
for(int i=0;i<(page.length()/(rowFontNum*TotalRow))+1;i++){
// System.out.println("stringBuffer长度"+((page.length()/(rowFontNum*TotalRow))+1));
if(i<(page.length()/(rowFontNum*TotalRow))){
stringBuff[i]=new StringBuffer(page.toString().substring(rowFontNum*TotalRow*i,(i+1)*(rowFontNum*TotalRow)));
// System.out.println( page.toString().substring(rowFontNum*TotalRow*i,(i+1)*(rowFontNum*TotalRow)));
}else{
stringBuff[i]=new StringBuffer(page.toString().substring(rowFontNum*TotalRow*i,page.length()));
//System.out.println( page.toString().substring(rowFontNum*TotalRow*i,page.length()));
}
}
}

public StringBuffer SBuffer(int num){
return stringBuff[num];
}

//将字符串分离成单页显示的子字符串
public StringBuffer[] SChildBuffer(int num2){
StringBuffer[] sbf=new StringBuffer[TotalRow];
for(int i=0;i<sbf.length;i++){
sbf[i]=new StringBuffer();
if(i<sbf.length-1){
sbf[i]=new StringBuffer(this.SBuffer(num2).toString().substring(i*rowFontNum,(i+1)*rowFontNum));
}else{
sbf[i]=new StringBuffer(this.SBuffer(num2).toString().substring(i*rowFontNum,this.SBuffer(num2).length()));
}
}
//System.out.println();
return sbf;
}

public int getrowFontNum(){
return rowFontNum;
}

public int getTotalRow(){
return TotalRow;
}

public int getTalkPageNum(){
return ((page.length()/(rowFontNum*TotalRow))+1);
}

public void paint(Graphics g){
if(TestCanvas.talkPage<this.getTalkPageNum()){
for(int i=0;i<this.getTotalRow();i++){
childString[i]=this.SChildBuffer(TestCanvas.talkPage)[i];
// System.out.println("talkPage="+talkPage+" childString["+i+"]="+childString[i].toString());
System.out.println("talkPage: "+ TestCanvas.talkPage);
}

for(int i=0;i<this.getTotalRow();i++){
for(int j=0;j<childString[i].length();j++){
chars[i][j]=childString[i].charAt(j);
if(j>childString[i].length()){
change=true;
}
g.drawChar(chars[i][j],TestCanvas.imageX+j*fontH+23-talkAreaWidth/2,TestCanvas.imageY+i*font.getHeight()+10+5*i-talkAreaHeight/2,Graphics.HCENTER|Graphics.TOP);
}
}
}else{
isOver=true;
System.out.println("Over Now");
}
}

}

----------------------调用演示------------------------------------------------------------
/*
* TestCanvas.java
*
* Created on 2007年7月24日, 上午11:27
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;
import java.io.IOException;
import java.lang.*;
/**
*
* @author victor
*/
public class TestCanvas extends GameCanvas implements Runnable,CommandListener{
Font font=Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,Font.SIZE_SMALL);
/** Creates a new instance of TestCanvas */
Graphics g=this.getGraphics();
Display display;
TalkTest midlet;
Command cmdBack=new Command("返回",Command.BACK,2);
public Image talkImage=null;
Image person=null;
int imageWidth;
int imageHeight;
public static int imageX;
public static int imageY;
TalkChar talkChar;
public static int talkPage=0;
public TestCanvas(Display display,TalkTest midlet) {
super(true);
this.display=display;
this.midlet=midlet;
System.out.println("进入Canvas");
try{
talkImage=Image.createImage("/dhk2.png");
person=Image.createImage("/userpic1.png");
}catch(IOException e){e.printStackTrace();}
imageWidth=talkImage.getWidth();//取得背景图片大小
imageHeight=talkImage.getHeight();
System.out.println("W "+imageWidth+" H "+imageHeight+" font "+font.getHeight());
String name;
name="一坨优质牛粪";
String s1="啊 她无论打扮装束,都是淡雅可人,予人庄重矜持的印象,可是那双含情脉脉的明媚秀眸,配合着她宛若与生俱来略带羞涩的动人神态,却没有多少个男人能抵御得了. ";
String s2="["+name+"]:"+s1;
StringBuffer page=new StringBuffer(s2);
//System.out.println("现有字体:"+page.length()+"个字");
talkChar=new TalkChar(page,imageWidth,imageHeight,font.getHeight(),this,g);
Thread t=new Thread(this);
t.start();
}

public void run(){
//线程开始
while(true){
this.input();
this.repaint();

try{
Thread.sleep(800);
}catch(InterruptedException e){}
}
}

public void paint(Graphics g){
g.setFont(font);
g.setColor(0,0,0);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.drawImage(talkImage,this.getWidth()/2,this.getHeight()/2,Graphics.HCENTER|Graphics.VCENTER); //绘制背景图片
this.setImageX(this.getWidth()/2);
this.setImageY(this.getHeight()/2);
g.drawImage(person,imageX-90,imageY-55,Graphics.HCENTER|Graphics.VCENTER);
g.setColor(255,255,255);
talkChar.paint(g);
}

public void input(){
int keyState=this.getKeyStates();
// System.out.println( childString[0].toString());
if((keyState&this.DOWN_PRESSED)!=0){
System.out.println("按下任意键");
TestCanvas.talkPage+=1;
}

}

public void commandAction(Command cmd,Displayable displaya){
if(cmd==cmdBack){
this.midlet.exit();
}
}

public void setImageX(int x){
this.imageX=x;
}

public void setImageY(int y){
this.imageY=y;
}

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