j2me程序内存问题

来源:互联网 发布:淘宝网商贷申请条件 编辑:程序博客网 时间:2024/05/04 16:30

我写了个j2me小程序,程序无图片,没有用线程等,无声音等任何资源,只是用paint画了些表格,总共有6个类,打包后大小是60多k,可是在手机上运行内存使用竟然接近4M。
我自己估计应该200k左右内存使用,可是竟然这么大,我按照网上的方法做了,可是似乎不起作用
谁能帮我优化一下呢?顺便请大侠指出代码优化问题

源代码:
[code=Java]

Dialog.java
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;

public class Dialog extends Form implements CommandListener {
 public static final int CANCEL = 8;

 public static final int CONFIRM = 16;

 public static final int NO = 4;

 public static final int OK = 1;

 public static final int YES = 2;

 Command cmCANCEL;

 Command cmCONFIRM;

 Command cmNO;

 Command cmOK;

 Command cmYES;

 Display display;

 DialogListener dll;

 Displayable parent;

 StringItem text;

 public Dialog(String title, String text, int mode, MIDlet midlet,
   DialogListener dll, Displayable dis) {
  super(title);
  this.dll = dll;
  this.text = new StringItem(null, text);
  this.display = Display.getDisplay(midlet);
  this.parent = dis;

  if ((mode & OK) != 0) {
   cmOK = new Command("确定", Command.OK, 1);
   addCommand(cmOK);
  }
  if ((mode & YES) != 0) {
   cmYES = new Command("是", Command.OK, 1);
   addCommand(cmYES);
  }
  if ((mode & CANCEL) != 0) {
   cmCANCEL = new Command("取消", Command.CANCEL, 1);
   addCommand(cmCANCEL);
  }
  if ((mode & CONFIRM) != 0) {
   cmCONFIRM = new Command("应用", Command.SCREEN, 1);
   addCommand(cmCONFIRM);
  }
  if ((mode & NO) != 0) {
   cmNO = new Command("否", Command.EXIT, 1);
   addCommand(cmNO);
  }
  append(text);
  setCommandListener(this);
 }

 public void commandAction(Command c, Displayable s) {
  if (dll != null) {
   if (c == cmOK)
    dll.onOK();
   else if (c == cmYES)
    dll.onYES();
   else if (c == cmNO)
    dll.onNO();
   else if (c == cmCANCEL)
    dll.onCANCELK();
   else if (c == cmCONFIRM)
    dll.onCONFOIRM();

  }
  display.setCurrent(parent);
 }

}

class DialogListener {
 public void onCANCELK() {
 };

 public void onCONFOIRM() {
 };

 public void onNO() {
 };

 public void onOK() {
 };

 public void onYES() {
 };

-----------------------------------------------------------------------------------------------------------
inputCn.java
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class inputCn extends Form implements CommandListener {
 private byte[] bytes;
 private Command detailCmd = new Command("说明", Command.SCREEN, 1);
 private Dialog dl;
 private DialogListener dllistener;
 private Command exitCmd = new Command("退出", Command.SCREEN, 1);
 private int hh = 0, hightByte, lowByte, ascii;
 private Command okCmd = new Command("确定", Command.EXIT, 1);
 private Command optionCmd = new Command("关于", Command.SCREEN, 1);
 private ShowRP srp;
 private String str, message;
 private TestPersonRP tc;
 private TextField tf = new TextField("请输入你的姓名", "", 10, TextField.ANY);

 inputCn(TestPersonRP tc) {
  super("输入你的名字");
  this.tc = tc;
  srp = new ShowRP(tc);
  this.append(tf);
  this.addCommand(okCmd);
  this.addCommand(detailCmd);
  this.addCommand(optionCmd);
  this.addCommand(exitCmd);
  this.setCommandListener(this);
 }

 public void commandAction(Command c, Displayable d) {
  if (c == exitCmd) {
   tc.quitApp();
  } else if (c == okCmd) {
   str = tf.getString();
   if (str.equals("")) {
    dllistener = new DialogListener();
    message = "请输入你的名字!";
    dl = new Dialog("提醒", message, Dialog.OK, tc, dllistener, this);
    tc.dis.setCurrent(dl);
   }else{
   hh = 0;
   hh = 29*this.StrTOUnicode(str.trim()) +70;
   srp.score = hh;
   srp.name = str;
   srp.testRP(hh);
   tc.dis.setCurrent(srp);}
  } else if (c == detailCmd) {
   dllistener = new DialogListener();
   message = "此人品计算器纯属娱乐!/n/n人品值仅供参考/n/n不必太在意/n";
   dl = new Dialog("关于", message, Dialog.OK, tc, dllistener, this);
   tc.dis.setCurrent(dl);
  } else if (c == optionCmd) {
   dllistener = new DialogListener();
   message = "联系邮件:/nzqzhr@163.com/n/n"
     + "QQ联系:/n820125956/n/n感谢您的使用!";
   dl = new Dialog("荣少所有", message, Dialog.OK, tc, dllistener, this);
   tc.dis.setCurrent(dl);
  }
 }

 public int getCnAscii(String cn) {
  bytes = (String.valueOf(cn)).getBytes();
  if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
   return 0;
  }
  if (bytes.length == 1) {
   return bytes[0];
  }
  if (bytes.length == 2) {
   hightByte = 256 + bytes[0];
   lowByte = 256 + bytes[1];
   ascii = (256 * hightByte + lowByte) - 256 * 256;
   return ascii;
  }
  return 0;
 }
 public int StrTOUnicode(String strs) {
    int len = strs.length();
    int x = 0;
    for (int i = 0; i < len; i++) {
     char c = strs.charAt(i);
     x+= ((int)c)*(i+1) + 17;
     System.out.println(x + "ddddddddddd");
    }
    return x;
   }

}

-----------------------------------------------------------------------------------------------------------
ShowRP.java
import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ShowRP extends Canvas {

 public static int strLength(String str, Font font) {
  int l = 0;
  if (str != null) {
   char[] cArray = str.toCharArray();
   for (int i = 0; i < cArray.length; i++) {
    l += font.charWidth(cArray[i]);
   }
  }
  return l;
 }

 private Font fontLarge = Font.getFont(Font.FACE_PROPORTIONAL,
   Font.STYLE_BOLD, Font.SIZE_LARGE);
 private Font fontMidium = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
   Font.SIZE_LARGE);
 public String name;
 public int score;
 private int screenWidth, screenHeight;
 private String showScore1;
 private String showScore2;
 private Image source, copy;
 private TestPersonRP tc;

 public ShowRP(TestPersonRP tc) {
  this.tc = tc;
  this.setFullScreenMode(true);
  screenWidth = this.getWidth();
  screenHeight = this.getHeight();
  if (source == null) {
   try {
    source = Image.createImage("/0top.png");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   copy = Image.createImage(this.screenWidth, this.screenHeight);
   Graphics g = copy.getGraphics();
   g.drawImage(source, 0, 0, Graphics.TOP | Graphics.LEFT);
   g.setColor(173, 203, 249);
   g.fillRect(0, 30, this.screenWidth, this.screenHeight - 30);
   g.drawImage(source, 0, this.screenHeight, Graphics.BOTTOM
     | Graphics.LEFT);
   g.setColor(0, 0, 0);
   g.setFont(Font.getDefaultFont());
   g.drawString("人品计算器", (screenWidth - strLength("人品计算器", Font
     .getDefaultFont())) / 2, 5, Graphics.TOP | Graphics.LEFT);
   g.drawString("返回", 5, this.screenHeight - 3, Graphics.BOTTOM
     | Graphics.LEFT);
   source = null;
  }
  this.testRP(score);
 }

 public void keyPressed(int keyCode) {
  switch (keyCode) {
  case -6:
   tc.changInterface("inputCn");
   break;
  }
 }

 protected void paint(Graphics g) {
  g.drawImage(copy, 0, 0, Graphics.TOP | Graphics.LEFT);

  g.setFont(fontMidium);
  g.drawString("姓名:", 10, screenHeight / 4 + 5, Graphics.BOTTOM
    | Graphics.LEFT);
  g.drawString("你的分数是:", 10, screenHeight / 2 + 5, Graphics.BOTTOM
    | Graphics.LEFT);
  g.drawString("评语:", 10, 3 * screenHeight / 4 - 5, Graphics.BOTTOM
    | Graphics.LEFT);

  g.setFont(fontLarge);
  g.setColor(47, 21, 130);
  g.drawString(name, (screenWidth - strLength(name, fontLarge)) / 3,
    screenHeight / 4 + 30, Graphics.BOTTOM | Graphics.LEFT);
  g.setColor(255, 0, 0);
  g.drawString(String.valueOf(score % 101), screenWidth / 3,
    screenHeight / 2 + 30, Graphics.BOTTOM | Graphics.LEFT);
  g.setColor(172, 102, 189);
  g.drawString(showScore1, 3, 3 * screenHeight / 4 + 16, Graphics.BOTTOM
    | Graphics.LEFT);
  g.drawString(showScore2, 8, 3 * screenHeight / 4 + 40, Graphics.BOTTOM
    | Graphics.LEFT);
 }

 public void testRP(int score) {
  score = score % 101;
  if (score == 0) {
   showScore1 = "你一定不是人吧?";
   showScore2 = "怎么一点人品都没有?!";
  } else if (score > 0 && score < 5) {
   showScore1 = "算了,";
   showScore2 = "跟你没什么人品好谈的...";
  } else if (5 <= score && score < 10) {
   showScore1 = "是我不好...";
   showScore2 = "不应该跟你谈人品问题的...";
  } else if (10 <= score && score < 15) {
   showScore1 = "杀过人没有?放过火没有?";
   showScore2 = "你应该无恶不做吧?";
  } else if (15 <= score && score < 20) {
   showScore1 = "你貌似应该三岁";
   showScore2 = "就偷看隔壁大妈洗澡的吧...";
  } else if (20 <= score && score < 25) {
   showScore1 = "你的人品之低下";
   showScore2 = "实在让人惊讶啊...";
  } else if (25 <= score && score < 30) {
   showScore1 = "你的人品太差了。";
   showScore2 = "你应该有干坏事的嗜好吧?";
  } else if (30 <= score && score < 35) {
   showScore1 = "你的人品真差!";
   showScore2 = "肯定经常做偷鸡摸狗的事...";
  } else if (35 <= score && score < 40) {
   showScore1 = "你拥有如此差的人品";
   showScore2 = "祈求佛祖保佑你吧...";
  } else if (40 <= score && score < 45) {
   showScore1 = "老实交待..那些论坛上面";
   showScore2 = "出现的偷拍照是你的杰作?";
  } else if (45 <= score && score < 50) {
   showScore1 = "你随地大小便之类的事";
   showScore2 = "没少干吧?";
  } else if (50 <= score && score < 55) {
   showScore1 = "你的人品太差了..";
   showScore2 = "稍不小心就会去干坏事了吧?";
  } else if (55 <= score && score < 60) {
   showScore1 = "你的人品很差了..";
   showScore2 = "要克制住做坏事的冲动哦..";
  } else if (60 <= score && score < 65) {
   showScore1 = "你的人品比较差了..";
   showScore2 = "要好好的约束自己啊..";
  } else if (65 <= score && score < 70) {
   showScore1 = "你的人品勉勉强强..";
   showScore2 = "要自己好自为之..";
  } else if (70 <= score && score < 75) {
   showScore1 = "有你这样的人品";
   showScore2 = "算是不错了..";
  } else if (75 <= score && score < 80) {
   showScore1 = "你有较好的人品..";
   showScore2 = "继续保持..";
  } else if (80 <= score && score < 85) {
   showScore1 = "你的人品不错..";
   showScore2 = "应该一表人才吧?";
  } else if (85 <= score && score < 90) {
   showScore1 = "你的人品真好..";
   showScore2 = "做好事是你的爱好吧..";
  } else if (90 <= score && score < 95) {
   showScore1 = "你的人品太好了..";
   showScore2 = "你就是当代活雷锋啊..";
  } else if (95 <= score && score < 100) {
   showScore1 = "你是";
   showScore2 = "世人的榜样!";
  } else if (score == 100) {
   showScore1 = "天啦!你不是人!";
   showScore2 = "你是神!!!";
  } else {
   showScore1 = "唉,无法估计你的人品,";
   showScore2 = "你是魔鬼吧?";
  }
 }

}

----------------------------------------------------------------------------------------------------------
TestPersonRP.java
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class TestPersonRP extends MIDlet {

 public Display dis;
 private inputCn ic;

 public TestPersonRP() {
  ic = new inputCn(this);
  dis = Display.getDisplay(this);
 }

 public void changInterface(String interfaceName) {
  if (interfaceName.equals("inputCn")) {
   dis.setCurrent(ic);
  }
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 }

 protected void pauseApp() {
 }

 public void quitApp() {
  try {
   this.destroyApp(false);
   this.notifyDestroyed();
  } catch (MIDletStateChangeException e) {
   e.printStackTrace();
  }
 }

 protected void startApp() throws MIDletStateChangeException {
  dis.setCurrent(ic);
 }

}
---------------------------------------------------------------------------------------------------------

[/code]

求真相……………………………………

高手顺便指出有什么代码可以优化呢?

万分感谢

原创粉丝点击