应网友要求,写个RPG人物对话的片断 |
以下是代码:
MIDP1.0测试通过
自己写代码并不专业,请大家评评,修改修改。
import javax.microedition.lcdui.*;public class Mi extends javax.microedition.midlet.MIDlet{ Gc canvas; public void startApp(){ System.out.print("玄远科技 "); canvas = new Gc(); javax.microedition.lcdui.Display.getDisplay(this).setCurrent(canvas); canvas.doStart(); } public void pauseApp() {} public void destroyApp(boolean a) {canvas.doExit();} }
/** * messageClass * * @author shaNa * * @version 1.0 */ class Gc extends Canvas implements Runnable{ //基本 public static int GCW,GCH; public Image dbImage; public static Graphics g; public boolean isRun; public Thread thr; //消息 public static String msg; public static byte page; public static boolean isShowMsg; public static String msgEnd = "^"; public static byte rowValue=3;//行数
public static byte textValue=24;//字数 //font public static Font font = Font.getFont(0,0,0);; public static int STRW=font.stringWidth("青"); public static int STRH=font.getHeight(); public static String sEnd = "/"; //msg public Gc(){ GCW=getWidth();GCH=getHeight(); dbImage = Image.createImage(GCW,GCH); g = dbImage.getGraphics(); iniTxt(); } public void iniTxt(){
formatMsg("刹那:祝大家新年快乐、万事如意、财源广进!^美女:新的一年走桃花运哦,新的一年好运多多,钱多多哦。^" + "男人:百尺竿头,更进一步,读书百遍,其义自见。^" );//个人认为对话时只针对一个目标,所以设定时只要formatMsg()这方法 } String formatMsg(String tmpStr){
StringBuffer tmpStringBuffer= new StringBuffer(""); int j; String s1 = tmpStr; for(; ( j = inOf(s1,msgEnd)) > 0; s1 = subStr(s1,j + 1)){ j = inOf(s1,msgEnd); tmpStringBuffer.append(subStr(s1,0, j)) ; if(j%textValue!=0){ for(int i = 0; i <textValue-j%textValue; i++){ tmpStringBuffer.append(" "); //tmpStringBuffer.setLength(20) ; } } } msg=tmpStringBuffer.toString(); return msg; } public void drawMgs(Graphics g,int tmpX,int tmpY){
if (isShowMsg){ int tmpColor=g.getColor(); g.setColor(0xff0000); g.fillRect(GCW-STRW*textValue/rowValue>>1,GCH-STRH*rowValue>>1,STRW*textValue/rowValue,STRH*rowValue); g.setColor(0xffffff); for(int i = 0; i <rowValue; i++){ try { g.drawString(msg./*formatMsg.substring()*/substring((page*textValue)+ (i*textValue/rowValue),(page*textValue)+((i+1)*textValue/rowValue)),tmpX, tmpY+ (i*Gc.STRH/*行高*/),0); }catch (Exception e){e.printStackTrace();} } g.setColor(tmpColor); } } public void talk(){
if(!isShowMsg){ isShowMsg = true; } else{ if(msg.length() > textValue + (page * textValue)){ page++; }else{ isShowMsg = false; page = 0; } } } void drawAll(){
g.setFont(font); g.setColor(0); g.fillRect(0,0,GCW,GCH); g.setColor(0xffffff); g.drawString("按5键",0,0,0); drawMgs(g,GCW-STRW*textValue/rowValue>>1,GCH-STRH*rowValue>>1); }
public void keyPressed(int keyCode){
if(keyCode==-5|keyCode==53){ talk(); } } public void paint(Graphics g){
this.g=g; } public void run(){
if(!isRun){ return; } while(isRun){ drawAll(); repaint(); try { Thread .sleep(80); }catch (Exception e){e.printStackTrace();} } } public void doStart(){
if(thr==null){ thr= new Thread (this); isRun=true; thr.start(); System.out.println("mainThreadRuning"); } } public void doExit(){ isRun=false; thr=null; //free(); } ///字符串操作方法 public static int toInt(String tempStr){ return Integer.parseInt(tempStr); } public String subStr(String x,int j){ return x.substring(j); } public String subStr(String x,int j,int i){ return x.substring(j,i); } public int inOf(String x,String of){ return x.indexOf(of); } } //end |